|
| 1 | +package healthcheck |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/gofiber/fiber/v2" |
| 9 | + "github.com/oidc-mytoken/utils/httpclient" |
| 10 | + "github.com/oidc-mytoken/utils/utils" |
| 11 | + log "github.com/sirupsen/logrus" |
| 12 | + "github.com/zachmann/go-oidfed/pkg" |
| 13 | + "github.com/zachmann/go-oidfed/pkg/cache" |
| 14 | + |
| 15 | + "github.com/oidc-mytoken/server/internal/config" |
| 16 | + "github.com/oidc-mytoken/server/internal/db/dbrepo/versionrepo" |
| 17 | + "github.com/oidc-mytoken/server/internal/model/version" |
| 18 | + "github.com/oidc-mytoken/server/internal/server/routes" |
| 19 | +) |
| 20 | + |
| 21 | +// Start starts the healthcheck endpoint on the configured port if enabled |
| 22 | +func Start() { |
| 23 | + if !config.Get().Server.Healthcheck.Enabled { |
| 24 | + return |
| 25 | + } |
| 26 | + httpServer := fiber.New() |
| 27 | + httpServer.Get( |
| 28 | + "", handleHealthCheck, |
| 29 | + ) |
| 30 | + addr := fmt.Sprintf(":%d", config.Get().Server.Healthcheck.Port) |
| 31 | + log.Infof("Healthcheck endpoint started on %s", addr) |
| 32 | + go func() { |
| 33 | + log.WithError(httpServer.Listen(addr)).Fatal() |
| 34 | + }() |
| 35 | +} |
| 36 | + |
| 37 | +type status struct { |
| 38 | + Healthy bool `json:"healthy"` |
| 39 | + Operational bool `json:"operational"` |
| 40 | + Components componentsStatus `json:"components"` |
| 41 | + Version string `json:"version"` |
| 42 | + Timestamp pkg.Unixtime `json:"timestamp"` |
| 43 | +} |
| 44 | + |
| 45 | +type componentsStatus struct { |
| 46 | + ServerUp bool `json:"server_up"` |
| 47 | + ServerReachable bool `json:"server_reachable"` |
| 48 | + Database bool `json:"database_up"` |
| 49 | + Cache bool `json:"cache_up"` |
| 50 | +} |
| 51 | + |
| 52 | +func (c componentsStatus) healthy() bool { |
| 53 | + return c.ServerUp && c.ServerReachable && c.Database && c.Cache |
| 54 | +} |
| 55 | +func (c componentsStatus) operational() bool { |
| 56 | + return c.ServerUp && c.ServerReachable && c.Database |
| 57 | +} |
| 58 | + |
| 59 | +func handleHealthCheck(ctx *fiber.Ctx) error { |
| 60 | + state := healthcheck() |
| 61 | + if !state.Operational { |
| 62 | + ctx.Status(fiber.StatusServiceUnavailable) |
| 63 | + } |
| 64 | + return ctx.JSON(state) |
| 65 | +} |
| 66 | + |
| 67 | +func healthcheck() status { |
| 68 | + components := componentsStatus{ |
| 69 | + ServerUp: true, |
| 70 | + ServerReachable: checkServer(), |
| 71 | + Database: checkDB(), |
| 72 | + Cache: checkCache(), |
| 73 | + } |
| 74 | + return status{ |
| 75 | + Healthy: components.healthy(), |
| 76 | + Operational: components.operational(), |
| 77 | + Components: components, |
| 78 | + Version: version.VERSION, |
| 79 | + Timestamp: pkg.Unixtime{Time: time.Now()}, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func checkServer() bool { |
| 84 | + _, err := httpclient.Do().R().Get(routes.ConfigEndpoint) |
| 85 | + if err != nil { |
| 86 | + log.WithError(err).WithField("healthcheck", "server_reachable").Error("error server healthcheck") |
| 87 | + return false |
| 88 | + } |
| 89 | + return true |
| 90 | +} |
| 91 | + |
| 92 | +func checkDB() bool { |
| 93 | + _, err := versionrepo.GetVersionState(log.StandardLogger(), nil) |
| 94 | + if err != nil { |
| 95 | + log.WithError(err).WithField("healthcheck", "db").Error("error db healthcheck") |
| 96 | + return false |
| 97 | + } |
| 98 | + return true |
| 99 | +} |
| 100 | + |
| 101 | +var cacheMutex sync.Mutex |
| 102 | + |
| 103 | +func checkCache() bool { |
| 104 | + cacheMutex.Lock() |
| 105 | + defer cacheMutex.Unlock() |
| 106 | + k := "healthcheck" |
| 107 | + v := utils.RandASCIIString(64) |
| 108 | + if err := cache.Set(k, v, time.Second); err != nil { |
| 109 | + log.WithError(err).WithField("healthcheck", "cache").Error("error caching healthcheck") |
| 110 | + return false |
| 111 | + } |
| 112 | + var cached string |
| 113 | + set, err := cache.Get(k, &cached) |
| 114 | + if err != nil { |
| 115 | + log.WithError(err).WithField("healthcheck", "cache"). |
| 116 | + Error("error obtaining cached healthcheck") |
| 117 | + return false |
| 118 | + } |
| 119 | + if !set { |
| 120 | + log.WithField("healthcheck", "cache").Error("cached healthcheck not found") |
| 121 | + return false |
| 122 | + } |
| 123 | + if cached != v { |
| 124 | + log.WithField("healthcheck", "cache"). |
| 125 | + WithField("cached", v). |
| 126 | + WithField("obtained", cached). |
| 127 | + Error("cached value does not match") |
| 128 | + return false |
| 129 | + } |
| 130 | + return true |
| 131 | +} |
0 commit comments