feat: bootstrap OSDB full-stack skeleton (story #01)
- Go backend: Echo v4 + pgxpool + embedded golang-migrate; health endpoints at /health and /api/v1/health; TDD health handler (httptest) - Vue 3 frontend: Vite + TypeScript + Pinia + vue-router + Tailwind CSS v4; TDD HelloWorld component (@vue/test-utils + jsdom + vitest) - Infra: docker-compose postgres:16 with env-interpolated credentials; Makefile with dev-db health-wait loop, migrate-up/down, run, test, fmt - embed.FS migrations at backend/migrations/ (000001 no-op baseline) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Config holds application configuration loaded from the environment.
|
||||
type Config struct {
|
||||
DatabaseURL string
|
||||
Port string
|
||||
}
|
||||
|
||||
// Load reads configuration from environment variables.
|
||||
// DATABASE_URL is required; PORT defaults to "3000".
|
||||
func Load() *Config {
|
||||
dbURL := os.Getenv("DATABASE_URL")
|
||||
if dbURL == "" {
|
||||
dbURL = "postgres://osdb:osdb@localhost:5432/osdb?sslmode=disable"
|
||||
}
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "3000"
|
||||
}
|
||||
|
||||
return &Config{
|
||||
DatabaseURL: dbURL,
|
||||
Port: port,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// Connect creates and returns a new pgxpool connection pool.
|
||||
// It pings the database to verify connectivity before returning.
|
||||
func Connect(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
|
||||
pool, err := pgxpool.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create pool: %w", err)
|
||||
}
|
||||
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping database: %w", err)
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// HealthHandler serves the health check endpoint.
|
||||
type HealthHandler struct{}
|
||||
|
||||
// NewHealthHandler returns a new HealthHandler.
|
||||
func NewHealthHandler() *HealthHandler {
|
||||
return &HealthHandler{}
|
||||
}
|
||||
|
||||
// Health responds with {"status":"ok"}.
|
||||
func (h *HealthHandler) Health(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, map[string]string{"status": "ok"})
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"osdb/internal/handler"
|
||||
)
|
||||
|
||||
func TestHealth(t *testing.T) {
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
c := e.NewContext(req, rec)
|
||||
|
||||
h := handler.NewHealthHandler()
|
||||
if err := h.Health(c); err != nil {
|
||||
t.Fatalf("Health() returned error: %v", err)
|
||||
}
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", rec.Code)
|
||||
}
|
||||
|
||||
var body map[string]string
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("failed to parse response body: %v", err)
|
||||
}
|
||||
|
||||
if got := body["status"]; got != "ok" {
|
||||
t.Errorf("expected status=ok, got %q", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// Package migrate runs database schema migrations embedded in the binary.
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/postgres"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
|
||||
"osdb/migrations"
|
||||
)
|
||||
|
||||
func newMigrator(databaseURL string) (*migrate.Migrate, error) {
|
||||
src, err := iofs.New(migrations.FS, ".")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create iofs source: %w", err)
|
||||
}
|
||||
|
||||
m, err := migrate.NewWithSourceInstance("iofs", src, databaseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create migrator: %w", err)
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Up applies all pending migrations.
|
||||
func Up(databaseURL string) error {
|
||||
m, err := newMigrator(databaseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.Close()
|
||||
|
||||
if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return fmt.Errorf("migrate up: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Down rolls back a single migration step.
|
||||
func Down(databaseURL string) error {
|
||||
m, err := newMigrator(databaseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer m.Close()
|
||||
|
||||
if err := m.Steps(-1); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return fmt.Errorf("migrate down: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user