-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (56 loc) · 1.57 KB
/
main.py
File metadata and controls
68 lines (56 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import uvicorn
import logging
import redis.asyncio as redis
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi_limiter import FastAPILimiter
from routers import AuthRouter, AppRouter
from core.config import settings
# Logging configuration
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Lifespan events handler for the FastAPI application.
This context manager handles startup and shutdown events.
It initializes the Redis connection for rate limiting on startup
and closes it on shutdown.
Args:
app (FastAPI): The FastAPI application instance.
"""
app.state.redis = redis.from_url(settings.REDIS_URL, encoding="utf-8", decode_responses=True)
await FastAPILimiter.init(app.state.redis)
yield
await app.state.redis.close()
allow_origins = [
"http://192.168.0.92",
"http://localhost"
]
app = FastAPI(
title="Documents Exp API",
docs_url=None,
redoc_url=None,
openapi_url=None,
lifespan=lifespan,
root_path="/documents-exp/api"
)
app.add_middleware(
CORSMiddleware,
allow_origins=allow_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(AuthRouter)
app.include_router(AppRouter)
if __name__ == "__main__":
uvicorn.run(
"main:app",
host=settings.SERVER_HOST,
port=settings.SERVER_PORT,
reload=False
)