| import os |
| import uvicorn |
| import aiofiles |
| from fastapi import FastAPI, HTTPException, File, UploadFile |
| from fastapi.middleware.cors import CORSMiddleware |
| from fastapi.responses import JSONResponse |
|
|
| from typing import List |
|
|
| import service |
|
|
| uploadPath = "./data/latents/" |
|
|
|
|
| app = FastAPI() |
| |
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["http://localhost:5173"], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| |
| @app.get("/") |
| async def read_root(): |
| return {"message": "Hello, World!"} |
|
|
| @app.get("/dataList/{datatype}") |
| async def read_data(datatype): |
| return service.getALlData() |
| |
|
|
|
|
| |
|
|
| |
| @app.get("/details/{name}") |
| async def read_item(name: str = None): |
| data = service.getListByName("integration_accuracy.csv", name) |
| return data |
|
|
| |
| @app.post("/upload") |
| async def upload_files(files:List[UploadFile] = File(...)): |
| for file in files: |
| save_path = os.path.join(uploadPath, file.filename) |
| try: |
| async with aiofiles.open(save_path, 'wb') as out_file: |
| content = await file.read() |
| await out_file.write(content) |
| except Exception as e: |
| return {"message": f"出现错误: {e}"} |
| finally: |
| |
| await file.close() |
| return {"success":True, "message": f"文件 {file.filename} 已成功保存"} |
|
|
| if __name__ == "__main__": |
| |
| port = int(os.getenv("APP_PORT", 8000)) |
| uvicorn.run(app, host="127.0.0.1", port=port) |