jokes-bapak2/api/handler/health/health.go

48 lines
850 B
Go
Raw Normal View History

package health
import (
2022-09-03 10:53:46 +00:00
"context"
"net/http"
"time"
"github.com/go-redis/redis/v8"
2022-09-03 10:53:46 +00:00
"github.com/minio/minio-go/v7"
)
2022-09-03 14:29:52 +00:00
// Dependencies provides a struct for dependency injection
// on health package
type Dependencies struct {
2022-09-03 10:53:46 +00:00
Bucket *minio.Client
Cache *redis.Client
}
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()
}
2022-09-03 10:53:46 +00:00
_, err = d.Cache.Ping(ctx).Result()
if err != nil {
2022-09-03 10:53:46 +00:00
cacheOk = false
}
2022-09-03 10:53:46 +00:00
if !bucketOk || !cacheOk {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}