diff --git a/src/autoscaler/api/publicapiserver/public_api_server.go b/src/autoscaler/api/publicapiserver/public_api_server.go index 6114b1f8e9..a64454fed8 100644 --- a/src/autoscaler/api/publicapiserver/public_api_server.go +++ b/src/autoscaler/api/publicapiserver/public_api_server.go @@ -6,6 +6,7 @@ import ( "os" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/cred_helper" + "code.cloudfoundry.org/app-autoscaler/src/autoscaler/helpers/apis/scalinghistory" "go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/api" @@ -13,7 +14,6 @@ import ( "code.cloudfoundry.org/app-autoscaler/src/autoscaler/cf" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/db" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/healthendpoint" - _ "code.cloudfoundry.org/app-autoscaler/src/autoscaler/helpers/apis/scalinghistory" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/ratelimiter" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/routes" @@ -34,10 +34,12 @@ func NewPublicApiServer(logger lager.Logger, conf *config.Config, policydb db.Po checkBindingFunc api.CheckBindingFunc, cfclient cf.CFClient, httpStatusCollector healthendpoint.HTTPStatusCollector, rateLimiter ratelimiter.Limiter, bindingdb db.BindingDB) (ifrit.Runner, error) { pah := NewPublicApiHandler(logger, conf, policydb, bindingdb, credentials) - scalingHistoryHandler, err := NewScalingHistoryHandler(logger, conf) + + scalingHistoryHandler, err := newScalingHistoryHandler(logger, conf) if err != nil { return nil, err } + mw := NewMiddleware(logger, cfclient, checkBindingFunc, conf.APIClientId) rateLimiterMiddleware := ratelimiter.NewRateLimiterMiddleware("appId", rateLimiter, logger.Session("api-ratelimiter-middleware")) httpStatusCollectMiddleware := healthendpoint.NewHTTPStatusCollectMiddleware(httpStatusCollector) @@ -103,3 +105,15 @@ func NewPublicApiServer(logger lager.Logger, conf *config.Config, policydb db.Po logger.Info("public-api-http-server-created", lager.Data{"serverConfig": conf.PublicApiServer}) return runner, nil } + +func newScalingHistoryHandler(logger lager.Logger, conf *config.Config) (http.Handler, error) { + scalingHistoryHandler, err := NewScalingHistoryHandler(logger, conf) + if err != nil { + return nil, fmt.Errorf("error creating scaling history handler: %w", err) + } + scalingHistoryServer, err := scalinghistory.NewServer(scalingHistoryHandler, scalingHistoryHandler) + if err != nil { + return nil, fmt.Errorf("error creating ogen scaling history server: %w", err) + } + return scalingHistoryServer, nil +} diff --git a/src/autoscaler/api/publicapiserver/scaling_history_handler.go b/src/autoscaler/api/publicapiserver/scaling_history_handler.go index e20f9dfc89..5ac2af5a80 100644 --- a/src/autoscaler/api/publicapiserver/scaling_history_handler.go +++ b/src/autoscaler/api/publicapiserver/scaling_history_handler.go @@ -12,7 +12,6 @@ import ( ) var ( - _ = scalinghistory.Handler(&ScalingHistoryHandler{}) _ = scalinghistory.SecurityHandler(&ScalingHistoryHandler{}) _ = scalinghistory.SecuritySource(&ScalingHistoryHandler{}) ) @@ -21,7 +20,6 @@ type ScalingHistoryHandler struct { logger lager.Logger conf *config.Config scalingEngineClient *http.Client - server *scalinghistory.Server client *scalinghistory.Client } @@ -36,11 +34,7 @@ func NewScalingHistoryHandler(logger lager.Logger, conf *config.Config) (*Scalin conf: conf, scalingEngineClient: seClient, } - if server, err := scalinghistory.NewServer(newHandler, newHandler); err != nil { - return nil, fmt.Errorf("error creating ogen scaling history server: %w", err) - } else { - newHandler.server = server - } + if client, err := scalinghistory.NewClient(conf.ScalingEngine.ScalingEngineUrl, newHandler, scalinghistory.WithClient(seClient)); err != nil { return nil, fmt.Errorf("error creating ogen scaling history client: %w", err) } else { @@ -50,9 +44,6 @@ func NewScalingHistoryHandler(logger lager.Logger, conf *config.Config) (*Scalin return newHandler, nil } -func (h *ScalingHistoryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - h.server.ServeHTTP(w, r) -} func (h *ScalingHistoryHandler) NewError(_ context.Context, _ error) *scalinghistory.ErrorResponseStatusCode { result := &scalinghistory.ErrorResponseStatusCode{} result.SetStatusCode(500) diff --git a/src/autoscaler/scalingengine/server/scaling_history_handler.go b/src/autoscaler/scalingengine/server/scaling_history_handler.go index 96c6564b7d..35dfd70511 100644 --- a/src/autoscaler/scalingengine/server/scaling_history_handler.go +++ b/src/autoscaler/scalingengine/server/scaling_history_handler.go @@ -3,7 +3,6 @@ package server import ( "context" "errors" - "fmt" "math" "net/url" "strconv" @@ -22,14 +21,12 @@ import ( ) var ( - _ = scalinghistory.Handler(&ScalingHistoryHandler{}) _ = scalinghistory.SecurityHandler(&ScalingHistoryHandler{}) ) type ScalingHistoryHandler struct { logger lager.Logger scalingEngineDB db.ScalingEngineDB - server *scalinghistory.Server } func NewScalingHistoryHandler(logger lager.Logger, scalingEngineDB db.ScalingEngineDB) (*ScalingHistoryHandler, error) { @@ -37,16 +34,8 @@ func NewScalingHistoryHandler(logger lager.Logger, scalingEngineDB db.ScalingEng logger: logger.Session("scaling-history-handler"), scalingEngineDB: scalingEngineDB, } - if server, err := scalinghistory.NewServer(newHandler, newHandler); err != nil { - return nil, fmt.Errorf("error creating ogen scaling history server: %w", err) - } else { - newHandler.server = server - } - return newHandler, nil -} -func (h *ScalingHistoryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - h.server.ServeHTTP(w, r) + return newHandler, nil } func (h *ScalingHistoryHandler) NewError(_ context.Context, err error) *scalinghistory.ErrorResponseStatusCode { diff --git a/src/autoscaler/scalingengine/server/server.go b/src/autoscaler/scalingengine/server/server.go index 256a602ab9..e424c36063 100644 --- a/src/autoscaler/scalingengine/server/server.go +++ b/src/autoscaler/scalingengine/server/server.go @@ -5,6 +5,7 @@ import ( "code.cloudfoundry.org/app-autoscaler/src/autoscaler/db" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/healthendpoint" + "code.cloudfoundry.org/app-autoscaler/src/autoscaler/helpers/apis/scalinghistory" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/routes" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/scalingengine" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/scalingengine/config" @@ -37,7 +38,7 @@ func NewServer(logger lager.Logger, conf *config.Config, scalingEngineDB db.Scal r.Use(httpStatusCollectMiddleware.Collect) r.Get(routes.ScaleRouteName).Handler(VarsFunc(handler.Scale)) - scalingHistoryHandler, err := NewScalingHistoryHandler(logger, scalingEngineDB) + scalingHistoryHandler, err := newScalingHistoryHandler(logger, scalingEngineDB) if err != nil { return nil, err } @@ -69,3 +70,15 @@ func NewServer(logger lager.Logger, conf *config.Config, scalingEngineDB db.Scal return http_server.New(addr, r), nil } + +func newScalingHistoryHandler(logger lager.Logger, scalingEngineDB db.ScalingEngineDB) (http.Handler, error) { + scalingHistoryHandler, err := NewScalingHistoryHandler(logger, scalingEngineDB) + if err != nil { + return nil, fmt.Errorf("error creating scaling history handler: %w", err) + } + server, err := scalinghistory.NewServer(scalingHistoryHandler, scalingHistoryHandler) + if err != nil { + return nil, fmt.Errorf("error creating ogen scaling history server: %w", err) + } + return server, err +}