Files
gatekeeper/app/main.py
2026-06-12 13:52:44 +02:00

61 lines
1.8 KiB
Python

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
from .controllers import userManager, cardManager, groupManager, aaManager, doorManager, debugManager
from .services.database import create_db_and_tables, get_db_session
from .services.auth import token_router, create_first_user
from app.services.scanner import BackgroundScanner
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
scanner = BackgroundScanner(db=get_db_session())
logging.basicConfig(level=logging.INFO)
def checkDeps():
load_dotenv()
MIFARE_APP_MASTER_KEY = os.getenv('MIFARE_APP_MASTER_KEY')
if not MIFARE_APP_MASTER_KEY:
logger.critical(f"MIFARE APP MASTER KEY not found!")
logger.critical("Writing and reading cards is disabled!")
@asynccontextmanager
async def lifespan(app: FastAPI):
checkDeps()
create_db_and_tables()
create_first_user(db=get_db_session())
logger.info("Database created and tables initialized.")
disableCards = os.getenv("DISABLE_CARDS")
if not disableCards:
scanner.start()
yield
#scanner.stop()
app = FastAPI(lifespan=lifespan)
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)