Spaces:
Sleeping
Sleeping
Download src/main.py from hashir-qt/keyvault-api: direct link, hf CLI and curl.
- Browser
- Download file 4.18 kB
-
https://huggingface.co/spaces/hashir-qt/keyvault-api/resolve/main/src/main.py
- Command line
-
hf download hf://spaces/hashir-qt/keyvault-api/src/main.py
-
curl -L -o main.py https://huggingface.co/spaces/hashir-qt/keyvault-api/resolve/main/src/main.py
4.18 kB
| """FastAPI application entry point.""" | |
| import logging | |
| import logging.config | |
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from src.config import settings | |
| from src.database import init_db | |
| import src.models # noqa: F401 — registers all models with SQLModel before create_all | |
| from src.routers import admin as admin_router | |
| from src.routers import auth as auth_router | |
| from src.routers import orders as orders_router | |
| from src.routers import products as products_router | |
| from src.routers import seller as seller_router | |
| from src.routers import store as store_router | |
| # --------------------------------------------------------------------------- | |
| # Logging | |
| # --------------------------------------------------------------------------- | |
| LOGGING_CONFIG: dict = { | |
| "version": 1, | |
| "disable_existing_loggers": False, | |
| "formatters": { | |
| "default": { | |
| "format": "%(asctime)s | %(levelname)-8s | %(name)s | %(message)s", | |
| "datefmt": "%Y-%m-%d %H:%M:%S", | |
| }, | |
| }, | |
| "handlers": { | |
| "console": { | |
| "class": "logging.StreamHandler", | |
| "formatter": "default", | |
| "stream": "ext://sys.stdout", | |
| }, | |
| }, | |
| "loggers": { | |
| "marketplace": {"handlers": ["console"], "level": "DEBUG", "propagate": False}, | |
| "uvicorn": {"handlers": ["console"], "level": "INFO", "propagate": False}, | |
| "uvicorn.error": {"handlers": ["console"], "level": "INFO", "propagate": False}, | |
| "uvicorn.access": {"handlers": ["console"], "level": "INFO", "propagate": False}, | |
| "sqlalchemy.engine": { | |
| "handlers": ["console"], | |
| "level": "WARNING", # set to INFO to see all SQL queries | |
| "propagate": False, | |
| }, | |
| }, | |
| "root": {"handlers": ["console"], "level": "INFO"}, | |
| } | |
| logging.config.dictConfig(LOGGING_CONFIG) | |
| logger = logging.getLogger("marketplace") | |
| # --------------------------------------------------------------------------- | |
| # App | |
| # --------------------------------------------------------------------------- | |
| app = FastAPI( | |
| title="Digital Marketplace API", | |
| description="Multi-vendor digital goods marketplace", | |
| version="0.1.0", | |
| docs_url="/docs", | |
| redoc_url="/redoc", | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=settings.cors_origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # Routers | |
| # --------------------------------------------------------------------------- | |
| app.include_router(admin_router.router, prefix="/api/v1/admin", tags=["admin"]) | |
| app.include_router(auth_router.router, prefix="/api/v1/auth", tags=["auth"]) | |
| app.include_router(orders_router.router, prefix="/api/v1/orders", tags=["orders"]) | |
| app.include_router(products_router.router, prefix="/api/v1/products", tags=["products"]) | |
| app.include_router(seller_router.router, prefix="/api/v1/seller", tags=["seller"]) | |
| app.include_router(store_router.router, prefix="/api/v1/store", tags=["store"]) | |
| # --------------------------------------------------------------------------- | |
| # Lifecycle | |
| # --------------------------------------------------------------------------- | |
| async def on_startup() -> None: | |
| """Initialize database tables on startup.""" | |
| logger.info("Starting up — environment: %s", settings.app_env) | |
| try: | |
| await init_db() | |
| logger.info("Database tables ready") | |
| except Exception as exc: | |
| logger.warning("DB init skipped — could not connect at startup: %s", exc) | |
| async def on_shutdown() -> None: | |
| """Clean up on shutdown.""" | |
| logger.info("Shutting down") | |
| # --------------------------------------------------------------------------- | |
| # Routes | |
| # --------------------------------------------------------------------------- | |
| async def health_check(): | |
| """Health check endpoint.""" | |
| return {"status": "ok", "env": settings.app_env} | |