Skip to content

Commit fb1740a

Browse files
committed
remove gorilla/mux dependency
1 parent ba82c45 commit fb1740a

File tree

7 files changed

+20
-35
lines changed

7 files changed

+20
-35
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/gofrs/uuid/v5 v5.3.2
88
github.com/golang-migrate/migrate/v4 v4.18.2
99
github.com/gophercloud/gophercloud/v2 v2.6.0
10-
github.com/gorilla/mux v1.8.1
1110
github.com/hashicorp/golang-lru/v2 v2.0.7
1211
github.com/hashicorp/vault/api v1.16.0
1312
github.com/lib/pq v1.10.9

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX
4949
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
5050
github.com/gophercloud/gophercloud/v2 v2.6.0 h1:XJKQ0in3iHOZHVAFMXq/OhjCuvvG+BKR0unOqRfG1EI=
5151
github.com/gophercloud/gophercloud/v2 v2.6.0/go.mod h1:Ki/ILhYZr/5EPebrPL9Ej+tUg4lqx71/YH2JWVeU+Qk=
52-
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
53-
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
5452
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
5553
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
5654
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=

httpapi/api.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ package httpapi
2020

2121
import (
2222
"net/http"
23-
24-
"github.com/gorilla/mux"
2523
)
2624

2725
// API is the interface that applications can use to plug their own API
@@ -32,7 +30,7 @@ import (
3230
// "Without..." are available that apply to the entire http.Handler returned by
3331
// Compose(), instead of just adding endpoints to it.
3432
type API interface {
35-
AddTo(r *mux.Router)
33+
AddTo(r *http.ServeMux)
3634
}
3735

3836
// HealthCheckAPI is an API with one endpoint, "GET /healthcheck", that
@@ -46,8 +44,8 @@ type HealthCheckAPI struct {
4644
}
4745

4846
// AddTo implements the API interface.
49-
func (h HealthCheckAPI) AddTo(r *mux.Router) {
50-
r.Methods("GET", "HEAD").Path("/healthcheck").HandlerFunc(h.handleRequest)
47+
func (h HealthCheckAPI) AddTo(s *http.ServeMux) {
48+
s.HandleFunc("GET /healthcheck", h.handleRequest)
5149
}
5250

5351
func (h HealthCheckAPI) handleRequest(w http.ResponseWriter, r *http.Request) {
@@ -74,7 +72,7 @@ type pseudoAPI struct {
7472
configure func(*middleware)
7573
}
7674

77-
func (p pseudoAPI) AddTo(r *mux.Router) {
75+
func (p pseudoAPI) AddTo(r *http.ServeMux) {
7876
// no-op, see above
7977
}
8078

httpapi/compose.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ package httpapi
2020

2121
import (
2222
"net/http"
23-
24-
"github.com/gorilla/mux"
2523
)
2624

2725
// Compose constructs an http.Handler serving all the provided APIs. The Handler
@@ -30,7 +28,7 @@ import (
3028
func Compose(apis ...API) http.Handler {
3129
autoConfigureMetricsIfNecessary()
3230

33-
r := mux.NewRouter()
31+
r := http.NewServeMux()
3432
m := middleware{inner: r}
3533

3634
for _, a := range apis {

httpapi/httpapi_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"testing"
3333
"time"
3434

35-
"github.com/gorilla/mux"
3635
"github.com/prometheus/client_golang/prometheus"
3736
"github.com/prometheus/client_golang/prometheus/promhttp"
3837

@@ -199,21 +198,19 @@ func TestMetrics(t *testing.T) {
199198

200199
type metricsTestingAPI struct{}
201200

202-
func (m metricsTestingAPI) AddTo(r *mux.Router) {
203-
r.Methods("POST").Path("/sleep/{secs}/return/{count}").HandlerFunc(m.handleRequest)
201+
func (m metricsTestingAPI) AddTo(s *http.ServeMux) {
202+
s.HandleFunc("POST /sleep/{secs}/return/{count}", m.handleRequest)
204203
}
205204

206205
func (m metricsTestingAPI) handleRequest(w http.ResponseWriter, r *http.Request) {
207-
vars := mux.Vars(r)
208-
209-
secs, err := strconv.ParseFloat(vars["secs"], 64)
206+
secs, err := strconv.ParseFloat(r.PathValue("secs"), 64)
210207
if respondwith.ErrorText(w, err) {
211208
return
212209
}
213210
//NOTE: `time.Duration(secs)` does not work because all values < 1 would all be truncated to 0.
214211
time.Sleep(time.Duration(secs * float64(time.Second)))
215212

216-
count, err := strconv.Atoi(vars["count"])
213+
count, err := strconv.Atoi(r.PathValue("count"))
217214
if respondwith.ErrorText(w, err) {
218215
return
219216
}

httpapi/pprofapi/pprofapi.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import (
3030
"os"
3131
"strconv"
3232

33-
"github.com/gorilla/mux"
34-
3533
"github.com/sapcc/go-bits/httpapi"
3634
"github.com/sapcc/go-bits/httpext"
3735
"github.com/sapcc/go-bits/logg"
@@ -49,12 +47,12 @@ type API struct {
4947
}
5048

5149
// AddTo implements the httpapi.API interface.
52-
func (a API) AddTo(r *mux.Router) {
50+
func (a API) AddTo(s *http.ServeMux) {
5351
if a.IsAuthorized == nil {
5452
panic("API.AddTo() called with IsAuthorized == nil!")
5553
}
5654

57-
r.Methods("GET").Path("/debug/pprof/{operation}").HandlerFunc(a.handler)
55+
s.HandleFunc("GET /debug/pprof/{operation}", a.handler)
5856
}
5957

6058
func (a API) handler(w http.ResponseWriter, r *http.Request) {
@@ -65,7 +63,7 @@ func (a API) handler(w http.ResponseWriter, r *http.Request) {
6563
return
6664
}
6765

68-
switch mux.Vars(r)["operation"] {
66+
switch r.PathValue("operation") {
6967
default:
7068
pprof.Index(w, r)
7169
case "cmdline":

liquidapi/liquidapi.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131

3232
"github.com/gophercloud/gophercloud/v2"
3333
"github.com/gophercloud/gophercloud/v2/openstack"
34-
"github.com/gorilla/mux"
3534
"github.com/prometheus/client_golang/prometheus/promhttp"
3635
"github.com/sapcc/go-api-declarations/liquid"
3736

@@ -289,11 +288,11 @@ func limitRequestsMiddleware(maxRequests int) func(http.Handler) http.Handler {
289288
}
290289

291290
// AddTo implements the httpapi.API interface.
292-
func (rt *runtime) AddTo(r *mux.Router) {
293-
r.Methods("GET").Path("/v1/info").HandlerFunc(rt.handleGetInfo)
294-
r.Methods("POST").Path("/v1/report-capacity").HandlerFunc(rt.handleReportCapacity)
295-
r.Methods("POST").Path("/v1/projects/{project_id}/report-usage").HandlerFunc(rt.handleReportUsage)
296-
r.Methods("PUT").Path("/v1/projects/{project_id}/quota").HandlerFunc(rt.handleSetQuota)
291+
func (rt *runtime) AddTo(s *http.ServeMux) {
292+
s.HandleFunc("GET /v1/info", rt.handleGetInfo)
293+
s.HandleFunc("POST /v1/report-capacity", rt.handleReportCapacity)
294+
s.HandleFunc("POST /v1/projects/{project_id}/report-usage", rt.handleReportUsage)
295+
s.HandleFunc("PUT /v1/projects/{project_id}/quota", rt.handleSetQuota)
297296
}
298297

299298
func (rt *runtime) handleGetInfo(w http.ResponseWriter, r *http.Request) {
@@ -328,14 +327,13 @@ func (rt *runtime) handleReportUsage(w http.ResponseWriter, r *http.Request) {
328327
if !rt.requireToken(w, r, "liquid:get_usage") {
329328
return
330329
}
331-
vars := mux.Vars(r)
332330

333331
var req liquid.ServiceUsageRequest
334332
if !requireJSON(w, r, &req) {
335333
return
336334
}
337335

338-
resp, err := rt.Logic.ScanUsage(r.Context(), vars["project_id"], req, rt.getServiceInfo())
336+
resp, err := rt.Logic.ScanUsage(r.Context(), r.PathValue("project_id"), req, rt.getServiceInfo())
339337
if respondwith.ErrorText(w, err) {
340338
return
341339
}
@@ -347,14 +345,13 @@ func (rt *runtime) handleSetQuota(w http.ResponseWriter, r *http.Request) {
347345
if !rt.requireToken(w, r, "liquid:set_quota") {
348346
return
349347
}
350-
vars := mux.Vars(r)
351348

352349
var req liquid.ServiceQuotaRequest
353350
if !requireJSON(w, r, &req) {
354351
return
355352
}
356353

357-
err := rt.Logic.SetQuota(r.Context(), vars["project_id"], req, rt.getServiceInfo())
354+
err := rt.Logic.SetQuota(r.Context(), r.PathValue("project_id"), req, rt.getServiceInfo())
358355
if respondwith.ErrorText(w, err) {
359356
return
360357
}
@@ -363,7 +360,7 @@ func (rt *runtime) handleSetQuota(w http.ResponseWriter, r *http.Request) {
363360

364361
func (rt *runtime) requireToken(w http.ResponseWriter, r *http.Request, policyRule string) bool {
365362
t := rt.TokenValidator.CheckToken(r)
366-
t.Context.Request = mux.Vars(r)
363+
t.Context.LookupRequestValue = r.PathValue
367364
return t.Require(w, policyRule)
368365
}
369366

0 commit comments

Comments
 (0)