2019-11-14 15:40:05 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type serviceManager interface {
|
2020-09-01 16:16:04 +00:00
|
|
|
BuildHTTP(rootCtx context.Context, serviceName string) (http.Handler, error)
|
2022-11-16 10:38:07 +00:00
|
|
|
LaunchHealthCheck(ctx context.Context)
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InternalHandlers is the internal HTTP handlers builder.
|
|
|
|
type InternalHandlers struct {
|
|
|
|
api http.Handler
|
|
|
|
dashboard http.Handler
|
|
|
|
rest http.Handler
|
|
|
|
prometheus http.Handler
|
|
|
|
ping http.Handler
|
2020-10-29 14:40:04 +00:00
|
|
|
acmeHTTP http.Handler
|
2019-11-14 15:40:05 +00:00
|
|
|
serviceManager
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInternalHandlers creates a new InternalHandlers.
|
2020-10-29 14:40:04 +00:00
|
|
|
func NewInternalHandlers(next serviceManager, apiHandler, rest, metricsHandler, pingHandler, dashboard, acmeHTTP http.Handler) *InternalHandlers {
|
2019-11-14 15:40:05 +00:00
|
|
|
return &InternalHandlers{
|
|
|
|
api: apiHandler,
|
|
|
|
dashboard: dashboard,
|
|
|
|
rest: rest,
|
|
|
|
prometheus: metricsHandler,
|
|
|
|
ping: pingHandler,
|
2020-10-29 14:40:04 +00:00
|
|
|
acmeHTTP: acmeHTTP,
|
2019-11-14 15:40:05 +00:00
|
|
|
serviceManager: next,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildHTTP builds an HTTP handler.
|
2020-09-01 16:16:04 +00:00
|
|
|
func (m *InternalHandlers) BuildHTTP(rootCtx context.Context, serviceName string) (http.Handler, error) {
|
2020-06-15 10:20:05 +00:00
|
|
|
if !strings.HasSuffix(serviceName, "@internal") {
|
2020-09-01 16:16:04 +00:00
|
|
|
return m.serviceManager.BuildHTTP(rootCtx, serviceName)
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 10:20:05 +00:00
|
|
|
internalHandler, err := m.get(serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-01 16:16:04 +00:00
|
|
|
|
|
|
|
return internalHandler, nil
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *InternalHandlers) get(serviceName string) (http.Handler, error) {
|
|
|
|
switch serviceName {
|
2020-03-05 11:46:05 +00:00
|
|
|
case "noop@internal":
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusTeapot)
|
|
|
|
}), nil
|
|
|
|
|
2020-10-29 14:40:04 +00:00
|
|
|
case "acme-http@internal":
|
|
|
|
if m.acmeHTTP == nil {
|
|
|
|
return nil, errors.New("HTTP challenge is not enabled")
|
|
|
|
}
|
|
|
|
return m.acmeHTTP, nil
|
|
|
|
|
2019-11-14 15:40:05 +00:00
|
|
|
case "api@internal":
|
|
|
|
if m.api == nil {
|
|
|
|
return nil, errors.New("api is not enabled")
|
|
|
|
}
|
|
|
|
return m.api, nil
|
|
|
|
|
|
|
|
case "dashboard@internal":
|
|
|
|
if m.dashboard == nil {
|
|
|
|
return nil, errors.New("dashboard is not enabled")
|
|
|
|
}
|
|
|
|
return m.dashboard, nil
|
|
|
|
|
|
|
|
case "rest@internal":
|
|
|
|
if m.rest == nil {
|
|
|
|
return nil, errors.New("rest is not enabled")
|
|
|
|
}
|
|
|
|
return m.rest, nil
|
|
|
|
|
|
|
|
case "ping@internal":
|
|
|
|
if m.ping == nil {
|
|
|
|
return nil, errors.New("ping is not enabled")
|
|
|
|
}
|
|
|
|
return m.ping, nil
|
|
|
|
|
|
|
|
case "prometheus@internal":
|
|
|
|
if m.prometheus == nil {
|
|
|
|
return nil, errors.New("prometheus is not enabled")
|
|
|
|
}
|
|
|
|
return m.prometheus, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown internal service %s", serviceName)
|
|
|
|
}
|
|
|
|
}
|