Implement health check for deploy. Make version endpoint to return revision if not set
This commit is contained in:
46
internal/api/health/health_handler.go
Normal file
46
internal/api/health/health_handler.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"payouts/internal/service/database"
|
||||
)
|
||||
|
||||
// Route health route
|
||||
const Route = "/health"
|
||||
|
||||
// New constructs a new health Handler.
|
||||
func New(dbService database.Service) (Handler, error) {
|
||||
return &handler{
|
||||
dbService: dbService,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
dbService database.Service
|
||||
}
|
||||
|
||||
// HealthHandler handles the health check requests
|
||||
func (h *handler) Health(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
status := map[string]any{}
|
||||
|
||||
// Check database connection
|
||||
err := h.dbService.HealthCheck()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
status["Error"] = fmt.Sprintf("%v", err)
|
||||
slog.Error("Health check failed", slog.String("error", status["Error"].(string)))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
status["OK"] = (err == nil)
|
||||
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.Encode(status)
|
||||
}
|
||||
16
internal/api/health/module.go
Normal file
16
internal/api/health/module.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
var Module = fx.Options(
|
||||
fx.Provide(New),
|
||||
)
|
||||
|
||||
// Handler health handler interface
|
||||
type Handler interface {
|
||||
Health(http.ResponseWriter, *http.Request)
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"go.uber.org/fx"
|
||||
|
||||
"payouts/internal/api/health"
|
||||
"payouts/internal/api/payout"
|
||||
"payouts/internal/api/user"
|
||||
"payouts/internal/api/version"
|
||||
@@ -21,6 +22,7 @@ import (
|
||||
// Module is a fx module
|
||||
var Module = fx.Options(
|
||||
user.Module,
|
||||
health.Module,
|
||||
payout.Module,
|
||||
version.Module,
|
||||
monitoring.Module,
|
||||
@@ -40,6 +42,7 @@ type Params struct {
|
||||
PayoutHandler payout.Handler
|
||||
UserHandler user.Handler
|
||||
Version version.Handler
|
||||
HealthHandler health.Handler
|
||||
|
||||
Metrics monitoring.Metrics
|
||||
}
|
||||
@@ -50,7 +53,10 @@ func RegisterRoutes(p Params, lc fx.Lifecycle) {
|
||||
router := mux.NewRouter()
|
||||
router.StrictSlash(true)
|
||||
|
||||
// Version endpoint
|
||||
router.HandleFunc(version.Route, p.Version.VersionHandler).Methods(http.MethodGet)
|
||||
// Health check endpoint
|
||||
router.HandleFunc(health.Route, p.HealthHandler.Health).Methods(http.MethodGet)
|
||||
|
||||
if p.AppConfig.Server.EnablePProfEndpoints {
|
||||
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
||||
|
||||
@@ -3,6 +3,7 @@ package version
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
|
||||
"go.uber.org/fx"
|
||||
|
||||
@@ -39,7 +40,20 @@ type handler struct {
|
||||
func (h *handler) VersionHandler(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
ver := h.version
|
||||
if ver == "unknown" {
|
||||
buildInfo, ok := debug.ReadBuildInfo()
|
||||
if ok {
|
||||
for _, setting := range buildInfo.Settings {
|
||||
if setting.Key == "vcs.revision" {
|
||||
ver = ver + "-" + setting.Value
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
io.WriteString(w, h.version)
|
||||
io.WriteString(w, ver+"\n")
|
||||
}
|
||||
Reference in New Issue
Block a user