feat: initialize Ballet Island 微信小程序 with basic structure and functionality

- Add project configuration for WeChat mini program.
- Create skills lock file for Taroify integration.
- Set up app configuration and styles.
- Implement main app component and index page with welcome message.
- Add health check service to verify backend readiness.
- Configure TypeScript settings and global types.
This commit is contained in:
yuxuanhui
2026-09-28 13:41:48 +08:00
commit 4b42b55928
32 changed files with 12054 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
.git
.env*
bin
coverage
*.log
+13
View File
@@ -0,0 +1,13 @@
# Local development only. Set a strong password for a deployed environment.
PGHOST=127.0.0.1
PGPORT=5432
PGDATABASE=ballet_island
PGUSER=ballet_island
PGPASSWORD=change-me-for-local-development
PGSSLMODE=disable
# Host Go process address; Docker always listens on :8080 inside its container.
HTTP_ADDR=127.0.0.1:8080
API_PORT=8080
API_BIND=127.0.0.1
+14
View File
@@ -0,0 +1,14 @@
FROM golang:1.26-alpine AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/api ./cmd/server
FROM alpine:3.23
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /out/api /usr/local/bin/api
USER 65532:65532
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/api"]
+84
View File
@@ -0,0 +1,84 @@
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"ballet-island/backend/internal/httpapi"
"github.com/jackc/pgx/v5/pgxpool"
)
func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
if err := run(); err != nil {
slog.Error("server stopped", "error", err)
os.Exit(1)
}
}
func run() error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
for _, key := range []string{"PGHOST", "PGDATABASE", "PGUSER", "PGPASSWORD"} {
if os.Getenv(key) == "" {
return fmt.Errorf("%s is required", key)
}
}
// Use libpq environment variables so passwords need no URL escaping.
pool, err := pgxpool.New(ctx, "")
if err != nil {
return fmt.Errorf("configure database: %w", err)
}
defer pool.Close()
startupCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
err = pool.Ping(startupCtx)
cancel()
if err != nil {
return fmt.Errorf("connect database: %w", err)
}
addr := os.Getenv("HTTP_ADDR")
if addr == "" {
addr = "127.0.0.1:8080"
}
server := &http.Server{
Addr: addr,
Handler: httpapi.NewHandler(pool.Ping),
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
}
serverErr := make(chan error, 1)
go func() {
slog.Info("server starting", "address", addr)
serverErr <- server.ListenAndServe()
}()
select {
case err := <-serverErr:
if !errors.Is(err, http.ErrServerClosed) {
return err
}
case <-ctx.Done():
slog.Info("server shutting down")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
_ = server.Close()
return fmt.Errorf("shutdown server: %w", err)
}
}
return nil
}
+49
View File
@@ -0,0 +1,49 @@
name: ballet-island
services:
postgres:
image: postgres:18-alpine
restart: unless-stopped
environment:
POSTGRES_DB: ${PGDATABASE:?Set PGDATABASE in .env}
POSTGRES_USER: ${PGUSER:?Set PGUSER in .env}
POSTGRES_PASSWORD: ${PGPASSWORD:?Set PGPASSWORD in .env}
ports:
- "127.0.0.1:${PGPORT:-5432}:5432"
volumes:
# PostgreSQL 18 stores its versioned data directory under this mount.
- postgres_data:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\""]
interval: 5s
timeout: 3s
retries: 10
start_period: 10s
api:
build:
context: .
restart: unless-stopped
environment:
HTTP_ADDR: :8080
PGHOST: postgres
PGPORT: 5432
PGDATABASE: ${PGDATABASE:?Set PGDATABASE in .env}
PGUSER: ${PGUSER:?Set PGUSER in .env}
PGPASSWORD: ${PGPASSWORD:?Set PGPASSWORD in .env}
PGSSLMODE: disable
ports:
- "${API_BIND:-127.0.0.1}:${API_PORT:-8080}:8080"
depends_on:
postgres:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "-q", "-T", "3", "-O", "/dev/null", "http://127.0.0.1:8080/readyz"]
interval: 10s
timeout: 5s
retries: 3
start_period: 10s
stop_grace_period: 15s
volumes:
postgres_data:
+13
View File
@@ -0,0 +1,13 @@
module ballet-island/backend
go 1.26.0
require github.com/jackc/pgx/v5 v5.11.0
require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/text v0.29.0 // indirect
)
+26
View File
@@ -0,0 +1,26 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+34
View File
@@ -0,0 +1,34 @@
package httpapi
import (
"context"
"encoding/json"
"net/http"
"time"
)
// NewHandler returns health endpoints. ping must honor its context deadline.
func NewHandler(ping func(context.Context) error) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
writeStatus(w, http.StatusOK, "ok")
})
mux.HandleFunc("GET /readyz", func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
if err := ping(ctx); err != nil {
// Do not expose database connection details through a public endpoint.
writeStatus(w, http.StatusServiceUnavailable, "unavailable")
return
}
writeStatus(w, http.StatusOK, "ok")
})
return mux
}
func writeStatus(w http.ResponseWriter, code int, status string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(map[string]string{"status": status})
}
+67
View File
@@ -0,0 +1,67 @@
package httpapi
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestHealthRemainsLiveWithoutDatabase(t *testing.T) {
handler := NewHandler(func(context.Context) error {
t.Fatal("liveness must not query the database")
return nil
})
response := httptest.NewRecorder()
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/healthz", nil))
if response.Code != http.StatusOK || response.Body.String() != "{\"status\":\"ok\"}\n" {
t.Fatalf("unexpected response: %d %s", response.Code, response.Body.String())
}
}
func TestReadinessReflectsDatabaseState(t *testing.T) {
for _, tc := range []struct {
name string
err error
code int
body string
}{
{"ready", nil, http.StatusOK, "{\"status\":\"ok\"}\n"},
{"unavailable", errors.New("private database details"), http.StatusServiceUnavailable, "{\"status\":\"unavailable\"}\n"},
} {
t.Run(tc.name, func(t *testing.T) {
called := false
handler := NewHandler(func(ctx context.Context) error {
called = true
deadline, ok := ctx.Deadline()
if !ok || time.Until(deadline) > 2*time.Second {
t.Fatal("database check must have a bounded deadline")
}
return tc.err
})
response := httptest.NewRecorder()
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/readyz", nil))
if !called || response.Code != tc.code || response.Body.String() != tc.body {
t.Fatalf("unexpected response: %d %s", response.Code, response.Body.String())
}
if !strings.HasPrefix(response.Header().Get("Content-Type"), "application/json") {
t.Fatal("health response must be JSON")
}
})
}
}
func TestReadinessPreservesRequestCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
handler := NewHandler(func(ctx context.Context) error { return ctx.Err() })
response := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodGet, "/readyz", nil).WithContext(ctx)
handler.ServeHTTP(response, request)
if response.Code != http.StatusServiceUnavailable {
t.Fatalf("got %d, want 503", response.Code)
}
}
+20
View File
@@ -0,0 +1,20 @@
#!/bin/sh
set -eu
backend_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$backend_dir"
if [ ! -f .env ]; then
printf '%s\n' '缺少 backend/.env,请先从 .env.example 复制并配置。' >&2
exit 1
fi
# Only source trusted local configuration; .env uses shell assignment syntax.
set -a
. ./.env
set +a
go build -o bin/api ./cmd/server
# Replace the shell so shutdown signals reach the Go process directly.
exec ./bin/api