dockerfiles added
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
"""UI main entry point."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
@@ -12,11 +13,30 @@ from apps.ui.api_client import fetch_devices, fetch_layout
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Initialize FastAPI app
|
||||
# Read configuration from environment variables
|
||||
API_BASE = os.getenv("API_BASE", "http://localhost:8001")
|
||||
BASE_PATH = os.getenv("BASE_PATH", "") # e.g., "/ui" for reverse proxy
|
||||
|
||||
print(f"UI using API_BASE: {API_BASE}")
|
||||
print(f"UI using BASE_PATH: {BASE_PATH}")
|
||||
|
||||
def api_url(path: str) -> str:
|
||||
"""Helper function to construct API URLs.
|
||||
|
||||
Args:
|
||||
path: API path (e.g., "/devices")
|
||||
|
||||
Returns:
|
||||
Full API URL
|
||||
"""
|
||||
return f"{API_BASE}{path}"
|
||||
|
||||
# Initialize FastAPI app with optional root_path for reverse proxy
|
||||
app = FastAPI(
|
||||
title="Home Automation UI",
|
||||
description="User interface for home automation system",
|
||||
version="0.1.0"
|
||||
version="0.1.0",
|
||||
root_path=BASE_PATH
|
||||
)
|
||||
|
||||
# Setup Jinja2 templates
|
||||
@@ -29,6 +49,21 @@ static_dir.mkdir(exist_ok=True)
|
||||
app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health() -> JSONResponse:
|
||||
"""Health check endpoint for Kubernetes/Docker.
|
||||
|
||||
Returns:
|
||||
JSONResponse: Health status
|
||||
"""
|
||||
return JSONResponse({
|
||||
"status": "ok",
|
||||
"service": "ui",
|
||||
"api_base": API_BASE,
|
||||
"base_path": BASE_PATH
|
||||
})
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def index(request: Request) -> HTMLResponse:
|
||||
"""Redirect to dashboard.
|
||||
@@ -53,11 +88,11 @@ async def dashboard(request: Request) -> HTMLResponse:
|
||||
HTMLResponse: Rendered dashboard template
|
||||
"""
|
||||
try:
|
||||
# Load layout from API
|
||||
layout_data = fetch_layout()
|
||||
# Load layout from API (use configured API_BASE)
|
||||
layout_data = fetch_layout(API_BASE)
|
||||
|
||||
# Fetch devices from API (now includes features)
|
||||
api_devices = fetch_devices()
|
||||
api_devices = fetch_devices(API_BASE)
|
||||
|
||||
# Create device lookup by device_id
|
||||
device_map = {d["device_id"]: d for d in api_devices}
|
||||
@@ -98,7 +133,8 @@ async def dashboard(request: Request) -> HTMLResponse:
|
||||
|
||||
return templates.TemplateResponse("dashboard.html", {
|
||||
"request": request,
|
||||
"rooms": rooms
|
||||
"rooms": rooms,
|
||||
"api_base": API_BASE # Pass API_BASE to template
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
@@ -106,7 +142,8 @@ async def dashboard(request: Request) -> HTMLResponse:
|
||||
# Fallback to empty dashboard
|
||||
return templates.TemplateResponse("dashboard.html", {
|
||||
"request": request,
|
||||
"rooms": []
|
||||
"rooms": [],
|
||||
"api_base": API_BASE # Pass API_BASE even on error
|
||||
})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user