my-team-docs / app.py
VeryOnly123's picture
Create app.py
b7d19fb verified
Raw History Blame Contribute Delete
990 Bytes
import os
from fastapi import FastAPI, Form
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import PlainTextResponse
import pathlib
app = FastAPI()
# 允许所有来源跨域访问(本地 HTML 调用时需要)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
DATA_DIR = pathlib.Path("/data")
DATA_DIR.mkdir(exist_ok=True)
@app.get("/api/read/{page_name}", response_class=PlainTextResponse)
async def read_page(page_name: str):
"""读取页面纯文本,不存在则返回空字符串"""
file_path = DATA_DIR / f"{page_name}.txt"
if file_path.exists():
return file_path.read_text(encoding="utf-8")
return ""
@app.post("/api/write/{page_name}")
async def write_page(page_name: str, t: str = Form(...)):
"""写入页面内容,表单参数 t"""
file_path = DATA_DIR / f"{page_name}.txt"
file_path.write_text(t, encoding="utf-8")
return {"ok": True}