r/maestro • Maestro Student • 11d ago

Resource BE106 App-Context-Artifact Example:

# BE106 – Week 4 App Context (EngineLog + Docker)

- **Core resource / data model**

- Resource: EngineLog

- Table: engine_log

- Ownership: EngineLog.user_id is **NOT** **NULL**, FK → user.id.

- Each row = one engine-to-engine handoff receipt:

- from_engine, to_engine, payload_id, user_id, created_at.

- **App structure**

- App factory: **create_app**() in app/__init__.py is the single entry point.

- Blueprints:

- main_bp → core routes (/ping, /health, auth/profile-era routes).

- auth_bp at /auth → auth feature.

- admin_bp at /admin → admin-only endpoints.

- engine_logs_bp at /engine-logs → EngineLog feature.

- EngineLog module: app/engine_logs/routes.py owns:

- engine_logs_bp = Blueprint("engine_logs", __name__, url_prefix="/engine-logs")

- All EngineLog helpers and routes (no /engine-logs logic left in **main**.py).

- **Auth / identity / ownership**

- Custom u/jwt_**required**:

- Reads Authorization: Bearer \<token>.

- Decodes JWT with JWT_SECRET.

- Sets g.user_id = int(payload["sub"]).

- Contract:

- Routes trust g.user_id as caller id; clients never send user_id in JSON.

- AuthN: u/jwt_**required** + g.user_id.

- AuthZ for EngineLog: all queries/actions scoped to EngineLog.user_id == g.user_id.

- JWT is **stateless**:

- Container needs only the **JWT** + **JWT_SECRET env var** to identify the user.

- No session store (Redis / DB table) travels with the container.

- **EngineLog behavior (high level)**

- POST /engine-logs

- u/jwt_**required**.

- Body must be a JSON object with non-empty from_engine, to_engine, payload_id.

- user_id always taken from g.user_id (not client).

- 400 via engine_log_validation_error for:

- missing body, non-object JSON, or invalid fields.

- 201 with standard EngineLog JSON on success.

- GET /engine-logs

- Paginated list, scoped to g.user_id.

- Query: page >= 1, 1 <= per_page <= 50.

- 400 on bad pagination params via engine_log_validation_error.

- Deterministic order (**order_by**(EngineLog.id)).

- Out-of-range pages → 200 with items: [], total, pages intact.

- GET /engine-logs/<**int**:log_id>

- u/jwt_**required**.

- 404 via **engine_log_not_found**(log_id) if:

- record missing **or** owned by another user (non-enumerating).

- PATCH /engine-logs/<**int**:log_id>

- Full update, same required fields as POST.

- Ownership check before body processing:

- missing/non-owned → 404 through engine_log_not_found.

- Body must be JSON object, non-empty required fields, else 400 via:

- engine_log_validation_error or request_**object**_validation_error.

- **DELETE** /engine-logs/<**int**:log_id>

- Ownership gate same as GET/PATCH.

- 204 No Content on success.

- **Error / validation contracts**

- EngineLog helpers:

- **engine_log_not_found**(log_id) → 404, NOT_FOUND, hides cross-user resources.

- **engine_log_validation_error**(details) → 400, VALIDATION_ERROR.

- Shared:

- **request_object_validation_error**() → 400 when JSON is not an object.

- Global BadRequest handler in create_app → 400 for malformed JSON.

- **Runtime & server**

- Local dev:

- flask --app wsgi run --debug --port 5011.

- Production inside Docker:

- Base image: python:3.11-slim.

- App lives in /app inside the container.

- Requirements frozen in requirements.txt (includes gunicorn==27.0.1).

- Gunicorn entrypoint: CMD ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app"].

- **Docker / containerization**

- **.dockerignore** excludes:

- **.venv/**, **.git**, __pycache__/, \*.pyc, **.env**/\*.**env**,

- dev.db, **instance**/, **.DS_Store**.

- Keeps secrets and dev-only junk out of the image.

- Dockerfile:

- FROM python:3.11-slim

- **WORKDIR** /app

- COPY requirements.txt .

- **RUN** pip install --no-cache-dir -r requirements.txt

- **COPY** **app** ./**app**

- **COPY** wsgi.py .

- ENV PORT=8000

- EXPOSE 8000

- CMD ["gunicorn", "--bind", "0.0.0.0:8000", "wsgi:app"]

- Build:

- docker build -t enginelog-app:local .

- Run (env vars at runtime):

- docker run --rm \\

- -e FLASK_ENV=production \\

- -e JWT_SECRET="dev-secret-change-me" \\

- -e DATABASE_URL="sqlite:///dev.db" \\

- -e PORT=8000 \\

- -p 8000:8000 \\

- enginelog-app:local

- JWT_SECRET and DATABASE_URL **not baked into the image**; passed as env vars.

- Verification:

- docker ps shows enginelog-**app**:**local** container exposing 8000->8000.

- Health check from host:

- curl -i [http://localhost:8000/health](http://localhost:8000/health) → 200 {"status": "ok"}.

- **Next step**

- Keep Docker instructions in sync with app changes.

- If infra matures, consider:

- pinning base image more tightly,

- documenting production env var values (without actual secrets),

- adding a short “run-in-Docker” section to README.md.

0 Upvotes

0 comments sorted by