Skip to content
Blueprint4AgentPublic

About

FastAPI-based full-stack web server blueprint for agentic coding workflows.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Blueprint4FastAPI

A FastAPI + React blueprint with authentication, typed API contracts and a repeatable workflow for agents and developers. Start with shared application patterns, then adapt the product identity and domain logic to your service.

Quick start · New project setup · Deployment · 한국어

B4A component library running locally in development mode

Actual local development showcase. No customer accounts or external integrations are used.

Included foundation Configuration boundary
FastAPI, SQLAlchemy, Alembic and typed React API clients Domain logic and production database settings belong to your service
Accounts, roles, profile, API keys and realtime refresh Email, OAuth and Stripe require provider credentials and configuration
Shared UI, English/Korean text, theme and component showcase Customize identity with project.json; showcase is development-only
Docker packaging, readiness, persistent storage and optional HTTPS Configure domain, secrets, backups and deployment-specific validation
Agent guides, worklogs, local hooks and contract/browser tests GitHub workflows are manual; local checks are not server-side enforcement
Optional Tauri desktop shell using the same React app Backend hosting, signing and distribution are separate tasks

See publication notes for the supported claims and release evidence.

Documentation Entry

  1. Agent/workflow rules: AGENTS.md
  2. Deployment guide: DEPLOY.md
  3. Backend engineering rules: src/backend/BACKEND.md
  4. Frontend engineering rules: src/frontend/FRONTEND.md
  5. Backend quick guide: src/backend/README.md
  6. Frontend quick guide: src/frontend/README.md
  7. Shared API contract: contracts/README.md (contracts/openapi.json)

Localized documentation rule:

  • Keep translated/localized docs under notes/<locale>/....
  • Mirror the original document structure by domain (backend, frontend, etc.).

Current locale example (ko):

  1. Root guide: notes/ko/README.md
  2. Agent/workflow rules: notes/ko/AGENTS.md
  3. Deployment guide: notes/ko/DEPLOY.md
  4. Backend engineering rules: notes/ko/backend/BACKEND.md
  5. Frontend engineering rules: notes/ko/frontend/FRONTEND.md
  6. Backend test guide: notes/ko/backend/TEST.md
  7. Frontend test guide: notes/ko/frontend/TEST.md
  8. Shared API contract: notes/ko/contracts/README.md

Repository Layout

contracts/
src/
  backend/
  frontend/  # B4React Git submodule
docker/
  scripts/

Quick Start

Install Git, Make, Python 3.11+, uv, Node.js 24 and npm. Local defaults use SQLite and in-memory Redis; Docker deployment additionally requires Docker/Compose.

git clone --recurse-submodules https://github.com/Blueprint4Agent/B4FastAPI.git
cd B4FastAPI
  1. Initialize env files:
make init
  1. Run backend (local development):
make backend-install
make backend-dev
  1. Run frontend (local development):
make frontend-install
make frontend-dev
  1. Open:
  • Backend API docs: http://localhost:8000/docs
  • Frontend app (Vite): http://localhost:5173

Make Workflow Hooks

Run make help to list the available workflow hooks.

Common targets:

make install              # Install backend and frontend dependencies
make backend-dev          # Run FastAPI development server
make frontend-dev         # Run Vite development server
make build                # Build backend environment and frontend artifacts
make test                 # Run backend and frontend tests
make check                # Check code, generated types, and API contracts
make format               # Format backend and frontend code
make ci                   # Run check, test, and build

Docker targets:

make docker-build
make docker-up
make docker-logs DOCKER_SERVICE=app
make docker-down
make docker-deploy
make docker-export
make docker-observability-up
make docker-observability-down

make docker-observability-down stops only Grafana, Prometheus, OpenTelemetry Collector, Tempo, and Loki. The app, PostgreSQL, and Redis keep running. Stopped containers and their data volumes are retained; use make docker-observability-up to start the observability services again.

