代码拉取完成,页面将自动刷新
import logging as log
import os
import pkg_resources
import uvicorn
from fastapi import FastAPI, Form
from fastapi import Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from starlette.responses import RedirectResponse
from query import qry_mdx_def, qry_ai_def
from staticfiles import CachedStaticFiles
app = FastAPI()
templates = Jinja2Templates(directory="resources/templates")
app.mount("/static", CachedStaticFiles(directory=pkg_resources.resource_filename(__name__, 'resources/static')),
name="static")
@app.get("/")
def index(request: Request):
return templates.TemplateResponse("index.html", context={"request": request})
@app.get("/{static_path}")
def static(static_path: str, request: Request):
folder = pkg_resources.resource_filename(__name__, 'resources/static')
files = os.listdir(folder)
if static_path not in files:
RedirectResponse(url='/static/index.css')
return RedirectResponse(url=f'/static/{static_path}')
@app.post("/query", response_class=HTMLResponse)
def query(word: str = Form(...)):
print(f"mdx query for={word}")
return qry_mdx_def(word)
@app.post("/ai", response_class=HTMLResponse)
def ai_query(word: str = Form(...)):
return qry_ai_def(word)
if __name__ == '__main__':
"""
阅读readme和config.ini
"""
# logger = log.getLogger(__name__)
# logger.setLevel(log.INFO)
# do not use 127.0.0.1! and request with localhost:8080
print("mdx server running at http://localhost:8080")
uvicorn.run("app:app", host="0.0.0.0", port=8080, log_level=log.WARNING)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。