feat: 重构
This commit is contained in:
@@ -1,14 +1,36 @@
|
||||
import json
|
||||
import os
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
from peewee import SqliteDatabase, Model, CharField, TextField
|
||||
|
||||
from fastapi import FastAPI
|
||||
db_path = os.path.join(os.path.dirname(__file__), "data.db")
|
||||
db = SqliteDatabase(db_path)
|
||||
|
||||
from modules import example
|
||||
from modules import voxy_import
|
||||
|
||||
app = FastAPI(title="ARinera Minecraft TUI Backend")
|
||||
class BaseModel(Model):
|
||||
class Meta:
|
||||
database = db
|
||||
|
||||
app.include_router(example.router, prefix="/api/v1")
|
||||
app.include_router(voxy_import.router, prefix="/api/v1")
|
||||
|
||||
class Tool(BaseModel):
|
||||
code = CharField(max_length=4, unique=True)
|
||||
step = TextField()
|
||||
desp = TextField(default="")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
db.connect()
|
||||
db.create_tables([Tool])
|
||||
yield
|
||||
if not db.is_closed():
|
||||
db.close()
|
||||
|
||||
|
||||
app = FastAPI(title="ARinera Minecraft TUI Backend", lifespan=lifespan)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
@@ -16,6 +38,16 @@ async def health():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get("/tools")
|
||||
async def get_tools(
|
||||
code: str = Query(..., min_length=4, max_length=4, pattern=r"^\d{4}$"),
|
||||
):
|
||||
tool = Tool.get_or_none(Tool.code == code)
|
||||
if tool is None:
|
||||
raise HTTPException(status_code=404, detail=f"code {code} not found")
|
||||
return json.loads(tool.step)
|
||||
|
||||
|
||||
def main():
|
||||
uvicorn.run("main:app", reload=True, port=3131)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user