Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from contextlib import asynccontextmanager | |
| from database import engine, get_db | |
| from models import Base | |
| from routes import auth_routes, game, admin | |
| from auth import init_admin_user | |
| async def lifespan(app: FastAPI): | |
| """Lifecycle events""" | |
| # Startup | |
| Base.metadata.create_all(bind=engine) | |
| # Инициализация администратора | |
| db = next(get_db()) | |
| try: | |
| init_admin_user(db) | |
| finally: | |
| db.close() | |
| print("✅ Database initialized") | |
| print("✅ Admin user initialized") | |
| yield | |
| # Shutdown | |
| print("👋 Shutting down...") | |
| app = FastAPI( | |
| title="2D Game API", | |
| description="Backend API для 2D игры с авторизацией и статистикой", | |
| version="1.0.0", | |
| lifespan=lifespan | |
| ) | |
| # CORS настройки | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # В продакшене укажите конкретные домены | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Подключение роутеров | |
| app.include_router(auth_routes.router) | |
| app.include_router(game.router) | |
| app.include_router(admin.router) | |
| async def root(): | |
| """Корневой эндпоинт""" | |
| return { | |
| "message": "2D Game API", | |
| "version": "1.0.0", | |
| "docs": "/docs", | |
| "endpoints": { | |
| "auth": "/api/auth", | |
| "game": "/api/game", | |
| "admin": "/api/admin" | |
| } | |
| } | |
| async def health_check(): | |
| """Проверка работоспособности""" | |
| return {"status": "healthy"} | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) | |