Skip to content

Commit 4f77de8

Browse files
committed
styling of swagger UI endpoint
1 parent 74063c1 commit 4f77de8

25 files changed

+24241
-116
lines changed

findthatpostcode/blueprints/__init__.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from fastapi import APIRouter, FastAPI, Request
2-
from fastapi.middleware.cors import CORSMiddleware
1+
from fastapi import APIRouter, Request
32

43
from findthatpostcode.blueprints import (
54
addtocsv,
@@ -16,19 +15,7 @@
1615
from findthatpostcode.db import ElasticsearchDep
1716
from findthatpostcode.utils import templates
1817

19-
app = FastAPI(
20-
docs_url=None,
21-
redoc_url=None,
22-
)
23-
24-
app.add_middleware(
25-
CORSMiddleware,
26-
allow_origins=["*"],
27-
allow_credentials=True,
28-
allow_methods=["*"],
29-
allow_headers=["*"],
30-
expose_headers=["Content-Disposition"],
31-
)
18+
app = APIRouter()
3219

3320

3421
@app.get("/")

findthatpostcode/blueprints/areas.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def areas_csv(areas: Iterator[dict], filename: str) -> Response:
5454

5555

5656
@bp.get("/{areacodes}.geojson")
57-
@api.get("/{areacodes}.geojson")
57+
@api.get("/{areacodes}.geojson", name="legacy_get_area_boundary")
5858
def get_area_boundary(areacodes: str, es: ElasticsearchDep, s3_client: S3Dep):
5959
areacode_list: list[str] = areacodes.split("+")
6060
features = []
@@ -69,7 +69,7 @@ def get_area_boundary(areacodes: str, es: ElasticsearchDep, s3_client: S3Dep):
6969

7070

7171
@bp.get("/{areacode}/children/{areatype}.geojson")
72-
@api.get("/{areacode}/children/{areatype}.geojson")
72+
@api.get("/{areacode}/children/{areatype}.geojson", include_in_schema=False)
7373
def get_area_children_boundary(
7474
areacode: str, areatype: str, es: ElasticsearchDep, s3_client: S3Dep
7575
):
@@ -92,7 +92,7 @@ def get_area_children_boundary(
9292

9393
@bp.get("/{areacode}")
9494
@bp.get("/{areacode}.{filetype}")
95-
@api.get("/{areacode}")
95+
@api.get("/{areacode}", name="legacy_get_area")
9696
def get_area(
9797
areacode: str,
9898
request: Request,

findthatpostcode/blueprints/areatypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def all_areatypes(es: ElasticsearchDep, request: Request) -> Response:
2929

3030
@bp.get("/{areacode}")
3131
@bp.get("/{areacode}.{filetype}")
32-
@api.get("/{areacode}")
32+
@api.get("/{areacode}", name="legacy_get_areatype")
3333
def get_areatype(
3434
areacode: str,
3535
request: Request,

findthatpostcode/blueprints/places.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def point_redirect(lat: float, lon: float, request: Request) -> Response:
2020

2121
@bp.get("/nearest/{lat},{lon}")
2222
@bp.get("/nearest/{lat},{lon}.{filetype}")
23-
@api.get("/nearest/{lat},{lon}")
23+
@api.get("/nearest/{lat},{lon}", include_in_schema=False)
2424
def nearest(
2525
lat: float, lon: float, es: ElasticsearchDep, filetype: str = "json"
2626
) -> Response:
@@ -43,7 +43,7 @@ def nearest(
4343

4444
@bp.get("/{areacode}")
4545
@bp.get("/{areacode}.{filetype}")
46-
@api.get("/{areacode}")
46+
@api.get("/{areacode}", name="legacy_get_place")
4747
def get_place(
4848
areacode: str, es: ElasticsearchDep, request: Request, filetype: str = "json"
4949
):

findthatpostcode/blueprints/points.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def point_redirect(lat: float, lon: float, request: Request) -> Response:
2222

2323

2424
@bp.get("/{latlon}")
25-
@api.get("/{latlon}")
25+
@api.get("/{latlon}", name="legacy_get_point")
2626
def get_point(latlon: str, es: ElasticsearchDep, request: Request):
2727
filetype = "json"
2828
if latlon.endswith(".json"):

findthatpostcode/blueprints/postcodes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def postcode_redirect(postcode: str, request: Request) -> Response:
3030

3131
@bp.get("/{postcode}")
3232
@bp.get("/{postcode}.{filetype}")
33-
@api.get("/{postcode}")
33+
@api.get("/{postcode}", name="legacy_get_postcode")
3434
def get_postcode(
3535
postcode: str, request: Request, es: ElasticsearchDep, filetype: str = "json"
3636
):
@@ -42,7 +42,7 @@ def get_postcode(
4242

4343
@bp.get("/hash/{hash_}")
4444
@bp.get("/hash/{hash_}.json")
45-
@api.get("/hash/{hash_}")
45+
@api.get("/hash/{hash_}", include_in_schema=False)
4646
def single_hash(
4747
hash_: str,
4848
properties: Annotated[list[str], Query()],
@@ -54,7 +54,7 @@ def single_hash(
5454

5555
@bp.get("/hashes")
5656
@bp.get("/hashes.json")
57-
@api.get("/hashes")
57+
@api.get("/hashes", include_in_schema=False)
5858
def multi_hash_get(
5959
hash: Annotated[str | list[str], Query()],
6060
properties: Annotated[list[str], Query()],
@@ -66,7 +66,7 @@ def multi_hash_get(
6666

6767
@bp.post("/hashes")
6868
@bp.post("/hashes.json")
69-
@api.post("/hashes")
69+
@api.post("/hashes", include_in_schema=False)
7070
def multi_hash_post(
7171
hash: Annotated[str | list[str], Form()],
7272
properties: Annotated[list[str], Form()],

findthatpostcode/blueprints/reconcile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ReconcileRequest(BaseModel):
5454

5555

5656
@bp.get("/")
57-
@api.get("/")
57+
@api.get("/", include_in_schema=False)
5858
def reconcile_get(
5959
es: ElasticsearchDep,
6060
extend: Annotated[str | None, Query()] = None,
@@ -65,7 +65,7 @@ def reconcile_get(
6565

6666

6767
@bp.post("/")
68-
@api.post("/")
68+
@api.post("/", include_in_schema=False)
6969
def reconcile_post(
7070
es: ElasticsearchDep,
7171
extend: Annotated[str | None, Form()] = None,

findthatpostcode/main.py

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import os
2+
13
import sentry_sdk
24
from fastapi import FastAPI
35
from fastapi.middleware.cors import CORSMiddleware
6+
from fastapi.openapi.docs import (
7+
get_swagger_ui_html,
8+
get_swagger_ui_oauth2_redirect_html,
9+
)
10+
from fastapi.responses import FileResponse
411
from fastapi.staticfiles import StaticFiles
512
from sentry_sdk.integrations.fastapi import FastApiIntegration
613

@@ -23,11 +30,11 @@
2330
title="Find that Postcode",
2431
version="2.0",
2532
description="""
26-
This site presents data on UK postcodes and geographical areas, based on open data released by
33+
This API presents data on UK postcodes and geographical areas, based on open data released by
2734
the [Office for National Statistics](https://geoportal.statistics.gov.uk/) and
2835
[Ordnance Survey](https://osdatahub.os.uk/).
2936
""",
30-
docs_url="/api/docs",
37+
docs_url=None,
3138
redoc_url=None,
3239
openapi_url="/api/openapi.json",
3340
openapi_tags=[
@@ -41,14 +48,16 @@
4148
},
4249
],
4350
contact={
44-
"name": "Kane Data Ltd",
45-
"url": "https://kanedata.co.uk",
51+
"name": "Find that Postcode",
52+
"url": "https://findthatpostcode.uk",
4653
"email": "info@findthatpostcode.uk",
4754
},
48-
license_info={
49-
"name": "TBD",
50-
# "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
51-
},
55+
# license_info={
56+
# "name": "TBD",
57+
# # "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
58+
# }, # @todo License?
59+
swagger_js_url="/static/swagger-ui/swagger-ui-bundle.js",
60+
swagger_css_url="/static/swagger-ui/swagger-ui.css",
5261
)
5362

5463

@@ -60,8 +69,9 @@
6069

6170
app.include_router(legacy_router, prefix="/api/v1", deprecated=True)
6271
app.include_router(api_router, prefix="/api/v2")
63-
64-
app.mount("/", legacy_app) # Mount the legacy FastAPI app at the root
72+
app.include_router(
73+
legacy_app, include_in_schema=False
74+
) # Mount the legacy FastAPI app at the root
6575

6676
app.add_middleware(
6777
CORSMiddleware,
@@ -71,3 +81,31 @@
7181
allow_headers=["*"],
7282
expose_headers=["Content-Disposition"],
7383
)
84+
85+
86+
@app.get("/favicon.ico", include_in_schema=False)
87+
async def favicon():
88+
return FileResponse(os.path.join(STATIC_DIR, "images/favicon.ico"))
89+
90+
91+
@app.get("/api/docs", include_in_schema=False)
92+
async def custom_swagger_ui_html():
93+
if app.openapi_url:
94+
return get_swagger_ui_html(
95+
openapi_url=app.openapi_url,
96+
title=app.title + " - API Documentation",
97+
oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
98+
swagger_js_url="/static/lib/swagger-ui/swagger-ui-bundle.js",
99+
swagger_css_url="/static/css/swagger-ui.css",
100+
swagger_favicon_url="/favicon.ico",
101+
swagger_ui_parameters={
102+
"defaultModelsExpandDepth": 0,
103+
},
104+
)
105+
106+
107+
if app.swagger_ui_oauth2_redirect_url:
108+
109+
@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
110+
async def swagger_ui_redirect():
111+
return get_swagger_ui_oauth2_redirect_html()

0 commit comments

Comments
 (0)