import logging logger = logging.getLogger(__name__) import os from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from fastapi.security import OAuth2PasswordBearer from contextlib import asynccontextmanager from dotenv import load_dotenv load_dotenv() from app.controllers import userManager, cardManager, groupManager, aaManager, doorManager, debugManager from app.services.database import create_db_and_tables, get_db_session from app.services.auth import token_router, create_first_user from app.services.settings import verify_settings from app.services.scanner import BackgroundScanner oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") scanner = BackgroundScanner(db=get_db_session()) logging.basicConfig(level=logging.INFO) @asynccontextmanager async def lifespan(app: FastAPI): verify_settings() create_db_and_tables() create_first_user(db=get_db_session()) if not os.getenv("DISABLE_CARDS"): scanner.start() logger.info("-"*63) logger.info("---- Documentation is at http://127.0.0.1:8000/api/v1/docs ----") logger.info("-"*63) yield #scanner.stop() app = FastAPI( lifespan=lifespan, docs_url="/api/v1/docs", openapi_url="/api/v1/openapi.json" ) origins = [ "http://127.0.0.1", "http://localhost", "http://localhost:8080", ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["GET", "PUT", "POST", "DELETE", "PATCH"], allow_headers=["*"], ) app.include_router(token_router) app.include_router(userManager.user_router) app.include_router(groupManager.group_router) app.include_router(cardManager.card_router) app.include_router(aaManager.aa_router) app.include_router(doorManager.door_router) app.include_router(debugManager.debug_router)