From 64885c28c6c4d46044e257277ca5b46a548dcb9a Mon Sep 17 00:00:00 2001 From: Lewis Injai Date: Sat, 30 May 2026 17:04:05 +0300 Subject: [PATCH] feat: add /health endpoint and HTTP server --- cmd/server/main.go | 63 ++++++++++++++++++++++++++++++++----- go.mod | 7 ++++- go.sum | 21 +++++++++++-- internal/api/.gitkeep | 0 internal/api/health.go | 56 +++++++++++++++++++++++++++++++++ internal/api/health_test.go | 60 +++++++++++++++++++++++++++++++++++ internal/api/router.go | 21 +++++++++++++ internal/db/db.go | 29 +++++++++++++++++ 8 files changed, 245 insertions(+), 12 deletions(-) delete mode 100644 internal/api/.gitkeep create mode 100644 internal/api/health.go create mode 100644 internal/api/health_test.go create mode 100644 internal/api/router.go create mode 100644 internal/db/db.go diff --git a/cmd/server/main.go b/cmd/server/main.go index 3ce7e36..b513f0f 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,23 +1,28 @@ package main import ( + "context" + "errors" "fmt" "log/slog" + "net/http" "os" + "os/signal" + "syscall" + "time" + "github.com/ratifydata/ratify/internal/api" "github.com/ratifydata/ratify/internal/config" "github.com/ratifydata/ratify/internal/db" ) func main() { - // Load configuration from environment variables and .env file. cfg, err := config.Load() if err != nil { slog.Error("failed to load configuration", "error", err) os.Exit(1) } - // Set up structured JSON logging. logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ Level: slog.LevelInfo, })) @@ -26,18 +31,60 @@ func main() { slog.Info("Ratify server starting", "port", cfg.Port, "environment", cfg.Environment, - "breach_interval", cfg.BreachDetectionInterval, ) - // Run database migrations before the server starts. - // All pending migrations are applied in order. Already-applied - // migrations are skipped. The server will not start if migrations fail. + // Execute migrations prior to accepting any traffic hooks slog.Info("running database migrations") if err := db.RunMigrations(cfg.DatabaseURL); err != nil { slog.Error("database migration failed", "error", err) os.Exit(1) } - // Placeholder — the real HTTP server starts in Card 15. - fmt.Printf("Ratify server ready on port %d\n", cfg.Port) + // Establish concurrent pool configuration + ctx := context.Background() + pool, err := db.Connect(ctx, cfg.DatabaseURL) + if err != nil { + slog.Error("failed to connect to database", "error", err) + os.Exit(1) + } + defer pool.Close() + + slog.Info("database connection established") + + router := api.NewRouter(pool) + + server := &http.Server{ + Addr: fmt.Sprintf(":%d", cfg.Port), + Handler: router, + ReadTimeout: 10 * time.Second, + WriteTimeout: 30 * time.Second, + IdleTimeout: 60 * time.Second, + } + + // Spin up server loop on a background goroutine so it doesn't block OS signal handlers + go func() { + slog.Info("HTTP server listening", "addr", server.Addr) + if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + slog.Error("HTTP server error", "error", err) + os.Exit(1) + } + }() + + // Orchestrate Graceful Shutdown via OS Traps + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + <-quit + + slog.Info("shutting down server") + + // Allow current requests a 30-second window to complete processing + shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + if err := server.Shutdown(shutdownCtx); err != nil { + slog.Error("server shutdown error", "error", err) + os.Exit(1) + } + + slog.Info("server stopped cleanly") } diff --git a/go.mod b/go.mod index a6260bf..7448578 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,18 @@ module github.com/ratifydata/ratify go 1.25.0 require ( + github.com/go-chi/chi/v5 v5.3.0 github.com/golang-migrate/migrate/v4 v4.19.1 + github.com/jackc/pgx/v5 v5.9.2 github.com/spf13/viper v1.21.0 ) require ( github.com/fsnotify/fsnotify v1.9.0 // indirect github.com/go-viper/mapstructure/v2 v2.4.0 // indirect + 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 github.com/lib/pq v1.10.9 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/sagikazarmark/locafero v0.11.0 // indirect @@ -19,7 +24,7 @@ require ( github.com/spf13/pflag v1.0.10 // indirect github.com/subosito/gotenv v1.6.0 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect + golang.org/x/sync v0.18.0 // indirect golang.org/x/sys v0.38.0 // indirect golang.org/x/text v0.31.0 // indirect - gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) diff --git a/go.sum b/go.sum index 51eb139..b8a1ec9 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,7 @@ github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M= github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE= github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dhui/dktest v0.4.6 h1:+DPKyScKSEp3VLtbMDHcUq6V5Lm5zfZZVb0Sk7Ahom4= @@ -24,6 +25,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/go-chi/chi/v5 v5.3.0 h1:halUjDxhshgXHMrao5bB8eNBXo/rnzwr8m5m36glehM= +github.com/go-chi/chi/v5 v5.3.0/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto= github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= @@ -36,11 +39,16 @@ github.com/golang-migrate/migrate/v4 v4.19.1 h1:OCyb44lFuQfYXYLx1SCxPZQGU7mcaZ7g github.com/golang-migrate/migrate/v4 v4.19.1/go.mod h1:CTcgfjxhaUtsLipnLoQRWCrjYXycRz/g5+RWDuYgPrE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +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.9.2 h1:3ZhOzMWnR4yJ+RW1XImIPsD1aNSz4T4fyP7zlQb56hw= +github.com/jackc/pgx/v5 v5.9.2/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/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= @@ -59,6 +67,7 @@ github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0 github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= @@ -75,6 +84,9 @@ github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU= github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY= +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= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= @@ -91,6 +103,8 @@ go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mx go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0= go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I= +golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM= @@ -98,5 +112,6 @@ golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +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= diff --git a/internal/api/.gitkeep b/internal/api/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/internal/api/health.go b/internal/api/health.go new file mode 100644 index 0000000..b97dea8 --- /dev/null +++ b/internal/api/health.go @@ -0,0 +1,56 @@ +package api + +import ( + "context" + "encoding/json" + "net/http" + "time" + + "github.com/ratifydata/ratify/internal/db" +) + +const version = "0.1.0" + +type healthResponse struct { + Status string `json:"status"` + Database string `json:"database"` + Version string `json:"version"` +} + +// healthHandler returns an HTTP handler that checks application health. +func healthHandler(pool *db.Pool) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + // Enforce a tight timeout constraint to prevent database bottlenecks + // from cascading into hanging HTTP requests. + ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second) + defer cancel() + + dbStatus := "ok" + httpStatus := http.StatusOK + + if err := pool.Ping(ctx); err != nil { + dbStatus = "unreachable" + httpStatus = http.StatusServiceUnavailable + } + + resp := healthResponse{ + Status: statusFromHTTP(httpStatus), + Database: dbStatus, + Version: version, + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(httpStatus) + + if err := json.NewEncoder(w).Encode(resp); err != nil { + return + } + } +} + +func statusFromHTTP(code int) string { + if code == http.StatusOK { + return "ok" + } + return "degraded" +} diff --git a/internal/api/health_test.go b/internal/api/health_test.go new file mode 100644 index 0000000..07d0941 --- /dev/null +++ b/internal/api/health_test.go @@ -0,0 +1,60 @@ +package api + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" +) + +func TestHealthHandler_Healthy(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/health", nil) + rec := httptest.NewRecorder() + + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + if _, err := w.Write([]byte(`{"status":"ok","database":"ok","version":"0.1.0"}`)); err != nil { + t.Fatalf("failed to write response body: %v", err) + } + }) + + handler.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Errorf("expected status 200, got %d", rec.Code) + } + + var resp healthResponse + if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil { + t.Fatalf("failed to decode response: %v", err) + } + + if resp.Status != "ok" { + t.Errorf("expected status 'ok', got %q", resp.Status) + } + if resp.Database != "ok" { + t.Errorf("expected database 'ok', got %q", resp.Database) + } + if resp.Version != version { + t.Errorf("expected version %q, got %q", version, resp.Version) + } +} + +func TestStatusFromHTTP(t *testing.T) { + tests := []struct { + code int + expected string + }{ + {http.StatusOK, "ok"}, + {http.StatusServiceUnavailable, "degraded"}, + {http.StatusInternalServerError, "degraded"}, + } + + for _, tt := range tests { + result := statusFromHTTP(tt.code) + if result != tt.expected { + t.Errorf("statusFromHTTP(%d) = %q, want %q", tt.code, result, tt.expected) + } + } +} diff --git a/internal/api/router.go b/internal/api/router.go new file mode 100644 index 0000000..6322b82 --- /dev/null +++ b/internal/api/router.go @@ -0,0 +1,21 @@ +package api + +import ( + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + "github.com/ratifydata/ratify/internal/db" +) + +// NewRouter creates and configures the HTTP router. +func NewRouter(pool *db.Pool) *chi.Mux { + r := chi.NewRouter() + + // Middleware applied to every request. + r.Use(middleware.Logger) + r.Use(middleware.Recoverer) + + // Health check endpoint. + r.Get("/health", healthHandler(pool)) + + return r +} diff --git a/internal/db/db.go b/internal/db/db.go new file mode 100644 index 0000000..d8b300b --- /dev/null +++ b/internal/db/db.go @@ -0,0 +1,29 @@ +package db + +import ( + "context" + "fmt" + + "github.com/jackc/pgx/v5/pgxpool" +) + +// Pool is a connection pool to the Ratify metadata database. +type Pool = pgxpool.Pool + +// Connect creates a new connection pool to the PostgreSQL database +// at the given URL and verifies connectivity by pinging the server. +func Connect(ctx context.Context, databaseURL string) (*Pool, error) { + pool, err := pgxpool.New(ctx, databaseURL) + if err != nil { + return nil, fmt.Errorf("failed to create connection pool: %w", err) + } + + // Verify the database is actually reachable. pgxpool.New does + // not establish a connection immediately — it only parses the URL. + if err := pool.Ping(ctx); err != nil { + pool.Close() + return nil, fmt.Errorf("failed to reach database: %w", err) + } + + return pool, nil +}