| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- #!/usr/bin/env python3
- from __future__ import annotations
- import time
- from pathlib import Path
- from flask import Flask, Response, render_template
- _START_TIME = time.time()
- def create_app(app_name: str, *, title: str | None = None) -> Flask:
- """Create a Flask app with shared web defaults for this monorepo."""
- template_dir = Path(__file__).with_name("templates")
- app = Flask(app_name, template_folder=str(template_dir))
- app.config["APP_TITLE"] = title or app_name
- register_default_routes(app)
- return app
- def register_default_routes(app: Flask) -> None:
- @app.get("/")
- def index() -> str:
- return render_template("index.html", app_title=app.config["APP_TITLE"])
- @app.get("/healthz")
- def healthz() -> Response:
- uptime_seconds = max(0.0, time.time() - _START_TIME)
- lines = [
- "# HELP app_health Application health status (1 = healthy).",
- "# TYPE app_health gauge",
- f'app_health{{service="{app.name}"}} 1',
- "# HELP app_uptime_seconds Application uptime in seconds.",
- "# TYPE app_uptime_seconds gauge",
- f'app_uptime_seconds{{service="{app.name}"}} {uptime_seconds:.3f}',
- "",
- ]
- body = "\n".join(lines)
- return Response(body, status=200, headers={"Content-Type": "text/plain; version=0.0.4; charset=utf-8"})
|