Environment File Maintenance

make env-sync          # Update development env files (backend/frontend)
make env-check         # Check development env keys and layout
make docker-env-sync   # Update deployment env file (docker/.env)
make docker-env-check  # Check deployment env keys and layout
make env-contract-check # Check shared backend/Docker example keys only

Requires uv; development commands also require the initialized B4React submodule. Each command uses the selected directories' .env.example files. Sync rebuilds the file using the example's comments, blank lines, and key order, preserving existing value tokens (including empty values, quoting, and escapes). New keys receive example defaults. Local comments are replaced by example comments; the original file remains in the backup. Additional keys are reported and retained in a separate section at the end. Missing keys, duplicate keys, invalid syntax, or outdated comments/layout fail the check. Backend and Docker example keys must match, except the explicitly listed Docker-only image, Compose project, host-port, metrics target, and Grafana login settings in scripts/env.py. Values may differ by environment; their contents are never printed.

Add shared backend settings to both src/backend/.env.example and docker/.env.example. make env-contract-check reports which example is missing each key and fails on mismatches, without requiring actual .env files or the frontend submodule. It also runs through make check, make ci, and local backend verification as a required validation step alongside lint/format checks. The regular env commands enforce this same comparison; docker-env-check additionally catches keys missing from the actual deployment .env. Frontend VITE_* settings are separate. Keys present only in a local .env are reported as additional keys; declare new shared settings in the examples to enforce deployment coverage.

Before changing an existing file, sync saves a timestamped backup under the root .env-backups/ directory, excluded from Git and Docker builds. Backups are readable only by their owner. No backup is created for unchanged or newly created files. Missing files are created from their examples. Comment-only and ordering changes also trigger a sync and backup; unchanged files are not rewritten.

make docker-deploy validates resolved runtime settings and the built image, then checks actual HTTP readiness. Docker operation requires Python 3 and Compose, not host uv. docker-env-sync / docker-env-check remain optional development tools for template synchronization and still use uv. See the single-server runbook.

Docker Deployment

For a new AWS/server deployment, start with the monolithic runbook. Use make docker-init PUBLIC_DOMAIN=app.example.com on a fresh installation; it never overwrites an existing environment.

Project names and host ports

Container names are generated by Compose as <project>-<service>-1. COMPOSE_PROJECT_NAME=docker preserves the original project's volume names; newly created containers use names such as docker-postgres-1. Existing fixed-name containers remain until explicitly recreated. Scripts use service names, so they work with both naming styles. To start a separate installation, set a unique project name and unused host ports in docker/.env, for example:

COMPOSE_PROJECT_NAME=b4fastapi
POSTGRES_HOST_PORT=5433
REDIS_HOST_PORT=6380
Host-port setting Default Container port
APP_HOST_PORT 8000 8000
POSTGRES_HOST_PORT 5432 5432
REDIS_HOST_PORT 6379 6379
GRAFANA_HOST_PORT 3000 3000
PROMETHEUS_HOST_PORT 9090 9090
OTEL_GRPC_HOST_PORT 4317 4317
OTEL_HTTP_HOST_PORT 4318 4318

With the example above, the host backend uses DB_PORT=5433 and REDIS_PORT=6380 in src/backend/.env; the Docker app keeps DB_PORT=5432 and REDIS_PORT=6379. When running multiple complete stacks, assign distinct values to every published port and distinct APP_IMAGE tags if they use different builds. Adjust host-facing URLs/CORS and local OTLP endpoints when their ports change. Set PROMETHEUS_BACKEND_TARGET to the actual host backend port for local development; the Docker app target remains app:8000 regardless of APP_HOST_PORT.

Run make docker-env-sync to add the settings. Changing the project name creates a separate stack with separate volumes; it does not rename or migrate existing data. Keep docker for the current installation unless intentionally migrating. To apply host-port changes to existing DB/Redis containers, explicitly recreate the affected service as described below (docker-up preserves existing infrastructure).

