Skip to content

Commit

Permalink
Update the /docs route
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresPradGo committed Mar 12, 2024
1 parent b8c7b0c commit 5d1c25e
Show file tree
Hide file tree
Showing 21 changed files with 50 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from fastapi import FastAPI

from startup.add_documentation import add_documentation
from startup.create_db import create_database
from startup.config_cors import config_cors
from startup import error_logger
Expand All @@ -22,11 +23,14 @@
error_logger.init_logger()

app = FastAPI(
docs_url=None, redoc_url=None,
title="NavCraft API",
version="1.0.0",
description="An API tool to help pilots craft the perfect navigation flight plans. It uses aircraft performance data, and Canadian aviation rules, regulations and definitions, to produce flight plans that include: \n \n - navigation logs, \n \n - weight and balance charts and calculations, \n \n - fuel calculations, \n \n - takeoff and landing distances."
swagger_favicon_url="../public/logo.png",
description="Helping pilots craft the perfect navigation flight plans. NavCraft API uses aircraft performance data, and Canadian aviation rules, regulations and definitions, to produce VFR flight plans that include: \n \n - navigation logs, \n \n - weight and balance graphs and calculations, \n \n - fuel calculations, \n \n - takeoff and landing distances."
)

add_documentation(app)
create_database()
migrate_db()
config_cors(app)
Expand Down
44 changes: 44 additions & 0 deletions src/startup/add_documentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Documentation startup module
Module asigns adds the /docs route to the API, to display the APIS's documentation.
Usage:
- Import the add_documentation function, and pass the FastAPI app as a parameter.
"""

from fastapi import FastAPI, status, HTTPException, Request
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles


def add_documentation(app: FastAPI):
"""
This function adds the API /docs route
"""
app.mount("/static", StaticFiles(directory="static"), name="static")

@app.get("/docs", include_in_schema=False)
def overridden_swagger(req: Request) -> HTMLResponse:
root_path = req.scope.get("root_path", "").rstrip("/")
openapi_url = root_path + app.openapi_url
oauth2_redirect_url = app.swagger_ui_oauth2_redirect_url
if oauth2_redirect_url:
oauth2_redirect_url = root_path + oauth2_redirect_url
return get_swagger_ui_html(
openapi_url=openapi_url,
title=app.title + " | Docs",
oauth2_redirect_url=oauth2_redirect_url,
init_oauth=app.swagger_ui_init_oauth,
swagger_favicon_url="/static/logo.png",
swagger_ui_parameters=app.swagger_ui_parameters,
)

@app.get("/redoc", include_in_schema=False)
def overridden_redoc():
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Not Found"
)
2 changes: 1 addition & 1 deletion src/startup/migrate_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from utils.db import engine, Session
from functions.data_processing import clean_string

_PATH = "static_data/"
_PATH = "static/"


def _set_charracter_set() -> None:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added src/static/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5d1c25e

Please sign in to comment.