-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.py
More file actions
45 lines (34 loc) · 1.28 KB
/
web.py
File metadata and controls
45 lines (34 loc) · 1.28 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
import uvicorn
from pathlib import Path
from fastapi import FastAPI, APIRouter
from starlette.responses import FileResponse
class WebAppWrapper:
def __init__(self, res_path: Path, port: int = 5000):
self.router = APIRouter()
self.res_path = res_path
self.port = port
self.app = None
self.srv = None
def run(self) -> uvicorn.Server:
self.app = FastAPI(title='TGBF2')
self.app.include_router(self.router)
@self.app.exception_handler(404)
async def ex(req, exc): return FileResponse(self.res_path / '404.html')
@self.app.get('/', include_in_schema=False)
async def root(): return FileResponse(self.res_path / 'root.html')
self.srv = uvicorn.Server(
uvicorn.Config(self.app, host='0.0.0.0', port=self.port)
)
return self.srv
def add_endpoint(self, path, endpoint):
if self.app:
self.app.add_api_route(path, endpoint)
else:
self.router.add_api_route(path, endpoint)
def remove_endpoint(self, path):
for route in self.router.routes:
if route.path == path:
if self.app:
self.app.routes.remove(route)
else:
self.router.routes.remove(route)