1234ty commited on
Commit
8650ce4
·
verified ·
1 Parent(s): 759caac

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -33
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(title="TANZOS Update API")
9
 
10
- # อนญาตให้เว็บจากภายนอกเข้าถึง API นี้ได้ (CORS)
11
  app.add_middleware(
12
  CORSMiddleware,
13
- allow_origins=["*"], # เปิดรับทุกโดเมน (หรือใส่เฉพาะโดเมนของเว็บคุณเพื่อความปลอดภัย)
14
- allow_credentials=True,
15
  allow_methods=["*"],
16
  allow_headers=["*"],
17
  )
18
 
19
- DATA_FILE = "tanzos_update.json"
 
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(DATA_FILE):
34
- try:
35
- with open(DATA_FILE, "r", encoding="utf-8") as f:
36
- return json.load(f)
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
- update_dict = data.dict()
46
- try:
47
- with open(DATA_FILE, "w", encoding="utf-8") as f:
48
- json.dump(update_dict, f, ensure_ascii=False, indent=4)
49
- return {"status": "success", "message": "TANZOS Update published successfully!"}
50
- except Exception as e:
51
- return {"status": "error", "message": str(e)}
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)