Skip to content

Commit

Permalink
Add health check endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthb committed Sep 7, 2022
1 parent e33af52 commit e5a3521
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ RUN poetry install --only main

# RUNTIME
FROM docker.io/library/python:${PYTHON_VERSION}-slim as runtime
ENV PATH=/app/.venv/bin:$PATH \
ENV WEB_DIR="/public" \
PATH=/app/.venv/bin:$PATH \
PYTHONPATH=/app:$PYTHONPATH \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
Expand Down
25 changes: 20 additions & 5 deletions rmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,27 @@
<https://www.gnu.org/licenses/>.
"""
from typing import Union
from pathlib import Path, PurePath
from typing import Any

from fastapi import FastAPI
from pydantic import BaseSettings
from starlette_cramjam.middleware import CompressionMiddleware # type: ignore
from starlette_prometheus import metrics, PrometheusMiddleware
from fastapi.staticfiles import StaticFiles
from pydantic import BaseConfig, BaseSettings
from starlette_cramjam.middleware import CompressionMiddleware # type: ignore
from starlette_prometheus import PrometheusMiddleware, metrics


class Settings(BaseSettings):
host: str = "127.0.0.1"
port: int = 8000
web_dir: Path = PurePath("web/public")

class Config(BaseConfig):
@classmethod
def parse_env_var(cls, field_name: str, raw_value: str) -> Any:
if field_name == "web_dir":
return PurePath(raw_value)
return super().parse_env_var(field_name, raw_value)


settings = Settings()
Expand All @@ -43,9 +52,15 @@ class Settings(BaseSettings):

app.add_route("/metrics", metrics)


@app.route("/health")
def health() -> str:
return "ok"


app.mount(
"/",
StaticFiles(directory="web/public", html=True),
StaticFiles(directory=settings.web_dir, html=True),
name="web",
)

Expand Down
1 change: 1 addition & 0 deletions web/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ esbuild.build({
plugins: [
sassPlugin({
precompile(source, pathname) {
// replace url(./relative-path) with url(/absolute-path)
const basedir = path.dirname(pathname)
return source.replace(/(url\(['"]?)(\.\.?\/)([^'")]+['"]?\))/g, `$1${basedir}/$2$3`)
}
Expand Down

0 comments on commit e5a3521

Please sign in to comment.