50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
from fastapi import FastAPI, Depends
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.api.api_v1.api import api_router
|
|
from app.core.config import settings
|
|
from app.db.session import engine, SessionLocal
|
|
from app.db.base import Base
|
|
from app.services.init_db import init_db
|
|
|
|
# Create database tables
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
# Initialize database with default user
|
|
db = SessionLocal()
|
|
try:
|
|
init_db(db)
|
|
finally:
|
|
db.close()
|
|
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
openapi_url=f"{settings.API_PREFIX}/openapi.json"
|
|
)
|
|
|
|
# Set up CORS middleware
|
|
if settings.BACKEND_CORS_ORIGINS:
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include API router
|
|
app.include_router(api_router, prefix=settings.API_PREFIX)
|
|
|
|
@app.get("/")
|
|
def root():
|
|
return {"message": "Welcome to SolarBank IoT Dashboard API"}
|
|
|
|
@app.get("/health")
|
|
def health_check():
|
|
"""Health check endpoint for monitoring and load balancers"""
|
|
return {"status": "healthy", "service": "solarbank-backend"}
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|