82 lines
2.0 KiB
Python
82 lines
2.0 KiB
Python
import asyncio
|
|
import json
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.responses import HTMLResponse, PlainTextResponse
|
|
from starlette.middleware.sessions import SessionMiddleware
|
|
|
|
from .settings import settings
|
|
from .auth import user_auth, config as auth_config
|
|
from .map_tiles import map_tiles, map_meta
|
|
from .templates import j2env
|
|
from .monitoring import monitoring, ws_manager
|
|
from .api import api
|
|
from . import db
|
|
|
|
app = FastAPI()
|
|
|
|
app.add_middleware(SessionMiddleware, secret_key=auth_config.get("SESSION_SECRET_KEY"))
|
|
|
|
app.mount("/user/", user_auth)
|
|
app.mount("/map/", map_meta)
|
|
app.mount("/tiles/", map_tiles)
|
|
app.mount("/ipmi/", monitoring)
|
|
app.mount("/api/", api)
|
|
|
|
|
|
def render_lua_installer():
|
|
with open(f"{settings.lua_out_path}/manifest.txt", "r") as f:
|
|
file_list = [name.strip() for name in f.readlines()]
|
|
return j2env.get_template("install.lua").render(
|
|
deploy_path=settings.deploy_path, file_list=file_list
|
|
)
|
|
|
|
|
|
installer = render_lua_installer()
|
|
|
|
|
|
@app.get("/install")
|
|
async def get_installer():
|
|
if settings.dev_mode:
|
|
installer = render_lua_installer()
|
|
return PlainTextResponse(installer)
|
|
|
|
|
|
app.mount("/lua/", StaticFiles(directory=settings.lua_out_path))
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def on_startup():
|
|
await db.on_startup()
|
|
asyncio.get_running_loop().create_task(ws_manager.queue_task())
|
|
|
|
|
|
frontend = FastAPI()
|
|
|
|
manifest = dict()
|
|
if not settings.dev_mode:
|
|
with open(f"{settings.frontend_path}/manifest.json", "r") as f:
|
|
manifest = json.load(f)
|
|
|
|
|
|
index = j2env.get_template("index.html").render(
|
|
dev_mode=settings.dev_mode,
|
|
manifest=manifest,
|
|
)
|
|
|
|
|
|
@frontend.middleware("http")
|
|
async def index_catch_all(request: Request, call_next):
|
|
response = await call_next(request)
|
|
|
|
if response.status_code == 404:
|
|
return HTMLResponse(index)
|
|
|
|
return response
|
|
|
|
|
|
if not settings.dev_mode:
|
|
frontend.mount("/", StaticFiles(directory=settings.frontend_path))
|
|
|
|
app.mount("/", frontend)
|