2023-04-28 21:59:31 +03:00
|
|
|
package health // import "go.unistack.org/micro-server-http/v4/handler/health"
|
2023-02-26 14:51:34 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-04-28 21:59:31 +03:00
|
|
|
codecpb "go.unistack.org/micro-proto/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/errors"
|
2023-02-26 14:51:34 +03:00
|
|
|
)
|
|
|
|
|
2023-05-04 02:20:25 +03:00
|
|
|
var _ HealthServiceServer = (*Handler)(nil)
|
2023-02-26 14:51:34 +03:00
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
opts Options
|
|
|
|
}
|
|
|
|
|
|
|
|
type CheckFunc func(context.Context) error
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Version string
|
|
|
|
Name string
|
|
|
|
LiveChecks []CheckFunc
|
|
|
|
ReadyChecks []CheckFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func LiveChecks(fns ...CheckFunc) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.LiveChecks = append(o.LiveChecks, fns...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadyChecks(fns ...CheckFunc) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.ReadyChecks = append(o.ReadyChecks, fns...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Name(name string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Version(version string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Version = version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandler(opts ...Option) *Handler {
|
|
|
|
options := Options{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return &Handler{opts: options}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Live(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error {
|
|
|
|
var err error
|
|
|
|
for _, fn := range h.opts.LiveChecks {
|
|
|
|
if err = fn(ctx); err != nil {
|
|
|
|
return errors.ServiceUnavailable(h.opts.Name, "%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Ready(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error {
|
|
|
|
var err error
|
|
|
|
for _, fn := range h.opts.ReadyChecks {
|
|
|
|
if err = fn(ctx); err != nil {
|
|
|
|
return errors.ServiceUnavailable(h.opts.Name, "%v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Version(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error {
|
|
|
|
rsp.Data = []byte(h.opts.Version)
|
|
|
|
return nil
|
|
|
|
}
|