Database and observability access

Loki application logs

Logs follow Python logging -> OTLP/gRPC -> Collector batch -> OTLP/HTTP -> Loki. The native Loki OTLP endpoint uses the existing Collector; no Docker socket or additional log agent is needed.

  1. Run make env-sync for development or make docker-env-sync for deployment to add the new setting while preserving existing values (with a backup).

  2. Set LOGS_ENABLED=true in src/backend/.env (host development) or docker/.env (Docker app). The default is false. It is independent of TRACING_ENABLED.

  3. Use OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 on the host, or http://otel-collector:4317 in Docker. Adjust the host port if overridden.

  4. Start the stack with make docker-observability-up. On an existing installation, reload the mounted Collector config and Grafana provisioning:

    docker compose -f docker/docker-compose.yml --env-file docker/.env --profile observability restart otel-collector grafana
  5. Restart the development backend (make backend-dev), or rebuild/recreate the Docker app (make docker-deploy) to load the new dependency and settings.

  6. Open Grafana Explore → Loki, select a recent time range, and run:

    {service_name="blueprint4fastapi-backend"}
    

This collects application/worker logs under uvicorn.app, Uvicorn server logs, and access logs after setup, at the configured LOG_LEVEL. Console output remains. It does not collect PostgreSQL/Redis/container stdout or arbitrary root loggers. Request/task IDs are structured metadata, not index labels; filter with | request_id="..." or | task_id="...". Active OTel spans supply native trace correlation; header/task fallback IDs do not guarantee a stored Tempo trace. SDK batches flush on normal process exit; abrupt termination, queue overflow, or prolonged collector outages can lose logs. This is not a durable audit log.

Before Loki starts, the one-shot loki-init service assigns the volume root to UID/GID 10001, the Loki image user. An Exited (0) status for loki-init is normal. This also repairs the root-owned volume created by the initial configuration; existing data is preserved. Loki itself runs as its non-root image user.

Loki is a single instance with a loki_data volume and seven-day retention (compactor deletion is asynchronous). Its unauthenticated API is only on the Compose network at loki:3100, without a published host port. Grafana queries it through the provisioned Loki datasource. This configuration targets a single host; shared production deployments need storage/access policies appropriate to that environment.

Collector trace batching

The trace pipeline is OTLP receiver -> batch processor -> Tempo/debug exporters. It uses Collector 0.114.0 defaults: a 200ms timeout and an 8192-span send trigger. The trigger is not a maximum batch size. This combines incoming spans before export independently of the backend SDK's existing BatchSpanProcessor. See the batch processor documentation.

After changing the mounted Collector config, restart the running Collector to apply it:

docker compose -f docker/docker-compose.yml --env-file docker/.env --profile observability restart otel-collector

Prometheus target and shared network

App, Prometheus, Grafana, and the other Compose services already share the same <COMPOSE_PROJECT_NAME>_default network. Keep the same project name for app and observability commands; no external shared network is required.

Set one target in docker/.env after make docker-env-sync:

Backend mode PROMETHEUS_BACKEND_TARGET
make backend-dev on the host (default port) host.docker.internal:8000
Host backend on port 8001 host.docker.internal:8001
make docker-up in the same Compose project app:8000

Then run make docker-observability-up to apply the target. Enable METRICS_ENABLED=true in the backend's active env file and restart/recreate the backend if needed. The host backend must listen on an address reachable from Docker (the default make backend-dev binds to 0.0.0.0). A host-gateway mapping supports host access on Linux too. The target contains only host:port; /metrics is configured separately. Prometheus renders and validates its config at container startup. The existing b4fastapi-backend-local job label is retained for dashboard/history compatibility in both modes. Check target health at Prometheus's /targets page.

Database driver

DB_NAME depends on the driver. Change both fields when switching engines:

