Add main.py
This commit is contained in:
commit
5be51bf199
1 file changed
+41
@@ -0,0 +1,41 @@
|
||||
import threading
|
||||
import time
|
||||
import fastapi
|
||||
import requests
|
||||
|
||||
app = fastapi.FastAPI()
|
||||
lanyard_link = "https://api.lanyard.rest/v1/users/1558056824387805300"
|
||||
CACHE_TTL = 1.0
|
||||
_cache = {"data": None, "time": 0.0}
|
||||
_lock = threading.Lock()
|
||||
|
||||
|
||||
|
||||
def get_lanyard():
|
||||
if _cache["data"] is not None and time.monotonic() - _cache["time"] < CACHE_TTL:
|
||||
return _cache["data"]
|
||||
with _lock:
|
||||
if _cache["data"] is not None and time.monotonic() - _cache["time"] < CACHE_TTL:
|
||||
return _cache["data"]
|
||||
try:
|
||||
response = requests.get(lanyard_link, timeout=5)
|
||||
response.raise_for_status()
|
||||
_cache["data"] = response.json()
|
||||
_cache["time"] = time.monotonic()
|
||||
except requests.RequestException:
|
||||
if _cache["data"] is None:
|
||||
raise fastapi.HTTPException(status_code=502, detail="Lanyard indisponible")
|
||||
return _cache["data"]
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"message": "Welcome to IKAVOID API (5005)"}
|
||||
@app.get("/discord")
|
||||
def read_lanyard():
|
||||
return get_lanyard()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=5005)
|
||||
Reference in new issue
Block a user