Skip to content

Commit

Permalink
Merge pull request #1943 from k6io/dropNegroni
Browse files Browse the repository at this point in the history
Drop github.com/urfave/negroni as a dependency
  • Loading branch information
mstoykov authored Apr 2, 2021
2 parents abce5d8 + f84bb8b commit 77c22e2
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 1,345 deletions.
33 changes: 18 additions & 15 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"net/http"

"github.com/sirupsen/logrus"
"github.com/urfave/negroni"

"github.com/loadimpact/k6/api/common"
v1 "github.com/loadimpact/k6/api/v1"
Expand All @@ -44,29 +43,33 @@ func newHandler(logger logrus.FieldLogger) http.Handler {
func ListenAndServe(addr string, engine *core.Engine, logger logrus.FieldLogger) error {
mux := newHandler(logger)

n := negroni.New()
n.Use(negroni.NewRecovery())
n.UseFunc(withEngine(engine))
n.UseFunc(newLogger(logger))
n.UseHandler(mux)
return http.ListenAndServe(addr, withEngine(engine, newLogger(logger, mux)))
}

type wrappedResponseWriter struct {
http.ResponseWriter
status int
}

return http.ListenAndServe(addr, n)
func (w wrappedResponseWriter) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}

// newLogger returns the middleware which logs response status for request.
func newLogger(l logrus.FieldLogger) negroni.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
next(rw, r)
func newLogger(l logrus.FieldLogger, next http.Handler) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
wrapped := wrappedResponseWriter{ResponseWriter: rw, status: 200} // The default status code is 200 if it's not set
next.ServeHTTP(wrapped, r)

res := rw.(negroni.ResponseWriter)
l.WithField("status", res.Status()).Debugf("%s %s", r.Method, r.URL.Path)
l.WithField("status", wrapped.status).Debugf("%s %s", r.Method, r.URL.Path)
}
}

func withEngine(engine *core.Engine) negroni.HandlerFunc {
return negroni.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
func withEngine(engine *core.Engine, next http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
r = r.WithContext(common.WithEngine(r.Context(), engine))
next(rw, r)
next.ServeHTTP(rw, r)
})
}

Expand Down
7 changes: 3 additions & 4 deletions api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
logtest "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/negroni"

"github.com/loadimpact/k6/api/common"
"github.com/loadimpact/k6/core"
Expand All @@ -57,7 +56,7 @@ func TestLogger(t *testing.T) {

l, hook := logtest.NewNullLogger()
l.Level = logrus.DebugLevel
newLogger(l)(negroni.NewResponseWriter(rw), r, testHTTPHandler)
newLogger(l, http.HandlerFunc(testHTTPHandler))(rw, r)

res := rw.Result()
assert.Equal(t, http.StatusOK, res.StatusCode)
Expand Down Expand Up @@ -87,9 +86,9 @@ func TestWithEngine(t *testing.T) {

rw := httptest.NewRecorder()
r := httptest.NewRequest("GET", "http://example.com/", nil)
withEngine(engine)(rw, r, func(rw http.ResponseWriter, r *http.Request) {
withEngine(engine, http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
assert.Equal(t, engine, common.GetEngine(r.Context()))
})
}))(rw, r)
}

func TestPing(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ require (
github.com/tidwall/gjson v1.7.4
github.com/tidwall/pretty v1.1.0
github.com/ugorji/go v1.1.7 // indirect
github.com/urfave/negroni v0.3.1-0.20180130044549-22c5532ea862
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 // indirect
github.com/zyedidia/highlight v0.0.0-20170330143449-201131ce5cf5
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/urfave/negroni v0.3.1-0.20180130044549-22c5532ea862 h1:o3gEt3MZ4RRJUzO8qtPd31kJYU3m/ga9re5xbjVFUvA=
github.com/urfave/negroni v0.3.1-0.20180130044549-22c5532ea862/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 h1:gKMu1Bf6QINDnvyZuTaACm9ofY+PRh+5vFz4oxBZeF8=
Expand Down
1 change: 0 additions & 1 deletion vendor/github.com/urfave/negroni/.gitignore

This file was deleted.

64 changes: 0 additions & 64 deletions vendor/github.com/urfave/negroni/CHANGELOG.md

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/github.com/urfave/negroni/LICENSE

This file was deleted.

Loading

0 comments on commit 77c22e2

Please sign in to comment.