Runtime DB_DRIVER DB_NAME DB_HOST
Local SQLite (development default) sqlite+aiosqlite template.db (file) Ignored
Host backend with Docker PostgreSQL postgresql+asyncpg template (database) localhost
Docker app with Docker PostgreSQL postgresql+asyncpg template (database) postgres

For PostgreSQL, use an existing database name and matching credentials; the example name template is created only on first initialization of the local DB volume. env-sync preserves existing values, so changing the driver does not automatically replace a SQLite file name with a PostgreSQL database name. Env checks do not verify that the database exists. A database "template.db" does not exist error after switching usually means the SQLite name was left in the PostgreSQL configuration.

  • Local PostgreSQL initializes its user, password, and database from DB_USER, DB_PASSWORD, and DB_NAME. The app uses the same resolved credentials. Existing PostgreSQL volumes keep their existing accounts: changing .env alone does not change a database password. Update the database account separately; do not delete the volume to rotate credentials.
  • A non-empty REDIS_PASSWORD enables password authentication on local Redis, the app connection, and its healthcheck. Empty remains supported for local development; configure a strong password for deployment. PostgreSQL still has a development default password, so set DB_PASSWORD for deployment as well.
  • Database usernames/passwords and Redis passwords are URL-encoded by the backend. Enter the original password in .env, not a pre-encoded URL component. Quote literal values containing $ with single quotes to prevent Compose interpolation.
  • DB, Redis, Grafana, Prometheus, and OTLP host ports bind to 127.0.0.1. Container-to-container traffic still uses Compose service names. Access from a remote workstation requires an SSH tunnel or a separately configured proxy. The app also binds to loopback only, using APP_HOST_PORT (default 8000). Public traffic enters through the selected HTTPS proxy.
  • Tempo and Loki have no published host ports. Grafana queries tempo:3200 and loki:3100 on the Compose network. The Collector sends traces to tempo:4317 and logs to loki:3100/otlp. The former TEMPO_HOST_PORT setting is unused and can be removed from existing docker/.env files.
  • Grafana anonymous access is disabled. Run make docker-env-sync, then set GRAFANA_ADMIN_PASSWORD in docker/.env before make docker-observability-up. Grafana refuses to start with an empty password, admin, or CHANGE_ME*. GRAFANA_ADMIN_USER defaults to admin. These settings initialize new Grafana volumes; existing installations require changing the stored password through Grafana's UI or administrator CLI. Keep .env aligned with that password.

These settings take effect when containers are recreated. Env sync/check only validates keys/layout; it does not rotate existing database or Grafana credentials.

  1. Prepare a new deployment env (review an existing file instead of replacing it):
make docker-init
  1. Build app image:
make docker-build
  1. Start services (app + optional local postgres/redis based on docker/.env):
make docker-up

Requires Python 3 and Docker Compose with up --wait --wait-timeout support. Infrastructure selection reads the app environment resolved by Compose, including quoting, comments, interpolation, and service environment overrides. Existing PostgreSQL/Redis containers are preserved with --no-recreate; missing containers are created and stopped containers are started. Each startup stage waits up to 120 seconds by default (HEALTH_TIMEOUT_SECONDS=180 make docker-up to override). The app healthcheck calls /health/ready, which checks both database and Redis. Startup fails if services do not become healthy; it does not automatically roll back.

When intentionally changing local infrastructure credentials or configuration, recreate only the affected service explicitly, then recreate/redeploy the app:

docker compose -f docker/docker-compose.yml --env-file docker/.env up -d --no-deps --force-recreate postgres
docker compose -f docker/docker-compose.yml --env-file docker/.env up -d --no-deps --force-recreate redis

Recreating PostgreSQL does not rotate credentials already stored in its data volume.

  1. View logs:
make docker-logs DOCKER_SERVICE=app
  1. Stop services:
