|
| 1 | +import os |
| 2 | + |
1 | 3 | import sentry_sdk |
2 | 4 | from fastapi import FastAPI |
3 | 5 | 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 |
4 | 11 | from fastapi.staticfiles import StaticFiles |
5 | 12 | from sentry_sdk.integrations.fastapi import FastApiIntegration |
6 | 13 |
|
|
23 | 30 | title="Find that Postcode", |
24 | 31 | version="2.0", |
25 | 32 | 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 |
27 | 34 | the [Office for National Statistics](https://geoportal.statistics.gov.uk/) and |
28 | 35 | [Ordnance Survey](https://osdatahub.os.uk/). |
29 | 36 | """, |
30 | | - docs_url="/api/docs", |
| 37 | + docs_url=None, |
31 | 38 | redoc_url=None, |
32 | 39 | openapi_url="/api/openapi.json", |
33 | 40 | openapi_tags=[ |
|
41 | 48 | }, |
42 | 49 | ], |
43 | 50 | contact={ |
44 | | - "name": "Kane Data Ltd", |
45 | | - "url": "https://kanedata.co.uk", |
| 51 | + "name": "Find that Postcode", |
| 52 | + "url": "https://findthatpostcode.uk", |
46 | 53 | "email": "info@findthatpostcode.uk", |
47 | 54 | }, |
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", |
52 | 61 | ) |
53 | 62 |
|
54 | 63 |
|
|
60 | 69 |
|
61 | 70 | app.include_router(legacy_router, prefix="/api/v1", deprecated=True) |
62 | 71 | 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 |
65 | 75 |
|
66 | 76 | app.add_middleware( |
67 | 77 | CORSMiddleware, |
|
71 | 81 | allow_headers=["*"], |
72 | 82 | expose_headers=["Content-Disposition"], |
73 | 83 | ) |
| 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