from fastapi import FastAPI, Request from config.config import LearnHouseConfig, get_learnhouse_config from src.core.events.events import shutdown_app, startup_app from src.router import v1_router from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from fastapi.staticfiles import StaticFiles from fastapi_jwt_auth.exceptions import AuthJWTException from fastapi.middleware.gzip import GZipMiddleware # from src.services.mocks.initial import create_initial_data ######################## # Pre-Alpha Version 0.1.0 # Author: @swve # (c) LearnHouse 2022 ######################## # Get LearnHouse Config learnhouse_config: LearnHouseConfig = get_learnhouse_config() # Global Config app = FastAPI( title=learnhouse_config.site_name, description=learnhouse_config.site_description, version="0.1.0", root_path="/", ) app.add_middleware( CORSMiddleware, allow_origin_regex=learnhouse_config.hosting_config.allowed_regexp, allow_methods=["*"], allow_credentials=True, allow_headers=["*"], ) # Gzip Middleware (will add brotli later) app.add_middleware(GZipMiddleware, minimum_size=1000) # Events app.add_event_handler("startup", startup_app(app)) app.add_event_handler("shutdown", shutdown_app(app)) # JWT Exception Handler @app.exception_handler(AuthJWTException) def authjwt_exception_handler(request: Request, exc: AuthJWTException): return JSONResponse( status_code=exc.status_code, # type: ignore content={"detail": exc.message}, # type: ignore ) # Static Files app.mount("/content", StaticFiles(directory="content"), name="content") # Global Routes app.include_router(v1_router) # General Routes @app.get("/") async def root(): return {"Message": "Welcome to LearnHouse ✨"}