make docker-down
  1. One-shot deploy (preflight + image preparation + selected services + HTTP smoke):
make docker-deploy

Selected app/mail-worker/proxy services are recreated after image validation; existing DB/Redis containers and data volumes are retained. Success requires HTTP readiness, runtime config and packaged frontend assets, plus public route protection when HTTPS is selected. No automatic database/image rollback or image archive is performed. Set DEPLOY_SOURCE=pull for a registry image or DEPLOY_SOURCE=local for an existing image. All core commands retain COMPOSE_FILE from the selected environment.

  1. Export app image tar:
make docker-export

Exported image files are stored in docker/artifacts/.

Build

Backend:

make backend-format
make backend-test

Frontend:

make frontend-format
make frontend-build

The browser frontend remains the default. For optional local desktop development:

cd src/frontend
npm run tauri:dev

Or run it from the repository root. The launcher automatically adds the standard Rust installation path (~/.cargo/bin) when the current shell has not loaded it:

make frontend-desktop-dev

Shared frontend repository

B4React lives at Blueprint4Agent/B4React and is pinned at src/frontend. Clone with git clone --recurse-submodules, or run make frontend-init in an existing checkout. make init and make frontend-install also initialize the committed pin.

  • make frontend-build: produce only src/frontend/dist.
  • make frontend-package: build and copy dist into backend static assets.
  • make build: backend environment plus integrated frontend packaging.
  • make contract-check: verify backend export freshness and strict additive compatibility with B4React's pinned snapshot.
  • make frontend-api-check: verify generated types without changing child files.

Frontend changes are reviewed and merged in B4React first. Update this repository's submodule commit in a PR afterwards; do not track a moving branch in builds. See frontend integration. Korean frontend source docs are owned by B4React at src/frontend/notes/ko/; the old paths are entry links.

Task workflow and PR checks

AGENTS.md defines required policy and routes to repository commit, PR and issue skills. GitHub templates define output fields; Make/hooks enforce checks. See local hooks for installation and manual Actions, and change-scoped verification for check selection.

Architecture checks

Run make architecture-check to validate the documented static layer boundaries. It also runs in make check and local verification and optional manual CI. Errors include file/line locations. Use make backend-architecture-check or make frontend-architecture-check for a narrower scope. Backend routers delegate DB work to services; lower layers cannot import routers/app.main. Frontend UI consumes page-owned hooks instead of runtime API imports. Schema/Enum and explicit type-only imports are allowed. See the domain guides for the precise static scope and limitations.

API Key consistency

API Key UI notifications are best-effort. After a committed create/delete/status change, Redis/OS/timeout delivery failures are logged without changing the success response. Publication waits at most two seconds; cancellation and programming errors still propagate. No durable event outbox/replay is provided.

The frontend useApiKeys hook owns list/mutation state. HTTP and SSE use the same ID-based updates; server refetches reconcile events and mutation completion. Connection/reconnection, developer-tab activation and desktop recovery reload the list. Stale list/account responses are ignored. Modal/input/one-time key state stays in SettingsPage; tab changes preserve a pending creation result. Background reloads do not blank an already loaded list. Schema contracts remain unchanged.

Operator role management

Assign roles to existing active accounts without adding a settings UI:

make user-role EMAIL=person@example.com ROLE=admin
make user-role EMAIL=person@example.com ROLE=user

This uses the backend environment (src/backend/.env and exported variables), not docker/.env. For deployment, run inside the app container built with this command:

cd docker
docker compose exec app .venv/bin/python -m app.manage_user_role --email person@example.com --role admin

Run only against the intended database after migrations. The command creates no accounts or tables, accepts user/admin, prints user ID and old/new roles, and rejects missing or inactive users and demotion of the last active admin. Changes are serialized in SQLite/PostgreSQL transactions. Output is an operator receipt, not a durable audit store. Authorization uses the new DB role on subsequent requests; reload account data for UI badges. LOGIN_ENABLED=false rejects this command because startup provisions a bootstrap admin. Profile account switching/add-account actions are hidden when login is disabled or unknown. Same-email Google/GitHub/email recent accounts display once using the latest login method; existing local duplicates normalize on read.

