Skip to content

Commit f04f24a

Browse files
authored
Merge pull request #193 from google/gorrila/mux/gorrila/mux
Support for gorrila/mux framework
2 parents 743b18c + a85954e commit f04f24a

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

go/gorrila/mux/go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/google/sqlcommenter/go/gorrila/mux
2+
3+
go 1.19
4+
5+
require (
6+
github.com/google/sqlcommenter/go/core v0.0.5-beta
7+
github.com/google/sqlcommenter/go/net/http v0.0.3-beta
8+
github.com/gorilla/mux v1.8.0
9+
)
10+
11+
require (
12+
go.opentelemetry.io/otel v1.11.1 // indirect
13+
go.opentelemetry.io/otel/trace v1.11.1 // indirect
14+
)

go/gorrila/mux/go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
3+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
4+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
5+
github.com/google/sqlcommenter/go/core v0.0.5-beta h1:axqYR1zQCCdRBLnwr/j+ckllBSBJ7uaVdsnANuGzCUI=
6+
github.com/google/sqlcommenter/go/core v0.0.5-beta/go.mod h1:GORu2htXRC4xtejBzOa4ct1L20pohP81DFNYKdCJI70=
7+
github.com/google/sqlcommenter/go/net/http v0.0.3-beta h1:IE/vO3xKddn/2Bq3k+hSy4CxcEuvE1lUiIDYTXjApzA=
8+
github.com/google/sqlcommenter/go/net/http v0.0.3-beta/go.mod h1:duXQQvXZYCX8eQ+XOrlojWF512ltEp1eSKXc/KiS9lg=
9+
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
10+
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
13+
go.opentelemetry.io/otel v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4=
14+
go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE=
15+
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ=
16+
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk=
17+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

go/gorrila/mux/mux.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package mux
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/google/sqlcommenter/go/core"
7+
httpnet "github.com/google/sqlcommenter/go/net/http"
8+
"github.com/gorilla/mux"
9+
)
10+
11+
func SQLCommenterMiddleware(h http.Handler) http.Handler {
12+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
13+
route := mux.CurrentRoute(r)
14+
pathTemplate, err := route.GetPathTemplate()
15+
if err != nil {
16+
pathTemplate = ""
17+
}
18+
19+
ctx := core.ContextInject(r.Context(), httpnet.NewHTTPRequestTags("gorrila/mux", pathTemplate, core.GetFunctionName(route.GetHandler())))
20+
h.ServeHTTP(w, r.WithContext(ctx))
21+
})
22+
}

go/gorrila/mux/mux_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package mux
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/google/sqlcommenter/go/core"
9+
"github.com/gorilla/mux"
10+
)
11+
12+
func TestSQLCommenterMiddleware(t *testing.T) {
13+
framework := "gorrila/mux"
14+
route := "/test/{id}"
15+
action := "github.com/google/sqlcommenter/go/gorrila/mux.TestSQLCommenterMiddleware.func1"
16+
17+
mockHandler := func(w http.ResponseWriter, r *http.Request) {
18+
ctx := r.Context()
19+
_framework := ctx.Value(core.Framework)
20+
_route := ctx.Value(core.Route)
21+
_action := ctx.Value(core.Action)
22+
23+
if _framework != framework {
24+
t.Errorf("mismatched framework - got: %s, want: %s", _framework, framework)
25+
}
26+
27+
if _route != route {
28+
t.Errorf("mismatched route - got: %s, want: %s", _route, route)
29+
}
30+
31+
if _action != action {
32+
t.Errorf("mismatched action - got: %s, want: %s", _action, action)
33+
}
34+
}
35+
36+
router := mux.NewRouter()
37+
router.Use(SQLCommenterMiddleware)
38+
router.HandleFunc(route, mockHandler).Methods("GET")
39+
40+
rr := httptest.NewRecorder()
41+
req, err := http.NewRequest("GET", "/test/1", nil)
42+
43+
if err != nil {
44+
t.Errorf("error while building req: %v", err)
45+
}
46+
47+
router.ServeHTTP(rr, req)
48+
}

0 commit comments

Comments
 (0)