-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
132 lines (92 loc) · 3.11 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from fastapi import FastAPI,HTTPException,Request,status
from fastapi.responses import JSONResponse
from app.config.database import engine , SessionLocal
import app.models as models
from app.utils.jwt import JWT
from app.v1.routers import auth, user,lawyer,review,appointement,location
from starlette.middleware.sessions import SessionMiddleware
from starlette.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
models.Base.metadata.create_all(bind=engine)
app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="!itsasecret")
origins = [
"http://localhost",
"http://localhost:5050",
"http://192.168.43.5",
"*"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")
non_authenticated_routes = [
"/","/docs",'/openapi.json',
"/api/lawyers/user",
"/api/lawyers/pending",
"/api/lawyers/categories",
"/api/lawyers/highest_rated",
"/api/lawyers/accepeted",
"/api/users/"
"/api/lawyers/",
]
authenticated_routes= [
"/api/reviews/add"
]
@app.middleware("http")
async def auth_middleware(request: Request, call_next):
print(request.url.path)
if not (request.url.path == "/api/lawyers/update/lawyer") :
if request.url.path in non_authenticated_routes or request.url.path.startswith(
(
"/api/location/",
"/api/auth/",
"/uploads/",
"/api/lawyers/lawyer/",
"/api/reviews/lawyer/",
"/api/lawyers/",
)
) :
print("non authenticated route : " + request.url.path)
response = await call_next(request)
return response
token = request.headers.get("Authorization")
print(token)
token = token.split(" ")[1] if token else None
if token:
try:
payload = JWT.verify_token(token)
request.state.user = payload
except:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid email or password")
else:
return JSONResponse({
"message": "Unauthorized",
"status_code": 401,
}, status_code=status.HTTP_401_UNAUTHORIZED)
response = await call_next(request)
return response
app.include_router(auth.router)
app.include_router(user.router)
app.include_router(lawyer.router)
app.include_router(review.router)
app.include_router(appointement.router)
app.include_router(location.router)
@app.get("/")
async def root():
return JSONResponse({
"message": "Welcome to FastAPI",
})
# @app.middleware("http")
# async def add_logger(request: Request, call_next):
# response = await custom_logger(request, call_next)
# return response
# @app.middleware("http")
# async def apply_auth_middleware(request: Request, call_next):
# if request.url.path.startswith("/users"):
# return await auth_middleware(request, call_next)
# return await call_next(request)