feat: add base FastAPI project structure with static frontend and API v1
This commit is contained in:
49
app/main.py
49
app/main.py
@@ -0,0 +1,49 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import ORJSONResponse, JSONResponse, FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from pathlib import Path
|
||||
|
||||
# Import de la config
|
||||
from app.core.config import settings
|
||||
|
||||
# Import du router v1
|
||||
from app.api.v1 import v1_router
|
||||
|
||||
# Création de l'app FastAPI
|
||||
app = FastAPI(
|
||||
title=settings.APP_TITLE,
|
||||
version=settings.APP_VERSION,
|
||||
default_response_class=ORJSONResponse if settings.ORJSON_STRICT else JSONResponse,
|
||||
debug=settings.DEBUG,
|
||||
|
||||
# Ajout de la documentation
|
||||
openapi_url=None if settings.ENV == "prod" else "/openapi.json",
|
||||
docs_url=None if settings.ENV == "prod" else "/docs",
|
||||
redoc_url=None if settings.ENV == "prod" else "/redoc",
|
||||
)
|
||||
|
||||
# =========================
|
||||
# Static files
|
||||
# =========================
|
||||
|
||||
STATIC_PATH = Path(__file__).parent / "static"
|
||||
app.mount("/assets", StaticFiles(directory=STATIC_PATH / "assets"), name="assets")
|
||||
|
||||
# =========================
|
||||
# Inclure les routes v1
|
||||
# =========================
|
||||
app.include_router(v1_router, prefix="/api/v1")
|
||||
|
||||
|
||||
# =========================
|
||||
# Static pages
|
||||
# =========================
|
||||
|
||||
def add_page(endpoint: str, html_file: str):
|
||||
file_path = STATIC_PATH / html_file
|
||||
@app.get(endpoint, include_in_schema=False)
|
||||
def page():
|
||||
return FileResponse(file_path)
|
||||
|
||||
|
||||
add_page("/", "index.html")
|
||||
Reference in New Issue
Block a user