gitscope
https://dev.kronoz.win/r/marzneshin.git
app/marzneshin.py · v0.7.3
Source
1 f7af2b3 Dawsh 2y ago import logging
2 d377c4c Dawsh 2y ago from contextlib import asynccontextmanager
3 f4fecee Dawsh 2y ago from typing import AsyncGenerator
4 f4fecee Dawsh 2y ago
5 f7af2b3 Dawsh 2y ago from apscheduler.schedulers.asyncio import AsyncIOScheduler
6 f7af2b3 Dawsh 2y ago from fastapi import FastAPI, Request, status
7 f7af2b3 Dawsh 2y ago from fastapi.encoders import jsonable_encoder
8 f7af2b3 Dawsh 2y ago from fastapi.exceptions import RequestValidationError
9 f7af2b3 Dawsh 2y ago from fastapi.middleware.cors import CORSMiddleware
10 eafb4a4 Mrdn 2y ago from fastapi.responses import HTMLResponse, JSONResponse
11 d4ec019 Dawsh 2y ago from fastapi_pagination import add_pagination
12 f7af2b3 Dawsh 2y ago from starlette.staticfiles import StaticFiles
13 eafb4a4 Mrdn 2y ago from uvicorn import Config, Server
14 f7af2b3 Dawsh 2y ago
15 8111eb7 Dawsh 2y ago from app.config.env import (
16 01cdf2e Dawsh 2y ago DEBUG,
17 01cdf2e Dawsh 2y ago DOCS,
18 01cdf2e Dawsh 2y ago HOME_PAGE_TEMPLATE,
19 01cdf2e Dawsh 2y ago UVICORN_HOST,
20 01cdf2e Dawsh 2y ago UVICORN_PORT,
21 01cdf2e Dawsh 2y ago UVICORN_SSL_CERTFILE,
22 01cdf2e Dawsh 2y ago UVICORN_SSL_KEYFILE,
23 01cdf2e Dawsh 2y ago UVICORN_UDS,
24 a0e778c Mrdn 2y ago DASHBOARD_PATH,
25 7caa138 BrainFuck Developer 1y ago TASKS_RECORD_USER_USAGES_INTERVAL,
26 7caa138 BrainFuck Developer 1y ago TASKS_REVIEW_USERS_INTERVAL,
27 7caa138 BrainFuck Developer 1y ago TASKS_EXPIRE_DAYS_REACHED_INTERVAL,
28 7caa138 BrainFuck Developer 1y ago TASKS_RESET_USER_DATA_USAGE,
29 01cdf2e Dawsh 2y ago )
30 8111eb7 Dawsh 2y ago from app.templates import render_template
31 8440155 mahdim10 1y ago from . import __version__
32 2de9d72 Dawsh 2y ago from .routes import api_router
33 01cdf2e Dawsh 2y ago from .tasks import (
34 01cdf2e Dawsh 2y ago nodes_startup,
35 01cdf2e Dawsh 2y ago record_user_usages,
36 01cdf2e Dawsh 2y ago reset_user_data_usage,
37 01cdf2e Dawsh 2y ago review_users,
38 8440155 mahdim10 1y ago expire_days_reached,
39 01cdf2e Dawsh 2y ago )
40 8440155 mahdim10 1y ago from .webhooks import webhooks_router
41 f7af2b3 Dawsh 2y ago
42 f7af2b3 Dawsh 2y ago logger = logging.getLogger(__name__)
43 f7af2b3 Dawsh 2y ago
44 f4fecee Dawsh 2y ago
45 f4fecee Dawsh 2y ago @asynccontextmanager
46 f4fecee Dawsh 2y ago async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
47 f4fecee Dawsh 2y ago await nodes_startup()
48 f4fecee Dawsh 2y ago yield
49 f4fecee Dawsh 2y ago scheduler.shutdown()
50 f4fecee Dawsh 2y ago
51 f4fecee Dawsh 2y ago
52 f7af2b3 Dawsh 2y ago app = FastAPI(
53 f7af2b3 Dawsh 2y ago title="MarzneshinAPI",
54 f7af2b3 Dawsh 2y ago description="Unified GUI Censorship Resistant Solution Powered by Xray",
55 f7af2b3 Dawsh 2y ago version=__version__,
56 f4fecee Dawsh 2y ago lifespan=lifespan,
57 f7af2b3 Dawsh 2y ago docs_url="/docs" if DOCS else None,
58 f7af2b3 Dawsh 2y ago redoc_url="/redoc" if DOCS else None,
59 f7af2b3 Dawsh 2y ago )
60 f7af2b3 Dawsh 2y ago
61 8440155 mahdim10 1y ago app.webhooks.include_router(webhooks_router)
62 8440155 mahdim10 1y ago
63 2de9d72 Dawsh 2y ago app.include_router(api_router)
64 d4ec019 Dawsh 2y ago add_pagination(app)
65 f7af2b3 Dawsh 2y ago
66 f7af2b3 Dawsh 2y ago
67 f7af2b3 Dawsh 2y ago @app.get("/", response_class=HTMLResponse)
68 2de9d72 Dawsh 2y ago def home_page():
69 f7af2b3 Dawsh 2y ago return render_template(HOME_PAGE_TEMPLATE)
70 f7af2b3 Dawsh 2y ago
71 f7af2b3 Dawsh 2y ago
72 f7af2b3 Dawsh 2y ago app.add_middleware(
73 f7af2b3 Dawsh 2y ago CORSMiddleware,
74 f7af2b3 Dawsh 2y ago allow_origins=["*"],
75 f7af2b3 Dawsh 2y ago allow_credentials=True,
76 f7af2b3 Dawsh 2y ago allow_methods=["*"],
77 f7af2b3 Dawsh 2y ago allow_headers=["*"],
78 f7af2b3 Dawsh 2y ago )
79 f7af2b3 Dawsh 2y ago
80 f7af2b3 Dawsh 2y ago scheduler = AsyncIOScheduler(timezone="UTC")
81 f1ab16c Dawsh 2y ago scheduler.add_job(
82 7caa138 BrainFuck Developer 1y ago record_user_usages,
83 7caa138 BrainFuck Developer 1y ago "interval",
84 7caa138 BrainFuck Developer 1y ago coalesce=True,
85 7caa138 BrainFuck Developer 1y ago seconds=TASKS_RECORD_USER_USAGES_INTERVAL,
86 7caa138 BrainFuck Developer 1y ago )
87 7caa138 BrainFuck Developer 1y ago scheduler.add_job(
88 7caa138 BrainFuck Developer 1y ago review_users,
89 7caa138 BrainFuck Developer 1y ago "interval",
90 7caa138 BrainFuck Developer 1y ago seconds=TASKS_REVIEW_USERS_INTERVAL,
91 7caa138 BrainFuck Developer 1y ago coalesce=True,
92 7caa138 BrainFuck Developer 1y ago max_instances=1,
93 f1ab16c Dawsh 2y ago )
94 8440155 mahdim10 1y ago scheduler.add_job(
95 7caa138 BrainFuck Developer 1y ago expire_days_reached,
96 7caa138 BrainFuck Developer 1y ago "interval",
97 7caa138 BrainFuck Developer 1y ago seconds=TASKS_EXPIRE_DAYS_REACHED_INTERVAL,
98 7caa138 BrainFuck Developer 1y ago coalesce=True,
99 7caa138 BrainFuck Developer 1y ago max_instances=1,
100 7caa138 BrainFuck Developer 1y ago )
101 7caa138 BrainFuck Developer 1y ago scheduler.add_job(
102 7caa138 BrainFuck Developer 1y ago reset_user_data_usage,
103 7caa138 BrainFuck Developer 1y ago "interval",
104 7caa138 BrainFuck Developer 1y ago seconds=TASKS_RESET_USER_DATA_USAGE,
105 7caa138 BrainFuck Developer 1y ago coalesce=True,
106 8440155 mahdim10 1y ago )
107 f7af2b3 Dawsh 2y ago
108 f7af2b3 Dawsh 2y ago
109 f7af2b3 Dawsh 2y ago @app.exception_handler(RequestValidationError)
110 f1ab16c Dawsh 2y ago def validation_exception_handler(
111 f1ab16c Dawsh 2y ago request: Request, exc: RequestValidationError
112 f1ab16c Dawsh 2y ago ):
113 f7af2b3 Dawsh 2y ago details = {}
114 f7af2b3 Dawsh 2y ago for error in exc.errors():
115 f7af2b3 Dawsh 2y ago details[error["loc"][-1]] = error.get("msg")
116 f7af2b3 Dawsh 2y ago return JSONResponse(
117 f7af2b3 Dawsh 2y ago status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
118 f7af2b3 Dawsh 2y ago content=jsonable_encoder({"detail": details}),
119 f7af2b3 Dawsh 2y ago )
120 f7af2b3 Dawsh 2y ago
121 f7af2b3 Dawsh 2y ago
122 f7af2b3 Dawsh 2y ago async def main():
123 eafb4a4 Mrdn 2y ago if not DEBUG:
124 f7af2b3 Dawsh 2y ago app.mount(
125 a0e778c Mrdn 2y ago DASHBOARD_PATH,
126 4466347 Mrdn 2y ago StaticFiles(directory="dashboard/dist", html=True),
127 4466347 Mrdn 2y ago name="dashboard",
128 f7af2b3 Dawsh 2y ago )
129 a0e778c Mrdn 2y ago app.mount(
130 a0e778c Mrdn 2y ago "/static/",
131 a0e778c Mrdn 2y ago StaticFiles(directory="dashboard/dist/static"),
132 a0e778c Mrdn 2y ago name="static",
133 a0e778c Mrdn 2y ago )
134 a0e778c Mrdn 2y ago app.mount(
135 a0e778c Mrdn 2y ago "/locales/",
136 a0e778c Mrdn 2y ago StaticFiles(directory="dashboard/dist/locales"),
137 a0e778c Mrdn 2y ago name="locales",
138 a0e778c Mrdn 2y ago )
139 f7af2b3 Dawsh 2y ago scheduler.start()
140 f7af2b3 Dawsh 2y ago cfg = Config(
141 f7af2b3 Dawsh 2y ago app=app,
142 f7af2b3 Dawsh 2y ago host=UVICORN_HOST,
143 f7af2b3 Dawsh 2y ago port=UVICORN_PORT,
144 f7af2b3 Dawsh 2y ago uds=(None if DEBUG else UVICORN_UDS),
145 f7af2b3 Dawsh 2y ago ssl_certfile=UVICORN_SSL_CERTFILE,
146 f7af2b3 Dawsh 2y ago ssl_keyfile=UVICORN_SSL_KEYFILE,
147 f7af2b3 Dawsh 2y ago workers=1,
148 f7af2b3 Dawsh 2y ago reload=DEBUG,
149 f7af2b3 Dawsh 2y ago log_level=logging.DEBUG if DEBUG else logging.INFO,
150 f7af2b3 Dawsh 2y ago )
151 f7af2b3 Dawsh 2y ago server = Server(cfg)
152 f7af2b3 Dawsh 2y ago await server.serve()