-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
38 lines (28 loc) · 1.1 KB
/
main.py
File metadata and controls
38 lines (28 loc) · 1.1 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
from __future__ import annotations
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.requests import Request
from starlette.middleware.base import BaseHTTPMiddleware
from fastapi.responses import JSONResponse
import uvicorn
from api.routes import router as api_router
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
class LimitUploadSizeMiddleware(BaseHTTPMiddleware):
def __init__(self, app, max_upload_size: int):
super().__init__(app)
self.max_upload_size = max_upload_size
async def dispatch(self, request: Request, call_next):
cl = request.headers.get("content-length")
if cl and int(cl) > self.max_upload_size:
return JSONResponse(status_code=413, content={"detail": "too damn big twin"})
return await call_next(request)
app.add_middleware(LimitUploadSizeMiddleware, max_upload_size=100 * 1024 * 1024)
app.include_router(api_router, prefix="/api")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7272)