Skip to content

Commit

Permalink
split out handler creation
Browse files Browse the repository at this point in the history
  • Loading branch information
silvestre committed Oct 30, 2023
1 parent 2c0fa24 commit eec2bd5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
18 changes: 16 additions & 2 deletions src/autoscaler/api/publicapiserver/public_api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ 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"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/api/config"
"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"

Expand All @@ -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)
Expand Down Expand Up @@ -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
}
11 changes: 1 addition & 10 deletions src/autoscaler/api/publicapiserver/scaling_history_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

var (
_ = scalinghistory.Handler(&ScalingHistoryHandler{})
_ = scalinghistory.SecurityHandler(&ScalingHistoryHandler{})
_ = scalinghistory.SecuritySource(&ScalingHistoryHandler{})
)
Expand All @@ -21,7 +20,6 @@ type ScalingHistoryHandler struct {
logger lager.Logger
conf *config.Config
scalingEngineClient *http.Client
server *scalinghistory.Server
client *scalinghistory.Client
}

Expand All @@ -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 {
Expand All @@ -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)
Expand Down
13 changes: 1 addition & 12 deletions src/autoscaler/scalingengine/server/scaling_history_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package server
import (
"context"
"errors"
"fmt"
"math"
"net/url"
"strconv"
Expand All @@ -22,31 +21,21 @@ 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) {
newHandler := &ScalingHistoryHandler{
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 {
Expand Down
15 changes: 14 additions & 1 deletion src/autoscaler/scalingengine/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit eec2bd5

Please sign in to comment.