Sub-system: api_gateway.py aka BFF

 Central DB init can be done separately (or by API Gateway during first run).


That’s a solid architectural direction — and your instinct to have API Gateway own DB initialization is correct for a few reasons:


Why API Gateway Should Initialize the DB

  • Single source of truth: Gateway knows the canonical schema (jobs, etc.).

  • Microservices stay focused: Worker, parser, logging just consume APIs/DB tables, no schema coupling.

  • Easier migrations: Gateway (or a future migration service) handles upgrades instead of each service duplicating logic.

  • Deployment order: DB init happens first, then worker/parser/logging spin up safely.


About Splitting API Gateway and BFF

Right now, your API Gateway is doing two jobs:

  1. Pure routing & orchestration: Upload, query, logging endpoints, etc.

  2. User-facing experience: Login flow (OIDC), session management, templates.

Splitting those would give you:

  • API Gateway

    • Stateless, JSON-only

    • Routes requests to microservices

    • Auth via JWT/OIDC tokens (no session state)

  • BFF (Backend for Frontend)

    • Owns login session UX (OIDC front-channel flow)

    • Renders templates (upload.html, query.html, logs.html)

    • Calls API Gateway for data

This aligns with future scaling: mobile apps → hit API Gateway directly; web → goes through BFF.


Migration Path

1. Short-term (Now)

  • Keep one project (API Gateway + BFF + DB init).

  • Clean up DB layer (MySQL, SQLAlchemy).

  • Worker & Logging just consume DB/API.

2. Medium-term

  • Split BFF (session + templates) from API Gateway (pure API).

  • Use shared JWT/OIDC layer for auth.

3. Long-term

  • Introduce service discovery or API Gateway like Kong/Traefik if needed.

  • API Gateway becomes more like a routing layer; BFF becomes optional client-facing service.


Implementation Plan

  1. Move DB init to API Gateway only

    • Base.metadata.create_all() runs in API Gateway startup.

    • Worker/logging services only query (no schema creation).

  2. Refactor models per table

    • models/job.py, models/log.py, models/authcode.py.

  3. Use MySQL + SQLAlchemy uniformly

    • Remove sqlite3 everywhere.

  4. Keep current monolith (API + BFF)

    • But isolate BFF logic (templates/session) into its own package folder

    • So later you can split it out easily.


Question for you

Do you want me to rewrite your current API Gateway to:

  • Use SQLAlchemy with MySQL

  • Initialize DB (jobs, etc.)

  • Expose APIs unchanged (so worker/logging still work)

  • Keep templates/session logic intact (but modularized so you can split later)?

Or do you want me to show you the refactor structure (folder layout + code snippets) first, so you can review before I rewrite everything?

(Also: want me to include docker-compose at this stage for MySQL + services?)

Comments

Popular posts from this blog

Feature: Audit log for one login, and identity service

Getting started - Build your data science lab environment

QA - Run #1 - Results