From f4bf239679f56a527f4bdfc5f42cbab6fa0912e3 Mon Sep 17 00:00:00 2001 From: ngqile Date: Thu, 26 Nov 2026 16:07:01 +0800 Subject: [PATCH] chore(local): docker-compose stack with MySQL, Redis, Pub/Sub emulator and all services Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM --- local/.env.example | 20 ++++ local/appkeys.json | 10 ++ local/docker-compose.yml | 113 ++++++++++++++++++ local/pubsub/init.sh | 15 +++ local/seed/00-schemas.sql | 6 + local/up.sh | 8 ++ .../add-repo-foundation-local-stack/tasks.md | 2 +- 7 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 local/.env.example create mode 100644 local/appkeys.json create mode 100644 local/docker-compose.yml create mode 100755 local/pubsub/init.sh create mode 100644 local/seed/00-schemas.sql create mode 100755 local/up.sh diff --git a/local/.env.example b/local/.env.example new file mode 100644 index 0000000..4f33232 --- /dev/null +++ b/local/.env.example @@ -0,0 +1,20 @@ +# Local docker-compose stack only. Dev values, never used anywhere else. +# up.sh copies this to local/.env (git-ignored) on first run. +MYSQLHOST=mysql +MYSQLPORT=3306 +MYSQLUSER=root +MYSQLPWD=colite-dev +REDISHOST=redis +GCP_PROJECT=co-lite-local +GCP_CREDENTIAL_FILE= +APIGATEWAYURL=http://api-gateway +AES_SECRET_KEY=colite-local-aes-key +# Auth service signs and mobile-api verifies, so both must hold the same value (32+ chars). +JWT_TOKEN=colite-local-jwt-secret-0123456789abcdef +JWT_SECRET=colite-local-jwt-secret-0123456789abcdef +USER_AUTH_API_SECRET=colite-local-user-auth-secret +# mobile-api -> gateway key; must match a key in local/appkeys.json. +APIKEY=colite-local-gateway-key-000000000000 +# Inbound keys clients send to mobile-api: ":" (APP or CP). +AUTH_API_KEYS=colite-local-app-key:APP,colite-local-cp-key:CP +JAVA_OPTS=-Xmx384m diff --git a/local/appkeys.json b/local/appkeys.json new file mode 100644 index 0000000..dfd7739 --- /dev/null +++ b/local/appkeys.json @@ -0,0 +1,10 @@ +[ + { + "key": "colite-local-gateway-key-000000000000", + "name": "Local mobile-api", + "desc": "Local docker-compose only", + "type": "BO", + "allowList": ["/"], + "ipList": ["0.0.0.0/0"] + } +] diff --git a/local/docker-compose.yml b/local/docker-compose.yml new file mode 100644 index 0000000..d15a6a5 --- /dev/null +++ b/local/docker-compose.yml @@ -0,0 +1,113 @@ +# Local dev stack. Service names match the Kubernetes service names. +name: co-lite +services: + mysql: + image: mysql:8.0 + environment: + MYSQL_ROOT_PASSWORD: ${MYSQLPWD} + ports: ["3307:3306"] + volumes: + - mysql-data:/var/lib/mysql + - ./seed:/docker-entrypoint-initdb.d:ro + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-p${MYSQLPWD}"] + interval: 5s + retries: 60 + redis: + image: redis:7 + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + retries: 20 + pubsub: + image: gcr.io/google.com/cloudsdktool/google-cloud-cli:emulators + command: gcloud beta emulators pubsub start --host-port=0.0.0.0:8085 --project=co-lite-local + healthcheck: + test: ["CMD", "curl", "-sf", "http://localhost:8085"] + interval: 5s + retries: 30 + pubsub-init: + image: curlimages/curl:8.11.1 + entrypoint: ["sh", "/pubsub/init.sh"] + volumes: + - ./pubsub:/pubsub:ro + depends_on: + pubsub: {condition: service_healthy} + api-gateway: + build: + context: ../solvway-api-gateway + args: {BUILD_PROFILE: local} + env_file: .env + depends_on: + mysql: {condition: service_healthy} + redis: {condition: service_healthy} + pubsub-init: {condition: service_completed_successfully} + ports: ["8081:80"] + volumes: + - ./appkeys.json:/deployments/appkeys/appkeys.json:ro + mobile-api: + build: + context: ../solvway-mobile-api + args: {BUILD_PROFILE: local} + env_file: .env + depends_on: + mysql: {condition: service_healthy} + redis: {condition: service_healthy} + pubsub-init: {condition: service_completed_successfully} + ports: ["8080:80"] + service-auth: + build: + context: ../solvway-services-auth + args: {BUILD_PROFILE: local} + env_file: .env + depends_on: + mysql: {condition: service_healthy} + redis: {condition: service_healthy} + pubsub-init: {condition: service_completed_successfully} + service-system: + build: + context: ../solvway-services-system + args: {BUILD_PROFILE: local} + env_file: .env + depends_on: + mysql: {condition: service_healthy} + redis: {condition: service_healthy} + pubsub-init: {condition: service_completed_successfully} + service-counter: + build: + context: ../solvway-services-counter + args: {BUILD_PROFILE: local} + env_file: .env + depends_on: + mysql: {condition: service_healthy} + redis: {condition: service_healthy} + pubsub-init: {condition: service_completed_successfully} + service-member: + build: + context: ../solvway-services-member + args: {BUILD_PROFILE: local} + env_file: .env + depends_on: + mysql: {condition: service_healthy} + redis: {condition: service_healthy} + pubsub-init: {condition: service_completed_successfully} + service-log: + build: + context: ../solvway-services-log + args: {BUILD_PROFILE: local} + env_file: .env + depends_on: + mysql: {condition: service_healthy} + redis: {condition: service_healthy} + pubsub-init: {condition: service_completed_successfully} + service-email: + build: + context: ../solvway-services-email + args: {BUILD_PROFILE: local} + env_file: .env + depends_on: + mysql: {condition: service_healthy} + redis: {condition: service_healthy} + pubsub-init: {condition: service_completed_successfully} +volumes: + mysql-data: diff --git a/local/pubsub/init.sh b/local/pubsub/init.sh new file mode 100755 index 0000000..1a98606 --- /dev/null +++ b/local/pubsub/init.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# Creates every topic and subscription in topics.txt on the Pub/Sub emulator. Idempotent. +set -eu +base="http://pubsub:8085/v1/projects/co-lite-local" +grep -v '^#' /pubsub/topics.txt | while read -r topic sub; do + [ -z "$topic" ] && continue + code=$(curl -s -o /dev/null -w '%{http_code}' -X PUT "$base/topics/$topic") + case $code in 200|409) ;; *) echo "topic $topic failed: HTTP $code"; exit 1;; esac + if [ -n "${sub:-}" ]; then + code=$(curl -s -o /dev/null -w '%{http_code}' -X PUT -H 'Content-Type: application/json' \ + -d "{\"topic\":\"projects/co-lite-local/topics/$topic\"}" "$base/subscriptions/$sub") + case $code in 200|409) ;; *) echo "subscription $sub failed: HTTP $code"; exit 1;; esac + fi +done +echo "pubsub-init: done" diff --git a/local/seed/00-schemas.sql b/local/seed/00-schemas.sql new file mode 100644 index 0000000..30d2787 --- /dev/null +++ b/local/seed/00-schemas.sql @@ -0,0 +1,6 @@ +-- Local dev only. Creates the empty service schemas; 01-schema.sql (Tastefully dump) fills them. +CREATE DATABASE IF NOT EXISTS `service-system` CHARACTER SET utf8mb4; +CREATE DATABASE IF NOT EXISTS `service-counter` CHARACTER SET utf8mb4; +CREATE DATABASE IF NOT EXISTS `service-member` CHARACTER SET utf8mb4; +CREATE DATABASE IF NOT EXISTS `service-log` CHARACTER SET utf8mb4; +CREATE DATABASE IF NOT EXISTS `service-email` CHARACTER SET utf8mb4; diff --git a/local/up.sh b/local/up.sh new file mode 100755 index 0000000..98168d0 --- /dev/null +++ b/local/up.sh @@ -0,0 +1,8 @@ +#!/bin/sh +# Builds every backend module and starts the local stack. +set -eu +cd "$(dirname "$0")" +[ -f .env ] || cp .env.example .env +(cd .. && mvn -T 1C -B -q -DskipTests package) +docker compose up -d --build +docker compose ps diff --git a/openspec/changes/add-repo-foundation-local-stack/tasks.md b/openspec/changes/add-repo-foundation-local-stack/tasks.md index 08acad6..7812afc 100644 --- a/openspec/changes/add-repo-foundation-local-stack/tasks.md +++ b/openspec/changes/add-repo-foundation-local-stack/tasks.md @@ -18,7 +18,7 @@ Run serially: every group touches build files or shared config. - [x] 3.1 Extract every Pub/Sub topic and subscription name used by the kept modules: read each `PubSubConfig`, `PubSubListener` and publisher call, and resolve property values. Write them to `local/pubsub/topics.txt` as `topic subscription` lines, plus publish-only topics as `topic` lines. Verify: every `@Value("${pubsub...}")` key in the kept modules resolves to a name (none unresolved). Result: 32 unique subscriptions plus 9 publish-only topics. There are fewer than the 37 adapter constructions because Tastefully shares some subscription names across services (e.g. `new-member-add-user-subscription` in system, counter and member). This is kept as-is. - [x] 3.2 Add `application-local.yml` to each kept Spring module with the emulator host and `co-lite-local` project id. Verify: `mvn -T 1C verify` still passes. -- [ ] 3.3 Add `local/docker-compose.yml`: mysql:8, redis:7, the Pub/Sub emulator, a pubsub-init one-shot that reads `topics.txt`, and the eight Java services named as in design decision 4, each with `JAVA_OPTS=-Xmx384m`. Add `local/.env.example` with dev-only values for every env var listed in design Context, and git-ignore `local/.env`. Add `local/up.sh`, which runs `mvn -T 1C -DskipTests package` then `docker compose up -d --build`. Verify: with an empty MySQL (schemas created empty by an init script), `local/up.sh` brings pubsub-init to exit 0, and no service log shows a missing topic or subscription error. +- [x] 3.3 Add `local/docker-compose.yml`: mysql:8, redis:7, the Pub/Sub emulator, a pubsub-init one-shot that reads `topics.txt`, and the eight Java services named as in design decision 4, each with `JAVA_OPTS=-Xmx384m`. Add `local/.env.example` with dev-only values for every env var listed in design Context, and git-ignore `local/.env`. Add `local/up.sh`, which runs `mvn -T 1C -DskipTests package` then `docker compose up -d --build`. Verify: with an empty MySQL (schemas created empty by an init script), `local/up.sh` brings pubsub-init to exit 0, and no service log shows a missing topic or subscription error. Result (2026-11-26): pubsub-init exit 0, 7 of 8 services start on empty schemas, and service-counter stops only on `Table 'service-counter.event' doesn't exist`, which is expected until group 4 seeds the schema. Gateway API keys are mounted from `local/appkeys.json` at `/deployments/appkeys/appkeys.json`, as in k8s. ## 4. Seed (needs the user's Tastefully dump)