-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from google/gorrila/mux/gorrila/mux
Support for gorrila/mux framework
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module github.com/google/sqlcommenter/go/gorrila/mux | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/google/sqlcommenter/go/core v0.0.5-beta | ||
github.com/google/sqlcommenter/go/net/http v0.0.3-beta | ||
github.com/gorilla/mux v1.8.0 | ||
) | ||
|
||
require ( | ||
go.opentelemetry.io/otel v1.11.1 // indirect | ||
go.opentelemetry.io/otel/trace v1.11.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= | ||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/google/sqlcommenter/go/core v0.0.5-beta h1:axqYR1zQCCdRBLnwr/j+ckllBSBJ7uaVdsnANuGzCUI= | ||
github.com/google/sqlcommenter/go/core v0.0.5-beta/go.mod h1:GORu2htXRC4xtejBzOa4ct1L20pohP81DFNYKdCJI70= | ||
github.com/google/sqlcommenter/go/net/http v0.0.3-beta h1:IE/vO3xKddn/2Bq3k+hSy4CxcEuvE1lUiIDYTXjApzA= | ||
github.com/google/sqlcommenter/go/net/http v0.0.3-beta/go.mod h1:duXQQvXZYCX8eQ+XOrlojWF512ltEp1eSKXc/KiS9lg= | ||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= | ||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= | ||
go.opentelemetry.io/otel v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4= | ||
go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE= | ||
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ= | ||
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package mux | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/google/sqlcommenter/go/core" | ||
httpnet "github.com/google/sqlcommenter/go/net/http" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func SQLCommenterMiddleware(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
route := mux.CurrentRoute(r) | ||
pathTemplate, err := route.GetPathTemplate() | ||
if err != nil { | ||
pathTemplate = "" | ||
} | ||
|
||
ctx := core.ContextInject(r.Context(), httpnet.NewHTTPRequestTags("gorrila/mux", pathTemplate, core.GetFunctionName(route.GetHandler()))) | ||
h.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package mux | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/google/sqlcommenter/go/core" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
func TestSQLCommenterMiddleware(t *testing.T) { | ||
framework := "gorrila/mux" | ||
route := "/test/{id}" | ||
action := "github.com/google/sqlcommenter/go/gorrila/mux.TestSQLCommenterMiddleware.func1" | ||
|
||
mockHandler := func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
_framework := ctx.Value(core.Framework) | ||
_route := ctx.Value(core.Route) | ||
_action := ctx.Value(core.Action) | ||
|
||
if _framework != framework { | ||
t.Errorf("mismatched framework - got: %s, want: %s", _framework, framework) | ||
} | ||
|
||
if _route != route { | ||
t.Errorf("mismatched route - got: %s, want: %s", _route, route) | ||
} | ||
|
||
if _action != action { | ||
t.Errorf("mismatched action - got: %s, want: %s", _action, action) | ||
} | ||
} | ||
|
||
router := mux.NewRouter() | ||
router.Use(SQLCommenterMiddleware) | ||
router.HandleFunc(route, mockHandler).Methods("GET") | ||
|
||
rr := httptest.NewRecorder() | ||
req, err := http.NewRequest("GET", "/test/1", nil) | ||
|
||
if err != nil { | ||
t.Errorf("error while building req: %v", err) | ||
} | ||
|
||
router.ServeHTTP(rr, req) | ||
} |