How bhamid7112/aws-chatbot works
Purpose and topology
A single-screen streaming chatbot: React/Vite UI sends chat requests to a FastAPI API and renders SSE reply fragments. It deploys as two containers on one AWS EC2 instance; Caddy is the sole public ingress, serves the compiled UI, terminates HTTPS, and proxies /api/* internally. Primary entry points: frontend/src/main.tsx, backend/app/main.py, deploy/docker-compose.yml.
Backend layers and request boundary
FastAPI is assembled in backend/app/main.py. HTTP routes validate DTO shape and expose POST /api/chat in backend/app/interfaces/routes.py; dependencies select concrete implementations in backend/app/interfaces/dependencies.py. Interface code translates HTTP/Pydantic data into domain requests, keeping framework concerns outside the use case.
Chat use case and domain port
backend/app/application/chat_service.py synchronously applies prompt rules before streaming, allowing invalid requests to return HTTP 422 before headers are committed. It depends on domain entities, errors, and the ReplyGenerator port rather than infrastructure: backend/app/domain/entities.py, backend/app/domain/ports.py, backend/app/domain/errors.py. This is the backend swap boundary for a real LLM provider.
Streaming generator and SSE protocol
The current adapter, backend/app/infrastructure/canned_reply_generator.py, emits a fixed reply word-by-word. backend/app/interfaces/sse.py frames generator chunks as SSE delta, in-stream error, and final [DONE] events. Mid-stream failures cannot change HTTP status, so they are encoded in-band and always followed by [DONE]; pre-stream failures remain normal HTTP errors.
Frontend composition and stream consumption
frontend/src/main.tsx wires the concrete gateway, while frontend/src/App.tsx hosts the single chat screen. frontend/src/application/useChat.ts owns transcript and in-flight assistant-message state: append user message, invoke the gateway, and append incoming deltas. Presentation components under frontend/src/presentation/ render the transcript, input, typing state, and wire readout without transport knowledge.
Frontend transport boundary
The domain gateway contract is frontend/src/domain/chatGateway.ts; HTTP/SSE parsing is isolated in frontend/src/infrastructure/sseChatGateway.ts. It POSTs message plus history to relative /api/chat, manually reads fetch response bytes, splits SSE frames, and yields chunks. Relative routing preserves one-origin behavior: Vite proxies locally and Caddy proxies in production, avoiding a production CORS dependency.
Runtime, edge, and deployment infrastructure
Compose builds an API image and a Caddy/UI image via deploy/api.Dockerfile, deploy/web.Dockerfile, and deploy/docker-compose.yml. deploy/caddy/Caddyfile disables proxy buffering for streaming and manages ACME IP-address certificates with persisted Caddy data. Terraform provisions a dedicated AWS VPC, public EC2 host, Elastic IP, security/IAM, and SSM-only administration: infra/compute.tf, infra/network.tf, infra/security.tf. Bootstrap/release behavior is installed through infra/templates/user_data.sh.tftpl; the host clones the public repository, rebuilds containers, and redeploys idempotently.
Main entry points: Caddyfile Caddy ingress · docker-compose.yml Container deployment · sseChatGateway.ts SSE chat gateway · useChat.ts Chat state use case
Components 20
Frontend
- Frontend bootstrap
frontend/src/main.tsx - Chat screen
frontend/src/App.tsx - Chat state use case · Owns transcript and in-flight assistant state.
frontend/src/application/useChat.ts - Chat gateway port
frontend/src/domain/chatGateway.ts - SSE chat gateway · Posts to relative /api/chat and yields parsed chunks.
frontend/src/infrastructure/sseChatGateway.ts - Chat presentation · Renders transcript, input, typing state, and wire readout.
Backend
- FastAPI assembly
backend/app/main.py - POST /api/chat · Validates DTOs and translates HTTP data into domain requests.
backend/app/interfaces/routes.py - Dependency selection
backend/app/interfaces/dependencies.py - Chat service · Applies prompt rules before streaming starts.
backend/app/application/chat_service.py - Chat domain · Defines ReplyGenerator provider boundary.
backend/app/domain/ports.py - Canned reply generator · Emits a fixed reply word-by-word.
backend/app/infrastructure/canned_reply_generator.py - SSE framing · Frames delta, in-band error, and final [DONE] events.
backend/app/interfaces/sse.py
Edge & containers
- Caddy ingress · Serves UI, terminates HTTPS, and disables proxy buffering.
deploy/caddy/Caddyfile - Container deployment · Runs API and Caddy/UI containers on one host.
deploy/docker-compose.yml - API and web images
deploy/api.Dockerfile
AWS infrastructure
- EC2 chat host · Single public host with Elastic IP and SSM-only administration.
infra/compute.tf - VPC, security & IAM
infra/network.tf - Host bootstrap & release · Clones the public repository and idempotently rebuilds and redeploys.
infra/templates/user_data.sh.tftpl
Other components
- Chat user
Connections 24
- Chat user → Caddy ingress: HTTPS
- Caddy ingress → Frontend bootstrap: serves compiled UI
- Frontend bootstrap → Chat screen: mounts
- Chat screen → Chat state use case: uses
- Chat state use case → Chat presentation: supplies state
- Chat state use case → Chat gateway port: depends on
- SSE chat gateway → Chat gateway port: implements
- Chat gateway port → SSE chat gateway: chat stream
- SSE chat gateway → Caddy ingress: POST /api/chat
- Caddy ingress → FastAPI assembly: proxies /api/*
- FastAPI assembly → POST /api/chat: registers
- POST /api/chat → Dependency selection: resolves
- POST /api/chat → Chat service: invokes
- Chat service → Chat domain: uses port and rules
- Dependency selection → Canned reply generator: selects
- Canned reply generator → Chat domain: implements ReplyGenerator
- Chat service → SSE framing: streams chunks
- SSE framing → SSE chat gateway: SSE delta/error/[DONE]
- Container deployment → API and web images: builds
- Container deployment → Caddy ingress: runs web container
- Container deployment → FastAPI assembly: runs API container
- EC2 chat host → Container deployment: hosts
- VPC, security & IAM → EC2 chat host: provisions access
- Host bootstrap & release → EC2 chat host: installs release flow