2021-08-03 18:14:32 +00:00
|
|
|
package health
|
|
|
|
|
|
|
|
import (
|
2022-09-03 10:53:46 +00:00
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
"github.com/go-redis/redis/v8"
|
2022-09-03 10:53:46 +00:00
|
|
|
"github.com/minio/minio-go/v7"
|
2021-08-03 18:14:32 +00:00
|
|
|
)
|
|
|
|
|
2022-09-03 14:29:52 +00:00
|
|
|
// Dependencies provides a struct for dependency injection
|
|
|
|
// on health package
|
2021-09-26 19:13:38 +00:00
|
|
|
type Dependencies struct {
|
2022-09-03 10:53:46 +00:00
|
|
|
Bucket *minio.Client
|
|
|
|
Cache *redis.Client
|
2021-09-26 19:13:38 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 14:29:52 +00:00
|
|
|
// Health provides a http handler for healthcheck
|
2022-09-03 10:53:46 +00:00
|
|
|
func (d *Dependencies) Health(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx, cancel := context.WithTimeout(r.Context(), time.Second*15)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var bucketOk = true
|
|
|
|
var cacheOk = true
|
|
|
|
|
|
|
|
cancel, err := d.Bucket.HealthCheck(time.Second * 15)
|
2021-09-27 10:10:19 +00:00
|
|
|
if err != nil {
|
2022-09-03 10:53:46 +00:00
|
|
|
bucketOk = false
|
2021-09-27 10:10:19 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 10:53:46 +00:00
|
|
|
if cancel != nil {
|
|
|
|
cancel()
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 10:53:46 +00:00
|
|
|
_, err = d.Cache.Ping(ctx).Result()
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
2022-09-03 10:53:46 +00:00
|
|
|
cacheOk = false
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
2022-09-03 10:53:46 +00:00
|
|
|
|
|
|
|
if !bucketOk || !cacheOk {
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|