Create authorization system

This commit is contained in:
2026-01-05 23:31:36 +03:00
parent d438f7bf5b
commit 2e14a7f364
39 changed files with 2500 additions and 9 deletions
Regular → Executable
+75 -4
View File
@@ -1,14 +1,85 @@
from contextlib import asynccontextmanager
import uvicorn
import logging
from fastapi import FastAPI
from fastapi import FastAPI, APIRouter, Request, Response
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
from app.utils.redis import close_redis, init_redis
from app.users.router import user_router, auth_router
from app.log_config import set_logging
from app.config import settings
set_logging()
log = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
await init_redis()
log.info("Redis connected")
yield
await close_redis()
log.info("Redis disconnected")
api_router = APIRouter(prefix="/api/v1")
api_router.include_router(user_router)
api_router.include_router(auth_router)
app = FastAPI(
title=settings.COMPANY_NAME,
description="## Backend messenger aether",
lifespan=lifespan
)
app.include_router(api_router)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=True,
allow_methods=settings.CORS_METHODS,
allow_headers=settings.CORS_HEADERS,
)
@app.middleware("http")
async def log_requests(request: Request, call_next):
response: Response = await call_next(request)
log.info(
"method=%s path=%s status=%s",
request.method,
request.url.path,
response.status_code,
extra={
"method": request.method,
"path": request.url.path,
"status": response.status_code
}
)
return response
@app.get("/health")
async def test_health():
return {"status": "ok"}
return {"status": True}
if __name__ == "__main__":
uvicorn.run("app.main:app", host="0.0.0.0", port=8080, reload=True, workers=3)
if settings.MODE == "PROD":
UVICORN_PARAMS = dict(
host=settings.HOST,
port=settings.PORT,
reload=False,
workers=settings.WORKERS,
access_log=False
)
else:
UVICORN_PARAMS = dict(
host=settings.HOST,
port=settings.PORT,
reload=True,
access_log=False
)
log.info("app is starting")
uvicorn.run("app.main:app", **UVICORN_PARAMS)