mirror of
https://github.com/furyhawk/lta_datamall_api.git
synced 2026-07-21 02:06:50 +00:00
Make backend port configurable via .env and update related configurations
This commit is contained in:
@@ -3,6 +3,7 @@ APP_ENV=development
|
|||||||
APP_DEBUG=false
|
APP_DEBUG=false
|
||||||
APP_NAME=LTA DataMall Bus Backend
|
APP_NAME=LTA DataMall Bus Backend
|
||||||
APP_VERSION=0.1.0
|
APP_VERSION=0.1.0
|
||||||
|
APP_PORT=8000
|
||||||
DATAMALL_BASE_URL=https://datamall2.mytransport.sg/ltaodataservice
|
DATAMALL_BASE_URL=https://datamall2.mytransport.sg/ltaodataservice
|
||||||
REQUEST_TIMEOUT_SECONDS=10
|
REQUEST_TIMEOUT_SECONDS=10
|
||||||
MAX_CONNECTIONS=200
|
MAX_CONNECTIONS=200
|
||||||
|
|||||||
+1
-1
@@ -19,4 +19,4 @@ COPY openapi.json ./openapi.json
|
|||||||
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
CMD ["uv", "run", "gunicorn", "app.main:app", "-k", "uvicorn.workers.UvicornWorker", "-w", "4", "-b", "0.0.0.0:8000", "--access-logfile", "-", "--error-logfile", "-"]
|
CMD ["sh", "-c", "uv run gunicorn app.main:app -k uvicorn.workers.UvicornWorker -w 4 -b 0.0.0.0:${APP_PORT:-8000} --access-logfile - --error-logfile -"]
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
APP_MODULE=app.main:app
|
APP_MODULE=app.main:app
|
||||||
HOST=0.0.0.0
|
HOST=0.0.0.0
|
||||||
PORT=8000
|
|
||||||
|
-include .env
|
||||||
|
|
||||||
|
APP_PORT ?= 8000
|
||||||
|
|
||||||
PYTHON_RUN=uv run
|
PYTHON_RUN=uv run
|
||||||
|
|
||||||
@@ -51,12 +54,12 @@ sync:
|
|||||||
uv sync
|
uv sync
|
||||||
|
|
||||||
run:
|
run:
|
||||||
$(PYTHON_RUN) uvicorn $(APP_MODULE) --host $(HOST) --port $(PORT) --reload
|
$(PYTHON_RUN) uvicorn $(APP_MODULE) --host $(HOST) --port $(APP_PORT) --reload
|
||||||
|
|
||||||
dev: run
|
dev: run
|
||||||
|
|
||||||
prod:
|
prod:
|
||||||
$(PYTHON_RUN) gunicorn $(APP_MODULE) -k uvicorn.workers.UvicornWorker -w 4 -b $(HOST):$(PORT) --access-logfile - --error-logfile -
|
$(PYTHON_RUN) gunicorn $(APP_MODULE) -k uvicorn.workers.UvicornWorker -w 4 -b $(HOST):$(APP_PORT) --access-logfile - --error-logfile -
|
||||||
|
|
||||||
compile:
|
compile:
|
||||||
$(PYTHON_RUN) python -m compileall app
|
$(PYTHON_RUN) python -m compileall app
|
||||||
@@ -70,7 +73,7 @@ image-build: check-engine
|
|||||||
$(CONTAINER_ENGINE) build -t lta-datamall-bus-backend:latest .
|
$(CONTAINER_ENGINE) build -t lta-datamall-bus-backend:latest .
|
||||||
|
|
||||||
image-run: check-engine
|
image-run: check-engine
|
||||||
$(CONTAINER_ENGINE) run -d --name lta-datamall-bus-backend --env-file .env -p 8000:8000 lta-datamall-bus-backend:latest
|
$(CONTAINER_ENGINE) run -d --name lta-datamall-bus-backend --env-file .env -p $(APP_PORT):$(APP_PORT) lta-datamall-bus-backend:latest
|
||||||
|
|
||||||
image-stop: check-engine
|
image-stop: check-engine
|
||||||
-$(CONTAINER_ENGINE) stop lta-datamall-bus-backend
|
-$(CONTAINER_ENGINE) stop lta-datamall-bus-backend
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ FastAPI backend platform for LTA DataMall Bus Transport APIs, dockerized for pro
|
|||||||
- Valkey caching layer with graceful fallback when cache is unavailable
|
- Valkey caching layer with graceful fallback when cache is unavailable
|
||||||
- Scalable runtime with Gunicorn + Uvicorn workers
|
- Scalable runtime with Gunicorn + Uvicorn workers
|
||||||
- API key loaded from `.env` (`DATAMALL_API_KEY`)
|
- API key loaded from `.env` (`DATAMALL_API_KEY`)
|
||||||
|
- Backend port configurable via `.env` (`APP_PORT`)
|
||||||
- Bus Transport endpoints exposed under `/api/v1`
|
- Bus Transport endpoints exposed under `/api/v1`
|
||||||
- Container health endpoints: `/healthz`, `/readyz`
|
- Container health endpoints: `/healthz`, `/readyz`
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ uv sync
|
|||||||
4. Start the app:
|
4. Start the app:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
uv run uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
|
uv run uvicorn app.main:app --host 0.0.0.0 --port ${APP_PORT:-8000} --reload
|
||||||
```
|
```
|
||||||
|
|
||||||
## Docker Run
|
## Docker Run
|
||||||
@@ -49,7 +50,7 @@ docker compose up --build -d
|
|||||||
3. Check health:
|
3. Check health:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl http://localhost:8000/healthz
|
curl http://localhost:${APP_PORT:-8000}/healthz
|
||||||
```
|
```
|
||||||
|
|
||||||
The compose stack includes a Valkey service for caching.
|
The compose stack includes a Valkey service for caching.
|
||||||
@@ -62,6 +63,7 @@ Set in `.env`:
|
|||||||
- `VALKEY_URL=redis://valkey:6379/0`
|
- `VALKEY_URL=redis://valkey:6379/0`
|
||||||
- `VALKEY_CONNECT_TIMEOUT_SECONDS=1`
|
- `VALKEY_CONNECT_TIMEOUT_SECONDS=1`
|
||||||
- `VALKEY_DEFAULT_TTL_SECONDS=120`
|
- `VALKEY_DEFAULT_TTL_SECONDS=120`
|
||||||
|
- `APP_PORT=8000`
|
||||||
|
|
||||||
Current cache behavior:
|
Current cache behavior:
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ class Settings(BaseSettings):
|
|||||||
app_env: str = "development"
|
app_env: str = "development"
|
||||||
app_debug: bool = False
|
app_debug: bool = False
|
||||||
app_version: str = "0.1.0"
|
app_version: str = "0.1.0"
|
||||||
|
app_port: int = Field(8000, ge=1, le=65535)
|
||||||
|
|
||||||
datamall_base_url: str = "https://datamall2.mytransport.sg/ltaodataservice"
|
datamall_base_url: str = "https://datamall2.mytransport.sg/ltaodataservice"
|
||||||
datamall_api_key: str = Field(..., description="LTA DataMall AccountKey")
|
datamall_api_key: str = Field(..., description="LTA DataMall AccountKey")
|
||||||
|
|||||||
+2
-2
@@ -16,9 +16,9 @@ services:
|
|||||||
depends_on:
|
depends_on:
|
||||||
- valkey
|
- valkey
|
||||||
ports:
|
ports:
|
||||||
- "8000:8000"
|
- "${APP_PORT:-8000}:${APP_PORT:-8000}"
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/healthz', timeout=3)"]
|
test: ["CMD", "python", "-c", "import os, urllib.request; urllib.request.urlopen(f'http://localhost:{os.getenv(\"APP_PORT\", \"8000\")}/healthz', timeout=3)"]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 3
|
retries: 3
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
from app.core.config import Settings
|
||||||
|
|
||||||
|
|
||||||
|
def test_settings_default_backend_port_is_8000():
|
||||||
|
settings = Settings(datamall_api_key="test-account-key")
|
||||||
|
|
||||||
|
assert settings.app_port == 8000
|
||||||
|
|
||||||
|
|
||||||
|
def test_settings_reads_backend_port_from_env_value():
|
||||||
|
settings = Settings(datamall_api_key="test-account-key", app_port=9010)
|
||||||
|
|
||||||
|
assert settings.app_port == 9010
|
||||||
Reference in New Issue
Block a user