File size: 3,354 Bytes
a2075f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c6fecc
 
 
 
a2075f6
 
 
 
 
9c6fecc
 
 
 
 
 
 
a2075f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
main.py
FastAPI app สำหรับ Hugging Face Space (Docker)
- ดึงข้อมูล e-GP ของ SUT ครั้งเดียวตอน startup แล้ว cache ไว้ใน memory
- เปิด endpoint ให้ frontend (Lovable) เรียกไปแสดงผล
- มี endpoint /refresh สำหรับสั่งดึงข้อมูลใหม่แบบ manual (ไม่ auto-refresh)
"""

from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from typing import Optional
import logging

from scraper import scrape_all

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("sut-tender-api")

# cache เก็บข้อมูลไว้ใน memory ของ process
CACHE: dict = {"data": [], "last_updated": None, "error": None}


@asynccontextmanager
async def lifespan(app: FastAPI):
    # ดึงข้อมูลครั้งเดียวตอน startup ตามที่ตกลงกันไว้
    refresh_cache()
    yield


app = FastAPI(title="SUT Tender API", lifespan=lifespan)

# เปิด CORS ให้ Lovable (หรือ origin ใดๆ) เรียกได้
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)


def refresh_cache():
    import datetime
    try:
        logger.info("Scraping SUT e-GP data...")
        data = scrape_all()
        CACHE["data"] = data
        CACHE["last_updated"] = datetime.datetime.utcnow().isoformat()
        CACHE["error"] = None
        logger.info(f"Done. Got {len(data)} items.")
    except Exception as e:
        logger.exception("Scrape failed")
        CACHE["error"] = str(e)


@app.get("/")
def root():
    return {
        "status": "ok",
        "items_cached": len(CACHE["data"]),
        "last_updated": CACHE["last_updated"],
        "error": CACHE["error"],
    }


@app.get("/api/tenders")
def get_tenders(
    keyword: Optional[str] = Query(
        None,
        description="กรองคำในชื่อโครงการ รองรับหลายคำคั่นด้วย comma เช่น 'server,คอมพิวเตอร์,computer' (เจอคำใดคำหนึ่งก็ติด)",
    ),
    limit: int = Query(50, ge=1, le=500),
    offset: int = Query(0, ge=0),
):
    items = CACHE["data"]
    if keyword:
        # แตกคำด้วย comma แล้ว trim whitespace, ตัดคำว่างทิ้ง
        keywords = [kw.strip().lower() for kw in keyword.split(",") if kw.strip()]
        if keywords:
            items = [
                i for i in items
                if any(kw in i["title"].lower() for kw in keywords)
            ]
    total = len(items)
    items = items[offset: offset + limit]
    return {
        "total": total,
        "limit": limit,
        "offset": offset,
        "last_updated": CACHE["last_updated"],
        "items": items,
    }


@app.post("/refresh")
def refresh():
    """สั่งดึงข้อมูลใหม่แบบ manual (ไม่ auto-refresh ตามที่ตกลงไว้)"""
    refresh_cache()
    return {"status": "refreshed", "items_cached": len(CACHE["data"]), "error": CACHE["error"]}