Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,54 +1,41 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi import FastAPI
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
app = FastAPI(
|
| 9 |
|
| 10 |
-
#
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
-
allow_origins=["*"],
|
| 14 |
-
allow_credentials=True,
|
| 15 |
allow_methods=["*"],
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
class UpdateData(BaseModel):
|
| 22 |
version: str
|
| 23 |
description: str
|
| 24 |
script: str
|
| 25 |
|
| 26 |
-
@app.get("/")
|
| 27 |
-
def read_root():
|
| 28 |
-
return {"message": "TANZOS Update API is running on Hugging Face!"}
|
| 29 |
-
|
| 30 |
-
# ดึงข้อมูลอัพเดทล่าสุด
|
| 31 |
@app.get("/api/update")
|
| 32 |
-
def get_update():
|
| 33 |
-
if os.path.exists(
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
except Exception as e:
|
| 38 |
-
return {"error": str(e)}
|
| 39 |
-
# ถ้ายังไม่มีไฟล์ ให้ส่งค่า default กลับไป
|
| 40 |
-
return {"version": "1.0", "description": "No update available yet.", "script": ""}
|
| 41 |
|
| 42 |
-
# บันทึกข้อมูลอัพเดทจากหน้าแอดมิน (/a1234)
|
| 43 |
@app.post("/api/update")
|
| 44 |
-
def post_update(data: UpdateData):
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# หากใช้งานบน Hugging Face Docker Space จะถูกรันอัตโนมัติผ่านคำสั่งใน Dockerfile
|
| 54 |
-
# เช่น CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
app = FastAPI()
|
| 8 |
|
| 9 |
+
# --- แก้ไขจุดนี้: เพิ่ม CORS เพื่อให้หน้าเว็บเข้าถึง API ได้ ---
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
|
|
|
| 13 |
allow_methods=["*"],
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# ไฟล์เก็บข้อมูลอัพเดท
|
| 18 |
+
DB_FILE = "update_data.json"
|
| 19 |
|
| 20 |
class UpdateData(BaseModel):
|
| 21 |
version: str
|
| 22 |
description: str
|
| 23 |
script: str
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
@app.get("/api/update")
|
| 26 |
+
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.dict(), f)
|
| 36 |
+
return {"status": "success"}
|
| 37 |
+
|
| 38 |
+
# ตรวจสอบว่าไฟล์รันได้ปกติ
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
import uvicorn
|
| 41 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|