-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (50 loc) · 2.42 KB
/
main.py
File metadata and controls
56 lines (50 loc) · 2.42 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
import os
import sys
from dotenv import load_dotenv
load_dotenv()
import logging
from fastapi import FastAPI
from contextlib import asynccontextmanager
from app.routers import attendance, users, actions, stats, push_api, data, commands, devices
from app.utils.network_scan import scanner
# ---------------------------------------------------------------------
# Logging setup
# ---------------------------------------------------------------------
logger = logging.getLogger(__name__)
handler = logging.StreamHandler(sys.stdout)
handler.flush = sys.stdout.flush
logging.basicConfig(level=logging.INFO, handlers=[handler])
# ---------------------------------------------------------------------
# Lifespan handler
# ---------------------------------------------------------------------
@asynccontextmanager
async def lifespan(app: FastAPI):
if os.getenv("DISCOVER_DEVICES_ON_STARTUP", "true").lower() == "true":
logger.info("📡 Scanning for ZKTeco devices on startup...")
await scanner.discover_devices()
logger.info(f"✅ Discovered {len(scanner.list_devices())} device(s): {scanner.list_ips()}")
yield
logger.info("🛑 Shutting down eSSL Attendance Logger...")
# ---------------------------------------------------------------------
# FastAPI app setup
# ---------------------------------------------------------------------
app = FastAPI(title="eSSL Attendance Logger", version="1.0.0", lifespan=lifespan)
# Register routers
app.include_router(attendance.router, prefix="/attendance", tags=["Attendance"])
app.include_router(users.router, prefix="/users", tags=["Users"])
app.include_router(data.router, prefix="/data", tags=["Data"])
app.include_router(devices.router, prefix="/devices", tags=["Devices"])
app.include_router(actions.router, prefix="/devices/{device_sn}/actions:", tags=["Device/Actions"])
app.include_router(stats.router, prefix="/devices/{device_sn}/stats", tags=["Device/Stats"])
app.include_router(commands.router, prefix="/devices/{device_sn}/commands", tags=["Device/Commands"])
app.include_router(push_api.router, prefix="/{token}/iclock", tags=["ADMS/Push API"])
@app.get("/health", tags=["Health"])
async def health_check():
"""Health check endpoint for deployment monitoring."""
return {
"status": "healthy",
"version": "1.0.0",
"data_dir": os.path.exists("data"),
"port": os.getenv("PORT", "8000"),
"devices_detected": len(scanner.list_devices())
}