PagerMaid-Pyro/pagermaid/web/__init__.py

61 lines
1.7 KiB
Python
Raw Normal View History

2023-01-31 16:24:56 +00:00
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from starlette.middleware.cors import CORSMiddleware
2023-01-31 16:24:56 +00:00
from starlette.responses import RedirectResponse
from pagermaid.config import Config
from pagermaid.web.api import base_api_router
from pagermaid.web.pages import admin_app, login_page
2023-01-31 16:24:56 +00:00
2023-03-12 03:56:01 +00:00
requestAdaptor = """
2023-01-31 16:24:56 +00:00
requestAdaptor(api) {
api.headers["token"] = localStorage.getItem("token");
return api;
},
2023-03-12 03:56:01 +00:00
"""
responseAdaptor = """
2023-01-31 16:24:56 +00:00
responseAdaptor(api, payload, query, request, response) {
if (response.data.detail == '登录验证失败或已失效,请重新登录') {
window.location.href = '/login'
window.localStorage.clear()
window.sessionStorage.clear()
window.alert('登录验证失败或已失效,请重新登录')
}
return payload
},
2023-03-12 03:56:01 +00:00
"""
icon_path = "https://xtaolabs.com/pagermaid-logo.png"
2023-01-31 16:24:56 +00:00
app: FastAPI = FastAPI()
def init_web():
app.include_router(base_api_router)
app.add_middleware(
CORSMiddleware,
allow_origins=Config.WEB_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
2023-03-12 03:56:01 +00:00
allow_headers=["*"],
)
2023-03-12 03:56:01 +00:00
@app.get("/", response_class=RedirectResponse)
2023-01-31 16:24:56 +00:00
async def index():
2023-03-12 03:56:01 +00:00
return "/admin"
2023-01-31 16:24:56 +00:00
2023-03-12 03:56:01 +00:00
@app.get("/admin", response_class=HTMLResponse)
2023-01-31 16:24:56 +00:00
async def admin():
return admin_app.render(
2023-03-12 03:56:01 +00:00
site_title="PagerMaid-Pyro 后台管理",
2023-01-31 16:24:56 +00:00
site_icon=icon_path,
requestAdaptor=requestAdaptor,
2023-03-12 03:56:01 +00:00
responseAdaptor=responseAdaptor,
2023-01-31 16:24:56 +00:00
)
2023-03-12 03:56:01 +00:00
@app.get("/login", response_class=HTMLResponse)
2023-01-31 16:24:56 +00:00
async def login():
return login_page.render(
2023-03-12 03:56:01 +00:00
site_title="登录 | PagerMaid-Pyro 后台管理",
2023-01-31 16:24:56 +00:00
site_icon=icon_path,
)