| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- from fastapi import FastAPI, HTTPException
- import uvicorn
- from api_config import ApiConfig
- from cargo_service import CargoHeightService
- from request_logging import setup_request_logging
- config = ApiConfig.from_env()
- service = CargoHeightService(config)
- app = FastAPI(title="Cargo Height API")
- setup_request_logging(
- app,
- max_response_len=config.request_log_max_len,
- max_bytes=config.request_log_max_bytes,
- backup_count=config.request_log_backup_count,
- )
- @app.on_event("startup")
- def on_startup() -> None:
- service.startup()
- @app.on_event("shutdown")
- def on_shutdown() -> None:
- service.shutdown()
- @app.get("/height")
- def get_height():
- result = service.measure_height()
- if result is None:
- raise HTTPException(status_code=503, detail="Insufficient valid samples from depth camera")
- return result
- @app.get("/health")
- def health():
- return {"status": "ok"}
- def main() -> None:
- uvicorn.run("api:app", host=config.api_host, port=config.api_port, log_level="info")
- if __name__ == "__main__":
- main()
|