Administrator panel

Administrators can open Admin panel from the profile menu (/admin). It uses the settings-style sidebar and lists user identities, roles, account/verification status, login methods, signup dates and latest successful login times. Search by name/email, filter by role/status, page results or refresh manually. Bootstrap admins can use it with login disabled; ordinary users cannot access its API or route. Roles remain managed with make user-role. Active means enabled, not online. No recorded login is shown explicitly; displayed times use the device time zone.

Frontend state management and React optimization policy: English · 한국어. Use make frontend-react-performance-check for safeguards and make frontend-test-routes for production route recovery; both are included in local frontend verification.

New project identity

Tell the repository agent “initialize this project” or “프로젝트 초기화하기” to start conversational onboarding. It collects the name, overview, screen references, icon, UI/document languages and features before applying setup, UI changes and documentation localization. No installed skill is required. 한국어 안내.

Use project initialization to configure a copied blueprint from project.json: service name, brand, desktop identifier and login/email/OAuth switches. Preview with make project-plan, apply with make project-init, and verify with make project-check. Korean guide: 새 프로젝트 초기화.

Local showcase style editing

Run make backend-dev, then make frontend-style-studio in another terminal. Open http://127.0.0.1:5173/show-case. The compact right-hand panel previews common colors, radius, fonts and spacing in the existing showcase before applying them to local src/frontend/src/styles/app.css. Theme mode updates the global app preference. Color dropdowns and numeric controls follow the shared style; the header reload icon rereads the file. Set FRONTEND_DIR=/absolute/path/to/B4React to run a compatible local frontend checkout instead. It must contain this editor's Make target and installed dependencies. The target starts the selected checkout's Vite server; it is not a remote server connection to a browser user's filesystem.

Normal make frontend-dev and production builds do not expose editing. Unsaved preview is scoped to the showcase; apply creates a CSS backup in that frontend's .style-studio-backups/, checks for external changes and triggers HMR. Changes inside the pinned frontend are child-repository changes: review/commit through B4React's own branch/PR workflow before updating the parent pin.

Search and pagination rules and usage: English · 한국어.

Change-scoped verification: English · 한국어.

Celery

Standalone worker/Beat setup and migration audit: English. Korean guide: 한국어.

Stripe / Link billing: English · 한국어. Issue #93 billing audit: English · 한국어. Additional payment provider research: English · 한국어.

API authentication policy audit: English · 한국어.

Runtime modes and roles: English · 한국어.

Object storage providers (local, s3, r2, supabase): English · 한국어.

Startup and shutdown use a B4A service/status panel in interactive terminals, with plain-log fallback. See lifecycle display settings.

Lifecycle email operations: English · 한국어.

License

Original project material is available under the MIT License. Retain the copyright and permission notice when redistributing copies or substantial portions. Third-party dependencies, data and brand assets retain their own notices; see the profile-location data notice. The pinned B4React repository maintains its own license and asset notices.

Notification inbox: English · 한국어.

Blueprint4Agent (B4A) is the umbrella project; Blueprint4FastAPI (B4FastAPI) combines this backend with the independently versioned B4React frontend. Public search pages and deployment: English · 한국어.

Path-based monolithic hosting: basePath deployment · 한국어.

Service layout modes: English · 한국어. Development-only Appearance previews do not change the deployed service default.

Guided service prompts: Prompt builder · 한국어. Available below Home in development and production.

Initialization completion requires the surface and rollback audit, covering configured/fallback identity, console/messages, documentation, language behavior and original assets. Trigger rehearsals use an isolated clone.

About

FastAPI-based full-stack web server blueprint for agentic coding workflows.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages