-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
170 lines (144 loc) · 5.35 KB
/
main.py
File metadata and controls
170 lines (144 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
"""
Unified Device API Entry Point
Automatically detects platform and loads appropriate routers and services.
"""
import logging
from contextlib import asynccontextmanager
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from src.oaDeviceAPI.core.caching import setup_cache
from src.oaDeviceAPI.core.config import APP_VERSION, app_config
# Configure logging
from src.oaDeviceAPI.core.logging import RequestTrackingMiddleware, setup_logging
from src.oaDeviceAPI.core.platform import platform_manager
from src.oaDeviceAPI.middleware import TailscaleSubnetMiddleware
logging_manager = setup_logging(app_config)
cache_manager = setup_cache(app_config)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan manager."""
logger.info(
f"Starting oaDeviceAPI v{APP_VERSION}",
extra={
"platform": platform_manager.platform,
"features": platform_manager.get_available_features(),
"event_type": "startup"
}
)
# Warm cache if enabled
if cache_manager and app_config.cache.enable_caching:
logger.info("Cache warming started")
# Add cache warming logic here if needed
yield
logger.info("Shutting down oaDeviceAPI", extra={"event_type": "shutdown"})
# Create FastAPI application
app = FastAPI(
title="OrangeAd Device API",
description="Unified API for device management across macOS and OrangePi platforms",
version=APP_VERSION,
lifespan=lifespan
)
# Configure middleware
app.add_middleware(RequestTrackingMiddleware)
# Add CORS middleware if enabled
if app_config.security.enable_cors:
app.add_middleware(
CORSMiddleware,
allow_origins=app_config.security.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add Tailscale subnet restriction if enabled
if app_config.security.enable_tailscale_restriction:
app.add_middleware(
TailscaleSubnetMiddleware,
tailscale_subnet_str=app_config.network.tailscale_subnet
)
# Platform-specific router loading
if platform_manager.is_macos():
from src.oaDeviceAPI.platforms.macos.router import router as macos_router
app.include_router(macos_router)
logger.info("Loaded macOS platform routers")
elif platform_manager.is_orangepi():
from src.oaDeviceAPI.platforms.orangepi.router import router as orangepi_router
app.include_router(orangepi_router)
logger.info("Loaded OrangePi platform routers")
else:
# Fallback for generic Linux
from src.oaDeviceAPI.platforms.orangepi.router import router as linux_router
app.include_router(linux_router)
logger.info("Loaded generic Linux platform routers")
# Root endpoint
@app.get("/")
async def root():
"""Root endpoint with API information."""
return {
"name": "oaDeviceAPI",
"version": APP_VERSION,
"platform": platform_manager.platform,
"features": platform_manager.get_available_features(),
"endpoints": {
"health": "/health",
"platform": "/", # Root endpoint provides platform info
"system": "/system",
"docs": "/docs"
}
}
# Generic health endpoint for deployment validation
@app.get("/health")
async def health():
"""Generic health check endpoint that works across all platforms."""
from datetime import datetime
return {
"status": "healthy",
"platform": platform_manager.platform,
"version": APP_VERSION,
"timestamp": datetime.utcnow().isoformat() + "Z",
"detailed_health": f"/{platform_manager.platform}/health"
}
def validate_startup_environment():
"""Validate runtime environment and dependencies before starting service."""
import sys
import os
from pathlib import Path
# Check Python version
if sys.version_info < (3, 12):
logger.error(f"Python 3.12+ required, got {sys.version_info}")
return False
# Check working directory has required files
required_files = ["pyproject.toml", "src/oaDeviceAPI/__init__.py"]
for file_path in required_files:
if not Path(file_path).exists():
logger.error(f"Required file missing: {file_path}")
logger.info(f"Current working directory: {os.getcwd()}")
logger.info(f"Directory contents: {list(Path('.').iterdir())}")
return False
# Log environment info
logger.info(f"oaDeviceAPI v{APP_VERSION} starting")
logger.info(f"Python: {sys.version}")
logger.info(f"Platform: {platform_manager.platform}")
logger.info(f"Working directory: {os.getcwd()}")
logger.info(f"Host: {app_config.network.host}:{app_config.network.port}")
return True
if __name__ == "__main__":
# Validate environment before starting
if not validate_startup_environment():
logger.error("Startup validation failed - exiting")
exit(1)
# Configure uvicorn server
try:
uvicorn.run(
"main:app",
host=app_config.network.host,
port=app_config.network.port,
log_level=app_config.logging.level.value.lower(),
reload=app_config.is_development(),
access_log=True
)
except Exception as e:
logger.error(f"Failed to start server: {e}")
exit(1)