Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -6,7 +6,7 @@ import os
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
# ---
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
allow_origins=["*"],
|
|
@@ -14,8 +14,8 @@ app.add_middleware(
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# ไ
|
| 18 |
-
DB_FILE = "update_data.json"
|
| 19 |
|
| 20 |
class UpdateData(BaseModel):
|
| 21 |
version: str
|
|
@@ -27,15 +27,16 @@ async def get_update():
|
|
| 27 |
if os.path.exists(DB_FILE):
|
| 28 |
with open(DB_FILE, "r") as f:
|
| 29 |
return json.load(f)
|
|
|
|
| 30 |
return {"version": "1.0", "description": "ระบบเริ่มต้น", "script": ""}
|
| 31 |
|
| 32 |
@app.post("/api/update")
|
| 33 |
async def post_update(data: UpdateData):
|
|
|
|
| 34 |
with open(DB_FILE, "w") as f:
|
| 35 |
-
json.dump(data.
|
| 36 |
return {"status": "success"}
|
| 37 |
|
| 38 |
-
# ตรวจสอบว่าไฟล์รันได้ปกติ
|
| 39 |
if __name__ == "__main__":
|
| 40 |
import uvicorn
|
| 41 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# --- คงเดิม: เพิ่ม CORS เพื่อให้หน้าเว็บเข้าถึง API ได้ ---
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
allow_origins=["*"],
|
|
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# --- แก้ไขจุดนี้: ย้ายไปเก็บที่ /tmp ซึ่งเป็นที่ที่เขียนได้เสมอ ---
|
| 18 |
+
DB_FILE = "/tmp/update_data.json"
|
| 19 |
|
| 20 |
class UpdateData(BaseModel):
|
| 21 |
version: str
|
|
|
|
| 27 |
if os.path.exists(DB_FILE):
|
| 28 |
with open(DB_FILE, "r") as f:
|
| 29 |
return json.load(f)
|
| 30 |
+
# ถ้าไม่มีไฟล์ ให้คืนค่า Default
|
| 31 |
return {"version": "1.0", "description": "ระบบเริ่มต้น", "script": ""}
|
| 32 |
|
| 33 |
@app.post("/api/update")
|
| 34 |
async def post_update(data: UpdateData):
|
| 35 |
+
# เขียนไฟล์ลง /tmp/ แทน
|
| 36 |
with open(DB_FILE, "w") as f:
|
| 37 |
+
json.dump(data.model_dump(), f) # ใช้ .model_dump() แทน .dict() สำหรับ Pydantic v2
|
| 38 |
return {"status": "success"}
|
| 39 |
|
|
|
|
| 40 |
if __name__ == "__main__":
|
| 41 |
import uvicorn
|
| 42 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|