forked from SVcheburator/pixels_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (70 loc) · 2.21 KB
/
main.py
File metadata and controls
93 lines (70 loc) · 2.21 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import redis.asyncio as redis
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request, Depends
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi_limiter.depends import RateLimiter
from fastapi.middleware.cors import CORSMiddleware
from fastapi_limiter import FastAPILimiter
import uvicorn
import logging
from src.conf.config import settings
from src.routes import users, comments, auth, tools, static, posts, cloudinary_route
@asynccontextmanager
async def lifespan(app: FastAPI):
await startup()
yield
templates = Jinja2Templates(directory="templates")
app = FastAPI(lifespan=lifespan) # type: ignore
app.include_router(users.router, prefix="/api")
app.include_router(auth.router, prefix="/api")
app.include_router(comments.router, prefix="/api")
app.include_router(tools.router, prefix="/api")
app.include_router(posts.posts_router, prefix='/posts',
dependencies=[Depends(RateLimiter(times=2, seconds=5))])
app.include_router(cloudinary_route.cloud_router, prefix='/cloudinary')
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# @app.on_event("startup")
async def startup():
"""
Startup function.
:return: None.
:rtype: None
"""
r = await redis.Redis(
host=settings.redis_host,
port=settings.redis_port,
password=settings.redis_password,
db=0,
encoding="utf-8",
decode_responses=True,
)
await FastAPILimiter.init(r)
@app.get("/", response_class=HTMLResponse, tags=["Main index.html"])
async def main(request: Request):
"""
Main HTML index page.
:return: HTML index page.
:rtype:
"""
return templates.TemplateResponse(
"index.html", {"request": request, "tilte": "Pixel Project"}
)
static.add_static(app)
# @app.get("/")
# def read_root():
# """
# Main function.
# :return: Message.
# :rtype: dict
# """
# return {"message": "That's root"}
if __name__ == "__main__":
uvicorn.run("main:app", host=settings.app_host, port=settings.app_port, reload=True)
logging.basicConfig(level=logging.INFO)