-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (37 loc) · 1.51 KB
/
main.py
File metadata and controls
51 lines (37 loc) · 1.51 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
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from auth.routes import auth
from quiz.routes import quiz
from db import models
from db.database import engine
def create_app():
app = FastAPI(docs_url=None, redoc_url=None)
app.include_router(auth)
app.include_router(quiz)
app.mount('/static', StaticFiles(directory="static"), name="static")
models.Base.metadata.create_all(bind=engine)
return app
app = create_app()
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
return RedirectResponse(request.url_for('login_get'))
@app.get('/logout')
async def logout_copy(request: Request):
return RedirectResponse(request.url_for('logout'))
@app.get('/login')
async def login_copy(request: Request):
return RedirectResponse(request.url_for('login_get'))
@app.get('/signup')
async def signup_copy(request: Request):
return RedirectResponse(request.url_for('signup_get'))
@app.middleware("http")
async def security_middleware(request: Request, call_next):
path = str(request.url.path)
cookie = request.cookies.get('hash')
awaited_response = await call_next(request)
if not (cookie or 'auth' in path) and all(x not in path for x in ['logout', 'static']):
return RedirectResponse(request.url_for('login_get'))
if cookie and 'auth' in path and 'logout' not in path:
return RedirectResponse(request.url_for('lobby_get'))
return awaited_response