Spaces:
Sleeping
Sleeping
| import firebase_admin | |
| from firebase_admin import credentials, db | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.staticfiles import StaticFiles | |
| # Path to your Firebase service account key file | |
| cred = credentials.Certificate("animvitals-firebase-adminsdk-sgfas-3c8e7f596c.json") | |
| # Initialize the app with a service account, granting admin privileges | |
| firebase_admin.initialize_app(cred, { | |
| 'databaseURL': 'https://animvitals-default-rtdb.firebaseio.com' | |
| }) | |
| app = FastAPI() | |
| # Allow CORS so your frontend can access this API | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Allow all origins | |
| allow_credentials=True, | |
| allow_methods=["*"], # Allow all methods | |
| allow_headers=["*"], # Allow all headers | |
| ) | |
| app.mount("/", StaticFiles(directory="static", html=True), name="static") | |
| async def get_latest_data(): | |
| # Reference to the 'animal_data' node | |
| ref = db.reference('animal_data') | |
| # Get the latest data | |
| data = ref.order_by_key().limit_to_last(1).get() | |
| # Since data is returned as a dictionary, get the first item (latest entry) | |
| if data: | |
| key = next(iter(data)) | |
| return data[key] | |
| else: | |
| return {"error": "No data found"} | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) | |