Spaces:
Sleeping
Sleeping
Download main.py from mohsinramzan/TestingFastApi: direct link, hf CLI and curl.
- Browser
- Download file 977 Bytes
-
https://huggingface.co/spaces/mohsinramzan/TestingFastApi/resolve/main/main.py
- Command line
-
hf download hf://spaces/mohsinramzan/TestingFastApi/main.py
-
curl -L -o main.py https://huggingface.co/spaces/mohsinramzan/TestingFastApi/resolve/main/main.py
977 Bytes
| from fastapi import FastAPI, Request | |
| from pydantic import BaseModel | |
| from typing import Dict | |
| import time | |
| from fastapi.middleware.cors import CORSMiddleware | |
| app = FastAPI() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # or set this to your frontend's origin | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # In-memory storage for now | |
| bus_locations: Dict[str, dict] = {} | |
| class Location(BaseModel): | |
| _type: str | |
| tid: str | |
| lat: float | |
| lon: float | |
| tst: int | |
| async def track_location(loc: Location): | |
| # Save by bus_id (tid) | |
| bus_locations[loc.tid] = { | |
| "lat": loc.lat, | |
| "lon": loc.lon, | |
| "timestamp": loc.tst, | |
| "last_updated": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(loc.tst)) | |
| } | |
| print(f"[{loc.tid}] → ({loc.lat}, {loc.lon}) @ {loc.tst}") | |
| return {"status": "ok"} | |
| def get_all_locations(): | |
| return bus_locations | |