-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (48 loc) · 1.78 KB
/
main.py
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
import uvicorn
from multiprocessing import cpu_count
from fastapi import FastAPI, Request, Response
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from starlette.exceptions import HTTPException as StarletteHTTPException
from apps import routers
from config import cfg
app = FastAPI(
title="SOP software",
description="Write, monitor and improve Standard Operating Procedures",
)
@app.get("/")
def it_works():
return "It works!"
[app.include_router(router) for router in routers]
app.mount("/public", StaticFiles(directory="public"), name="public")
app.add_middleware(
CORSMiddleware,
allow_origins=[url.strip() for url in cfg.ALLOWED_ORIGINS.split(" ") if url],
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "OPTIONS", "DELETE"],
)
@app.exception_handler(StarletteHTTPException)
async def exception_handler(request: Request, exc: StarletteHTTPException):
return RedirectResponse("/", status_code=302)
@app.middleware("http")
async def security_headers(request: Request, call_next):
response: Response = await call_next(request)
response.headers[
"strict-transport-security"
] = "max-age=63072000; includeSubdomains; preload"
response.headers["x-frame-options"] = "SAMEORIGIN"
response.headers["x-content-type-options"] = "nosniff"
response.headers["x-xss-protection"] = "0"
response.headers["referrer-policy"] = "no-referrer, strict-origin-when-cross-origin"
return response
if __name__ == "__main__":
uvicorn.run(
app="main:app",
host=cfg.HOST,
port=cfg.PORT,
reload=cfg.DEBUG,
proxy_headers=True,
forwarded_allow_ips="*",
workers=1 if cfg.DEBUG else cpu_count(),
)