Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix: azure ping not responding

goenninga1712e414d3b
diff --git a/app/cmd/routes.go b/app/cmd/routes.go
index c237ad314..073cdd648 100644
--- a/app/cmd/routes.go
+++ b/app/cmd/routes.go
@@ -22,7 +22,6 @@ func routes(r *web.Engine) *web.Engine {
mw := middlewares.Chain(
middlewares.WebSetup(),
middlewares.Tenant(),
- middlewares.Instrumentation(),
)
next := mw(func(c *web.Context) error {
return c.NotFound()
diff --git a/app/middlewares/chain.go b/app/middlewares/chain.go
index 4cea11764..b03fb83b6 100644
--- a/app/middlewares/chain.go
+++ b/app/middlewares/chain.go
@@ -9,7 +9,10 @@ func Chain(mws ...web.MiddlewareFunc) web.MiddlewareFunc {
return func(handler web.HandlerFunc) web.HandlerFunc {
next := handler
for i := len(mws) - 1; i >= 0; i-- {
- next = mws[i](next)
+ mw := mws[i]
+ if mw != nil {
+ next = mw(next)
+ }
}
return next
}
diff --git a/app/middlewares/chain_test.go b/app/middlewares/chain_test.go
new file mode 100644
index 000000000..c381f06f3
--- /dev/null
+++ b/app/middlewares/chain_test.go
@@ -0,0 +1,66 @@
+package middlewares_test
+
+import (
+ "net/http"
+ "testing"
+
+ "github.com/getfider/fider/app/middlewares"
+ . "github.com/getfider/fider/app/pkg/assert"
+ "github.com/getfider/fider/app/pkg/mock"
+ "github.com/getfider/fider/app/pkg/web"
+)
+
+func TestChain_ExecutedCorrectOrder(t *testing.T) {
+ RegisterT(t)
+
+ mw1 := func(next web.HandlerFunc) web.HandlerFunc {
+ return func(c *web.Context) error {
+ c.Response.Header().Add("Key1", "Value1")
+ c.Response.Header().Add("Key2", "Value2")
+ return next(c)
+ }
+ }
+
+ mw2 := func(next web.HandlerFunc) web.HandlerFunc {
+ return func(c *web.Context) error {
+ c.Response.Header().Del("Key2")
+ return next(c)
+ }
+ }
+
+ server := mock.NewServer()
+ server.Use(middlewares.Chain(mw1, mw2))
+ handler := func(c *web.Context) error {
+ return c.NoContent(http.StatusOK)
+ }
+
+ status, response := server.Execute(handler)
+
+ Expect(status).Equals(http.StatusOK)
+ Expect(response.Header().Get("Key1")).Equals("Value1")
+ Expect(response.Header().Get("Key2")).Equals("")
+}
+
+func TestChain_NilMiddleware(t *testing.T) {
+ RegisterT(t)
+
+ mw1 := func(next web.HandlerFunc) web.HandlerFunc {
+ return func(c *web.Context) error {
+ c.Response.Header().Add("Key1", "Value1")
+ c.Response.Header().Add("Key2", "Value2")
+ return next(c)
+ }
+ }
+
+ server := mock.NewServer()
+ server.Use(middlewares.Chain(mw1, nil))
+ handler := func(c *web.Context) error {
+ return c.NoContent(http.StatusOK)
+ }
+
+ status, response := server.Execute(handler)
+
+ Expect(status).Equals(http.StatusOK)
+ Expect(response.Header().Get("Key1")).Equals("Value1")
+ Expect(response.Header().Get("Key2")).Equals("Value2")
+}
diff --git a/app/middlewares/instrumentation.go b/app/middlewares/instrumentation.go
index 69edd92ed..3cabacafb 100644
--- a/app/middlewares/instrumentation.go
+++ b/app/middlewares/instrumentation.go
@@ -8,7 +8,6 @@ import (
"github.com/getfider/fider/app/metrics"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/web"
- "github.com/julienschmidt/httprouter"
)
// Instrumentation adds Prometheus HTTP Middlewares
@@ -23,7 +22,7 @@ func Instrumentation() web.MiddlewareFunc {
err := next(c)
- routeName := c.Param(httprouter.MatchedRoutePathParam)
+ routeName := c.GetMatchedRoutePath()
operation := fmt.Sprintf("%s %s", c.Request.Method, routeName)
if routeName == "" && c.Request.URL.Path != "/" {
operation = "No Route Matched"
diff --git a/app/pkg/mock/server.go b/app/pkg/mock/server.go
index 312a39daf..8c24d3905 100644
--- a/app/pkg/mock/server.go
+++ b/app/pkg/mock/server.go
@@ -31,11 +31,14 @@ func createServer() *Server {
engine := web.New()
+ // Create a new request and set matched routed into context
request, _ := http.NewRequest("GET", "/", nil)
+ request = request.WithContext(context.WithValue(request.Context(), httprouter.ParamsKey, httprouter.Params{
+ httprouter.Param{Key: httprouter.MatchedRoutePathParam, Value: "/"},
+ }))
+
recorder := httptest.NewRecorder()
params := make(web.StringMap)
- params[httprouter.MatchedRoutePathParam] = "/"
-
context := web.NewContext(engine, request, recorder, params)
return &Server{
diff --git a/app/pkg/web/context.go b/app/pkg/web/context.go
index cc341516e..6490c3cf9 100644
--- a/app/pkg/web/context.go
+++ b/app/pkg/web/context.go
@@ -10,7 +10,7 @@ import (
"strconv"
"time"
- "github.com/getfider/fider/app/pkg/dbx"
+ "github.com/julienschmidt/httprouter"
"strings"
@@ -18,6 +18,7 @@ import (
"github.com/getfider/fider/app/actions"
"github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/pkg/dbx"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/log"
@@ -391,7 +392,9 @@ func (c *Context) Param(name string) string {
if c.params == nil {
return ""
}
- return c.params[name]
+
+ // The leading slash is removed because of https://github.com/julienschmidt/httprouter/issues/77
+ return strings.TrimLeft(c.params[name], "/")
}
//ParamAsInt returns parameter as int
@@ -404,6 +407,11 @@ func (c *Context) ParamAsInt(name string) (int, error) {
return intValue, nil
}
+//GetMatchedRoutePath returns the Matched Route name
+func (c *Context) GetMatchedRoutePath() string {
+ return "/" + c.Param(httprouter.MatchedRoutePathParam)
+}
+
// Set saves data in the context.
func (c *Context) Set(key interface{}, val interface{}) {
c.Context = context.WithValue(c.Context, key, val)