Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md

Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
This commit is contained in:
2026-07-07 14:44:12 +08:00
parent d4ef996f49
commit aeddedf2a3
1348 changed files with 176 additions and 57 deletions
+69
View File
@@ -0,0 +1,69 @@
# GoChat Dockerfile — Multi-stage build with security hardening
# Reference: Chatwoot docker/Dockerfile multi-stage pattern + security best practices
# ========== Build Stage ==========
FROM golang:1.24-alpine AS builder
# Build arguments for version injection
ARG VERSION=dev
ARG COMMIT_SHA=unknown
ARG BUILD_DATE=unknown
WORKDIR /app
# Install git for go mod download with private repos
RUN apk add --no-cache git
# Dependency layer — cached unless go.mod/go.sum change
COPY backend/go.mod backend/go.sum ./
RUN go mod download
# Copy source code (build context = repo root)
COPY backend/ .
# Build with version info injected via ldflags
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -ldflags="-s -w \
-X github.com/gochat/gochat/internal/config.Version=${VERSION} \
-X github.com/gochat/gochat/internal/config.CommitSHA=${COMMIT_SHA} \
-X github.com/gochat/gochat/internal/config.BuildDate=${BUILD_DATE}" \
-o /gochat ./cmd/gochat/
# Build worker binary (same source, --worker-only flag in main)
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -ldflags="-s -w \
-X github.com/gochat/gochat/internal/config.Version=${VERSION} \
-X github.com/gochat/gochat/internal/config.CommitSHA=${COMMIT_SHA} \
-X github.com/gochat/gochat/internal/config.BuildDate=${BUILD_DATE}" \
-o /gochat-worker ./cmd/gochat/
# ========== Production Stage ==========
FROM alpine:3.21
# Install runtime dependencies
RUN apk --no-cache add ca-certificates tzdata curl && addgroup -S gochat && adduser -S gochat -G gochat
WORKDIR /app
# Copy binary and configs from builder
COPY --from=builder /gochat /app/gochat
COPY --from=builder /gochat-worker /app/gochat-worker
COPY configs/ /app/configs/
COPY migrations/ /app/migrations/
# Set ownership to non-root user
RUN mkdir -p /app/storage/uploads && chown -R gochat:gochat /app
# Switch to non-root user for security
USER gochat
# Health check — references the /health endpoint we will add
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD curl -f http://localhost:3000/health || exit 1
EXPOSE 3000
# Default entrypoint — can be overridden for worker mode:
# docker run gochat/gochat serve → API server
# docker run gochat/gochat serve --worker-only → background worker
ENTRYPOINT ["/app/gochat"]
CMD ["serve"]
+19
View File
@@ -0,0 +1,19 @@
# GoChat Development Dockerfile
# Uses air for hot reload, mirrors Chatwoot dev pattern with volume mount
FROM golang:1.24-alpine
# Install air (hot reload tool) and development tools
RUN go install github.com/air-verse/air@latest && apk add --no-cache git
WORKDIR /app
# Pre-download dependencies for faster rebuilds
COPY backend/go.mod backend/go.sum ./
RUN go mod download
# Full source mount via volume in compose — no COPY needed here
# air.toml config must exist in backend/ (project root for the Go module)
EXPOSE 3000
CMD ["air", "-c", ".air.toml"]
+99
View File
@@ -0,0 +1,99 @@
# GoChat Development Environment
# Reference: Chatwoot docker-compose.yaml pattern — rails + sidekiq + postgres + redis + mailhog + vite
# Enhanced with pgvector (for Captain AI vector search), volume mounts, hot reload
version: '3.8'
services:
postgres:
image: pgvector/pgvector:pg16
container_name: gochat-postgres
environment:
POSTGRES_DB: gochat_dev
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
container_name: gochat-redis
ports:
- "6379:6379"
volumes:
- redis_data:/data
command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 5s
retries: 5
mailhog:
image: mailhog/mailhog:v1.0.1
container_name: gochat-mailhog
ports:
- "1025:1025" # SMTP
- "8025:8025" # Web UI
gochat:
build:
context: ../..
dockerfile: deploy/docker/Dockerfile.dev
container_name: gochat-app
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
ports:
- "3000:3000"
volumes:
- ../../backend:/app # Hot reload: mount source code
env_file: ../../.env
environment:
- GOCHAT_ENV=development
- POSTGRES_HOST=postgres
- POSTGRES_PORT=5432
- POSTGRES_DATABASE=gochat_dev
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- REDIS_HOST=redis
- REDIS_PORT=6379
- SMTP_HOST=mailhog
- SMTP_PORT=1025
worker:
build:
context: ../..
dockerfile: deploy/docker/Dockerfile.dev
container_name: gochat-worker
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
command: ["serve", "--worker-only"]
volumes:
- ../../backend:/app
env_file: ../../.env
environment:
- GOCHAT_ENV=development
- POSTGRES_HOST=postgres
- POSTGRES_PORT=5432
- POSTGRES_DATABASE=gochat_dev
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- REDIS_HOST=redis
- REDIS_PORT=6379
volumes:
postgres_data:
redis_data:
+107
View File
@@ -0,0 +1,107 @@
# GoChat Production Environment
# Reference: Chatwoot docker-compose.production.yaml — rails + sidekiq + postgres + redis, restart policies
# Enhanced with pgvector, resource limits, non-root user, health checks
version: '3.8'
services:
postgres:
image: pgvector/pgvector:pg16
container_name: gochat-postgres
restart: always
environment:
POSTGRES_DB: ${POSTGRES_DB:-gochat_production}
POSTGRES_USER: ${POSTGRES_USER:-gochat}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} # MUST be set in .env
ports:
- "127.0.0.1:5432:5432" # Only localhost access
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-gochat}"]
interval: 10s
timeout: 5s
retries: 5
deploy:
resources:
limits:
memory: 1G
redis:
image: redis:7-alpine
container_name: gochat-redis
restart: always
command: redis-server --requirepass "${REDIS_PASSWORD}" --appendonly yes --maxmemory 512mb --maxmemory-policy allkeys-lru
ports:
- "127.0.0.1:6379:6379" # Only localhost access
volumes:
- redis_data:/data
healthcheck:
test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping"]
interval: 10s
timeout: 5s
retries: 5
deploy:
resources:
limits:
memory: 512M
gochat:
image: gochat/gochat:${GOCHAT_VERSION:-latest}
container_name: gochat-app
restart: always
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
ports:
- "127.0.0.1:3000:3000" # Reverse proxy should handle external access
env_file: ../../.env
environment:
- GOCHAT_ENV=production
- POSTGRES_HOST=postgres
- POSTGRES_PORT=5432
- REDIS_HOST=redis
- REDIS_PORT=6379
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 5s
start_period: 15s
retries: 3
deploy:
resources:
limits:
memory: 512M
cpus: '1.0'
reservations:
memory: 256M
cpus: '0.5'
worker:
image: gochat/gochat:${GOCHAT_VERSION:-latest}
container_name: gochat-worker
restart: always
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
command: ["serve", "--worker-only"]
env_file: ../../.env
environment:
- GOCHAT_ENV=production
- POSTGRES_HOST=postgres
- POSTGRES_PORT=5432
- REDIS_HOST=redis
- REDIS_PORT=6379
deploy:
resources:
limits:
memory: 512M
cpus: '1.0'
volumes:
postgres_data:
redis_data:
+51
View File
@@ -0,0 +1,51 @@
# GoChat Test Environment
# Used by CI pipeline for running integration tests
# Reference: Chatwoot docker-compose.test.yaml pattern
version: '3.8'
services:
postgres:
image: pgvector/pgvector:pg16
environment:
POSTGRES_DB: gochat_test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
ports:
- "6379:6379"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 5s
retries: 5
test:
build:
context: ../..
dockerfile: deploy/docker/Dockerfile
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
environment:
- GOCHAT_ENV=test
- POSTGRES_HOST=postgres
- POSTGRES_PORT=5432
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DATABASE=gochat_test
- REDIS_HOST=redis
- REDIS_PORT=6379
- GOCHAT_JWT_SECRET=test_secret
command: ["test"]
+72
View File
@@ -0,0 +1,72 @@
# GoChat Development Environment
# Reference: Chatwoot docker-compose.yaml development setup
# Provides PostgreSQL + Redis + Mailhog for local development
version: '3.8'
services:
postgres:
image: postgres:16-alpine
container_name: gochat-postgres
environment:
POSTGRES_DB: gochat_dev
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
container_name: gochat-redis
ports:
- "6379:6379"
volumes:
- redis_data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 5s
retries: 5
mailhog:
image: mailhog/mailhog:v1.0.1
container_name: gochat-mailhog
ports:
- "1025:1025" # SMTP
- "8025:8025" # Web UI
# Reference: Chatwoot docker-compose includes mailhog for email testing
gochat:
build:
context: ../..
dockerfile: deploy/docker/Dockerfile
container_name: gochat-app
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
ports:
- "3000:3000"
env_file: ../../.env
environment:
- GOCHAT_ENV=development
- POSTGRES_HOST=postgres
- POSTGRES_PORT=5432
- POSTGRES_DATABASE=gochat_dev
- POSTGRES_USERNAME=postgres
- POSTGRES_PASSWORD=postgres
- REDIS_URL=redis://redis:6379
volumes:
- ../../backend:/app:delegated
volumes:
postgres_data:
redis_data: