aboutsummaryrefslogtreecommitdiffstats
path: root/thallium-backend/src
diff options
context:
space:
mode:
Diffstat (limited to 'thallium-backend/src')
-rw-r--r--thallium-backend/src/__init__.py22
-rw-r--r--thallium-backend/src/app.py25
-rw-r--r--thallium-backend/src/settings.py9
3 files changed, 56 insertions, 0 deletions
diff --git a/thallium-backend/src/__init__.py b/thallium-backend/src/__init__.py
new file mode 100644
index 0000000..87f0d37
--- /dev/null
+++ b/thallium-backend/src/__init__.py
@@ -0,0 +1,22 @@
+import logging
+
+from src.settings import CONFIG
+
+# Console handler prints to terminal
+console_handler = logging.StreamHandler()
+level = logging.DEBUG if CONFIG.debug else logging.INFO
+console_handler.setLevel(level)
+
+# Remove old loggers, if any
+root = logging.getLogger()
+if root.handlers:
+ for handler in root.handlers:
+ root.removeHandler(handler)
+
+# Setup new logging configuration
+logging.basicConfig(
+ format="%(asctime)s - %(name)s %(levelname)s: %(message)s",
+ datefmt="%D %H:%M:%S",
+ level=logging.DEBUG if CONFIG.debug else logging.INFO,
+ handlers=[console_handler],
+)
diff --git a/thallium-backend/src/app.py b/thallium-backend/src/app.py
new file mode 100644
index 0000000..27ec8ab
--- /dev/null
+++ b/thallium-backend/src/app.py
@@ -0,0 +1,25 @@
+import logging
+
+from fastapi import FastAPI, Request
+from fastapi.exceptions import RequestValidationError
+from fastapi.responses import JSONResponse
+
+from src.settings import CONFIG
+
+log = logging.getLogger(__name__)
+
+
+fastapi_app = FastAPI(debug=CONFIG.debug)
+
+
+@fastapi_app.get("/heartbeat")
+def health_check() -> JSONResponse:
+ """Return basic response, for use as a health check."""
+ return JSONResponse({"detail": "I am alive!"})
+
+
+@fastapi_app.exception_handler(RequestValidationError)
+def pydantic_validation_error(request: Request, error: RequestValidationError) -> JSONResponse:
+ """Raise a warning for pydantic validation errors, before returning."""
+ log.warning("Error from %s: %s", request.url, error)
+ return JSONResponse({"error": str(error)}, status_code=422)
diff --git a/thallium-backend/src/settings.py b/thallium-backend/src/settings.py
new file mode 100644
index 0000000..2f9f6c1
--- /dev/null
+++ b/thallium-backend/src/settings.py
@@ -0,0 +1,9 @@
+from pydantic_settings import BaseSettings
+
+
+class _CONFIG(BaseSettings, env_file=".env", env_file_encoding="utf-8"):
+ debug: bool = False
+ git_sha: str = "development"
+
+
+CONFIG = _CONFIG()