calc_app.py 893 B

12345678910111213141516171819202122232425
  1. import os
  2. from pathlib import Path
  3. from fastapi import FastAPI
  4. from fastapi.middleware.cors import CORSMiddleware
  5. from fastapi.responses import HTMLResponse
  6. from room_cal._utils import services as _services, service_name
  7. from room_cal.api._routes.calc import router as _calc_router
  8. _CALC_HTML = Path(__file__).parents[1] / 'vue' / 'calc.html'
  9. _API_BASE = os.getenv('APP_BASE_URL', '').rstrip('/')
  10. app = FastAPI(title='room_cal calc')
  11. app.add_middleware(CORSMiddleware, allow_origins=['*'], allow_methods=['*'], allow_headers=['*'])
  12. app.include_router(_calc_router)
  13. @app.get('/services')
  14. async def get_services():
  15. return [{'key': s, 'name': service_name(s)} for s in _services()]
  16. @app.get('/', response_class=HTMLResponse, include_in_schema=False)
  17. async def calc_ui():
  18. html = _CALC_HTML.read_text()
  19. return html.replace("const API_BASE = '';", f"const API_BASE = '{_API_BASE}';", 1)