Skip to content

Commit

Permalink
Replace gorilla/mux with go-chi/chi in broker server
Browse files Browse the repository at this point in the history
  • Loading branch information
silvestre committed Jul 11, 2023
1 parent fcbab32 commit 15481c1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
15 changes: 8 additions & 7 deletions packages/golangapiserver/spec
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ files:
- autoscaler/vendor/code.cloudfoundry.org/tlsconfig/* # gosub
- autoscaler/vendor/github.com/beorn7/perks/quantile/* # gosub
- autoscaler/vendor/github.com/cespare/xxhash/v2/* # gosub
- autoscaler/vendor/github.com/go-chi/chi/v5/* # gosub
- autoscaler/vendor/github.com/go-sql-driver/mysql/* # gosub
- autoscaler/vendor/github.com/golang/protobuf/proto/* # gosub
- autoscaler/vendor/github.com/golang/protobuf/ptypes/timestamp/* # gosub
Expand Down Expand Up @@ -69,13 +70,13 @@ files:
- autoscaler/vendor/github.com/openzipkin/zipkin-go/model/* # gosub
- autoscaler/vendor/github.com/patrickmn/go-cache/* # gosub
- autoscaler/vendor/github.com/pborman/uuid/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v9/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v9/auth/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v9/domain/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v9/domain/apiresponses/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v9/handlers/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v9/middlewares/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v9/utils/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v10/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v10/auth/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v10/domain/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v10/domain/apiresponses/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v10/handlers/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v10/middlewares/* # gosub
- autoscaler/vendor/github.com/pivotal-cf/brokerapi/v10/utils/* # gosub
- autoscaler/vendor/github.com/pkg/errors/* # gosub
- autoscaler/vendor/github.com/prometheus/client_golang/prometheus/* # gosub
- autoscaler/vendor/github.com/prometheus/client_golang/prometheus/collectors/* # gosub
Expand Down
10 changes: 5 additions & 5 deletions src/autoscaler/api/brokerserver/broker_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"net/url"

"code.cloudfoundry.org/app-autoscaler/src/autoscaler/api/broker"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/testhelpers"

"github.com/gorilla/mux"
"github.com/pivotal-cf/brokerapi/v10/handlers"

"code.cloudfoundry.org/app-autoscaler/src/autoscaler/db"
Expand Down Expand Up @@ -76,7 +76,7 @@ var _ = Describe("BrokerHandler", func() {
var body []byte
JustBeforeEach(func() {
req, err = http.NewRequest(http.MethodPut, "", bytes.NewReader(body))
req = mux.SetURLVars(req, map[string]string{"instance_id": testInstanceId})
req = testhelpers.SetURLParams(req, "instance_id", testInstanceId)
handler.Provision(resp, req)
})
BeforeEach(func() {
Expand Down Expand Up @@ -300,7 +300,7 @@ var _ = Describe("BrokerHandler", func() {
callUpdateServiceInstance := func() {
req, err = http.NewRequest(http.MethodPut, "", bytes.NewReader(body))
Expect(err).NotTo(HaveOccurred())
req = mux.SetURLVars(req, map[string]string{"instance_id": testInstanceId})
req = testhelpers.SetURLParams(req, "instance_id", testInstanceId)
handler.Update(resp, req)
}
updatePlanAndDefaultPolicy := func(fromPlan string, targetPlan string, defaultPolicy json.RawMessage) {
Expand Down Expand Up @@ -693,7 +693,7 @@ var _ = Describe("BrokerHandler", func() {
Describe("DeleteServiceInstance", func() {
JustBeforeEach(func() {
req, _ = http.NewRequest(http.MethodDelete, "", nil)
req = mux.SetURLVars(req, map[string]string{"instance_id": testInstanceId})
req = testhelpers.SetURLParams(req, "instance_id", testInstanceId)
values := url.Values{}
values.Set("service_id", "autoscaler-guid")
values.Set("plan_id", "autoscaler-free-plan-id")
Expand Down Expand Up @@ -1100,7 +1100,7 @@ var _ = Describe("BrokerHandler", func() {
Describe("UnBindServiceInstance", func() {
BeforeEach(func() {
req, _ = http.NewRequest(http.MethodDelete, "", nil)
req = mux.SetURLVars(req, map[string]string{"instance_id": testInstanceId, "binding_id": testBindingId})
req = testhelpers.SetURLParams(req, "instance_id", testInstanceId, "binding_id", testBindingId)
values := url.Values{}
values.Set("service_id", "autoscaler-guid")
values.Set("plan_id", "autoscaler-free-plan-id")
Expand Down
11 changes: 2 additions & 9 deletions src/autoscaler/api/brokerserver/broker_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,12 @@ import (
"github.com/pivotal-cf/brokerapi/v10/domain"

"code.cloudfoundry.org/lager/v3"
"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
"github.com/tedsuo/ifrit"
"github.com/tedsuo/ifrit/http_server"
"golang.org/x/crypto/bcrypt"
)

type VarsFunc func(w http.ResponseWriter, r *http.Request, vars map[string]string)

func (vh VarsFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
vh(w, r, vars)
}

type MiddleWareBrokerCredentials struct {
BrokerUsername string
BrokerUsernameHash []byte
Expand Down Expand Up @@ -123,7 +116,7 @@ func NewBrokerServer(logger lager.Logger, conf *config.Config, bindingdb db.Bind
httpStatusCollectMiddleware := healthendpoint.NewHTTPStatusCollectMiddleware(httpStatusCollector)
autoscalerBroker := broker.New(logger.Session("broker"), conf, bindingdb, policydb, catalog.Services, credentials)

r := mux.NewRouter()
r := chi.NewRouter()

r.Use(basicAuthentication.Middleware)
r.Use(httpStatusCollectMiddleware.Collect)
Expand Down
2 changes: 1 addition & 1 deletion src/autoscaler/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
code.cloudfoundry.org/tlsconfig v0.0.0-20220621140725-0e6fbd869921
dario.cat/mergo v1.0.0
github.com/cenkalti/backoff/v4 v4.2.1
github.com/go-chi/chi/v5 v5.0.8
github.com/go-sql-driver/mysql v1.7.1
github.com/golang/protobuf v1.5.3
github.com/golangci/golangci-lint v1.53.3
Expand Down Expand Up @@ -86,7 +87,6 @@ require (
github.com/firefart/nonamedreturns v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/fzipp/gocyclo v0.6.0 // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/go-critic/go-critic v0.8.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
Expand Down
18 changes: 18 additions & 0 deletions src/autoscaler/testhelpers/chi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package testhelpers

import (
"context"
"net/http"

"github.com/go-chi/chi/v5"
)

func SetURLParams(req *http.Request, params ...string) *http.Request {
rc := chi.NewRouteContext()
for i := 0; i+1 < len(params); i += 2 {
rc.URLParams.Add(params[i], params[i+1])
}
ctx := context.WithValue(req.Context(), chi.RouteCtxKey, rc)
req = req.WithContext(ctx)
return req
}

0 comments on commit 15481c1

Please sign in to comment.