45 lines
1.0 KiB
Python
45 lines
1.0 KiB
Python
|
|
from pydantic import BaseModel
|
|
import random
|
|
from fastapi.responses import JSONResponse, FileResponse # 导入FileResponse
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware # 导入CORS中间件
|
|
from fastapi.responses import FileResponse # 用于返回文件响应
|
|
import uvicorn
|
|
|
|
app = FastAPI()
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
class Item(BaseModel):
|
|
x: float
|
|
y: float
|
|
z: float
|
|
|
|
@app.post("/data")
|
|
async def post_data(item: Item):
|
|
# 这里可以对item进行任何需要的处理
|
|
return item
|
|
|
|
@app.get("/")
|
|
async def get_index():
|
|
# 直接返回同目录下的index.html文件
|
|
return FileResponse('index.html')
|
|
|
|
@app.get("/2")
|
|
async def get_index():
|
|
# 直接返回同目录下的index.html文件
|
|
return FileResponse('index2.html')
|
|
def main():
|
|
uvicorn.run(app, host="127.0.0.1", port=8000, log_level="info")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|