Spaces:
Running
Running
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| import json | |
| import os | |
| app = FastAPI() | |
| # --- คงเดิม: เพิ่ม CORS เพื่อให้หน้าเว็บเข้าถึง API ได้ --- | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # --- แก้ไขจุดนี้: ย้ายไปเก็บที่ /tmp ซึ่งเป็นที่ที่เขียนได้เสมอ --- | |
| DB_FILE = "/tmp/update_data.json" | |
| class UpdateData(BaseModel): | |
| version: str | |
| description: str | |
| script: str | |
| async def get_update(): | |
| if os.path.exists(DB_FILE): | |
| with open(DB_FILE, "r") as f: | |
| return json.load(f) | |
| # ถ้าไม่มีไฟล์ ให้คืนค่า Default | |
| return {"version": "1.0", "description": "ระบบเริ่มต้น", "script": ""} | |
| async def post_update(data: UpdateData): | |
| # เขียนไฟล์ลง /tmp/ แทน | |
| with open(DB_FILE, "w") as f: | |
| json.dump(data.model_dump(), f) # ใช้ .model_dump() แทน .dict() สำหรับ Pydantic v2 | |
| return {"status": "success"} | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |