got error
swadhinbiswas committed 2 years ago
3089cf2
1 parent 5b81fa9
commit 3089cf2
2 files changed
+59 -31
Showing 2 changed files+59-31
▼backend/main.py
+50-19
| @@ -1,39 +1,70 @@ | ||
1 | -from fastapi import FastAPI | |
1 | +from fastapi import FastAPI,Depends | |
2 | 2 | from fastapi.middleware.cors import CORSMiddleware |
3 | 3 | from fastapi.responses import JSONResponse |
4 | 4 | from fastapi.encoders import jsonable_encoder |
5 | +import secure | |
6 | +from settings.setting import settings | |
7 | +from starlette.exceptions import HTTPException as StarletteHTTPException | |
5 | 8 | |
6 | 9 | |
7 | 10 | |
8 | 11 | |
9 | -app = FastAPI() | |
10 | 12 | |
11 | -origins = [ | |
12 | - "http://localhost", | |
13 | - "http://localhost:3000", | |
14 | - "http://localhost:8000", | |
15 | - "http://localhost:8080", | |
16 | - "http://localhost:5000", | |
17 | - "http://localhost:5500", | |
18 | - "http://localhost:4200", | |
19 | - "http://localhost:8081", | |
20 | - "http://localhost:8082", | |
21 | - "http://localhost:8083",] | |
13 | + | |
14 | +app = FastAPI( | |
15 | + openapi_url=f"{settings.API_V1_STR}/openapi.json", | |
16 | + docs_url=f"{settings.API_V1_STR}/docs", | |
17 | + redoc_url=f"{settings.API_V1_STR}/redoc", | |
18 | + | |
19 | +) | |
20 | + | |
21 | +csp=secure.ContentSecurityPolicy().default_src("'self'").frame_ancestors("'none'") | |
22 | +hsts=secure.StrictTransportSecurity().max_age(31536000).include_subdomains() | |
23 | +referrer=secure.ReferrerPolicy().no_referrer() | |
24 | +cache_value=secure.CacheControl().no_cache().no_store().must_revalidate() | |
25 | +x_frame=secure.XFrameOptions().deny() | |
26 | + | |
27 | + | |
28 | +secure_headers = secure.Secure( | |
29 | + csp=csp, | |
30 | + hsts=hsts, | |
31 | + referrer=referrer, | |
32 | + cache=cache_value, | |
33 | + xfo=x_frame, | |
34 | + | |
35 | + | |
36 | +) | |
37 | + | |
38 | +@app.middleware("http") | |
39 | +async def set_secure_headers(request, call_next): | |
40 | + response = await call_next(request) | |
41 | + secure_headers.framework.fastapi(response) | |
42 | + return response | |
43 | + | |
22 | 44 | app.add_middleware( |
23 | 45 | CORSMiddleware, |
24 | - allow_origins=origins, | |
46 | + allow_origins=["*"], | |
47 | + | |
25 | 48 | allow_credentials=True, |
26 | - allow_methods=["*"], | |
27 | - allow_headers=["*"], | |
49 | + allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"], | |
50 | + allow_headers=["Authorization", "Content-Type", "Accept", "Accept-Encoding", "Accept-Language", "Origin", "Referer", "User-Agent", "X-Requested-With", "X-CSRF-Token", "X-CSRFToken", "X-XSRF-TOKEN"], | |
28 | 51 | ) |
29 | 52 | |
30 | 53 | |
31 | -@app.get("/") | |
54 | + | |
55 | +@app.get('/api/test/') | |
32 | 56 | async def root(): |
33 | 57 | return {"message": "Hello World"} |
34 | - | |
58 | + | |
59 | + | |
60 | + | |
61 | + | |
35 | 62 | |
36 | 63 | if __name__ == "__main__": |
37 | 64 | import uvicorn |
38 | - uvicorn.run(app, host="localhost", port=8000) | |
65 | + uvicorn.run(app, | |
66 | + host="localhost", | |
67 | + port=8000, | |
68 | + | |
69 | + server_header=False) | |
39 | 70 | |
40 | 71 | |
▼backend/settings/setting.py
+9-12
| @@ -1,20 +1,16 @@ | ||
1 | - | |
2 | -from imp import reload | |
3 | 1 | from typing import List |
4 | 2 | from decouple import config |
5 | 3 | from pydantic import BaseSettings, AnyHttpUrl, validator |
6 | 4 | |
7 | 5 | class Settings(BaseSettings): |
8 | 6 | API_V1_STR: str = "/api/v1" |
9 | - SECRET_KEY: str | |
10 | - PROJECT_NAME: str | |
11 | - BACKEND_CORS_ORIGINS: List[AnyHttpUrl] | |
12 | - ALGORITHM:str | |
13 | - AUTH0_DOMAIN:str | |
14 | - AUTH0_AUDIENCE:str | |
15 | - CLIENT_URL:str | |
16 | - port : int | |
17 | - reload :bool | |
7 | + SECRET_KEY: str=config("SECRET_KEY") | |
8 | + PROJECT_NAME: str=config("PROJECT_NAME") | |
9 | + ALGORITHM:str=config("ALGORITHM") | |
10 | + AUTH0_DOMAIN:str=config("AUTH0_DOMAIN") | |
11 | + AUTH0_AUDIENCE:str=config("AUTH0_AUDIENCE") | |
12 | + CLIENT_URL:str=config("CLIENT_URL") | |
13 | + reload:bool=config("reload",default=True,cast=bool) | |
18 | 14 | |
19 | 15 | @classmethod |
20 | 16 | @validator("CLIENT_URL","AUTH0_DOMAIN","AUTH0_AUDIENCE") |
| @@ -24,7 +20,8 @@ class Settings(BaseSettings): | ||
24 | 20 | class Config: |
25 | 21 | env_file = ".env" |
26 | 22 | env_file_encoding = "utf-8" |
27 | - case_sensitive = True | |
28 | 23 | |
29 | 24 | |
30 | 25 | settings = Settings() |
26 | + | |
27 | +print(settings.API_V1_STR) | |
31 | 28 | |