Skip to content

Commit 33015b3

Browse files
committed
fix: azure ping not responding
1 parent e245b41 commit 33015b3

File tree

6 files changed

+86
-8
lines changed

6 files changed

+86
-8
lines changed

app/cmd/routes.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func routes(r *web.Engine) *web.Engine {
2222
mw := middlewares.Chain(
2323
middlewares.WebSetup(),
2424
middlewares.Tenant(),
25-
middlewares.Instrumentation(),
2625
)
2726
next := mw(func(c *web.Context) error {
2827
return c.NotFound()

app/middlewares/chain.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ func Chain(mws ...web.MiddlewareFunc) web.MiddlewareFunc {
99
return func(handler web.HandlerFunc) web.HandlerFunc {
1010
next := handler
1111
for i := len(mws) - 1; i >= 0; i-- {
12-
next = mws[i](next)
12+
mw := mws[i]
13+
if mw != nil {
14+
next = mw(next)
15+
}
1316
}
1417
return next
1518
}

app/middlewares/chain_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package middlewares_test
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/getfider/fider/app/middlewares"
8+
. "github.com/getfider/fider/app/pkg/assert"
9+
"github.com/getfider/fider/app/pkg/mock"
10+
"github.com/getfider/fider/app/pkg/web"
11+
)
12+
13+
func TestChain_ExecutedCorrectOrder(t *testing.T) {
14+
RegisterT(t)
15+
16+
mw1 := func(next web.HandlerFunc) web.HandlerFunc {
17+
return func(c *web.Context) error {
18+
c.Response.Header().Add("Key1", "Value1")
19+
c.Response.Header().Add("Key2", "Value2")
20+
return next(c)
21+
}
22+
}
23+
24+
mw2 := func(next web.HandlerFunc) web.HandlerFunc {
25+
return func(c *web.Context) error {
26+
c.Response.Header().Del("Key2")
27+
return next(c)
28+
}
29+
}
30+
31+
server := mock.NewServer()
32+
server.Use(middlewares.Chain(mw1, mw2))
33+
handler := func(c *web.Context) error {
34+
return c.NoContent(http.StatusOK)
35+
}
36+
37+
status, response := server.Execute(handler)
38+
39+
Expect(status).Equals(http.StatusOK)
40+
Expect(response.Header().Get("Key1")).Equals("Value1")
41+
Expect(response.Header().Get("Key2")).Equals("")
42+
}
43+
44+
func TestChain_NilMiddleware(t *testing.T) {
45+
RegisterT(t)
46+
47+
mw1 := func(next web.HandlerFunc) web.HandlerFunc {
48+
return func(c *web.Context) error {
49+
c.Response.Header().Add("Key1", "Value1")
50+
c.Response.Header().Add("Key2", "Value2")
51+
return next(c)
52+
}
53+
}
54+
55+
server := mock.NewServer()
56+
server.Use(middlewares.Chain(mw1, nil))
57+
handler := func(c *web.Context) error {
58+
return c.NoContent(http.StatusOK)
59+
}
60+
61+
status, response := server.Execute(handler)
62+
63+
Expect(status).Equals(http.StatusOK)
64+
Expect(response.Header().Get("Key1")).Equals("Value1")
65+
Expect(response.Header().Get("Key2")).Equals("Value2")
66+
}

app/middlewares/instrumentation.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/getfider/fider/app/metrics"
99
"github.com/getfider/fider/app/pkg/env"
1010
"github.com/getfider/fider/app/pkg/web"
11-
"github.com/julienschmidt/httprouter"
1211
)
1312

1413
// Instrumentation adds Prometheus HTTP Middlewares
@@ -23,7 +22,7 @@ func Instrumentation() web.MiddlewareFunc {
2322

2423
err := next(c)
2524

26-
routeName := c.Param(httprouter.MatchedRoutePathParam)
25+
routeName := c.GetMatchedRoutePath()
2726
operation := fmt.Sprintf("%s %s", c.Request.Method, routeName)
2827
if routeName == "" && c.Request.URL.Path != "/" {
2928
operation = "No Route Matched"

app/pkg/mock/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ func createServer() *Server {
3131

3232
engine := web.New()
3333

34+
// Create a new request and set matched routed into context
3435
request, _ := http.NewRequest("GET", "/", nil)
36+
request = request.WithContext(context.WithValue(request.Context(), httprouter.ParamsKey, httprouter.Params{
37+
httprouter.Param{Key: httprouter.MatchedRoutePathParam, Value: "/"},
38+
}))
39+
3540
recorder := httptest.NewRecorder()
3641
params := make(web.StringMap)
37-
params[httprouter.MatchedRoutePathParam] = "/"
38-
3942
context := web.NewContext(engine, request, recorder, params)
4043

4144
return &Server{

app/pkg/web/context.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010
"strconv"
1111
"time"
1212

13-
"github.com/getfider/fider/app/pkg/dbx"
13+
"github.com/julienschmidt/httprouter"
1414

1515
"strings"
1616

1717
"github.com/getfider/fider/app"
1818
"github.com/getfider/fider/app/actions"
1919
"github.com/getfider/fider/app/models/dto"
2020
"github.com/getfider/fider/app/models/entity"
21+
"github.com/getfider/fider/app/pkg/dbx"
2122
"github.com/getfider/fider/app/pkg/env"
2223
"github.com/getfider/fider/app/pkg/errors"
2324
"github.com/getfider/fider/app/pkg/log"
@@ -391,7 +392,9 @@ func (c *Context) Param(name string) string {
391392
if c.params == nil {
392393
return ""
393394
}
394-
return c.params[name]
395+
396+
// The leading slash is removed because of https://github.com/julienschmidt/httprouter/issues/77
397+
return strings.TrimLeft(c.params[name], "/")
395398
}
396399

397400
//ParamAsInt returns parameter as int
@@ -404,6 +407,11 @@ func (c *Context) ParamAsInt(name string) (int, error) {
404407
return intValue, nil
405408
}
406409

410+
//GetMatchedRoutePath returns the Matched Route name
411+
func (c *Context) GetMatchedRoutePath() string {
412+
return "/" + c.Param(httprouter.MatchedRoutePathParam)
413+
}
414+
407415
// Set saves data in the context.
408416
func (c *Context) Set(key interface{}, val interface{}) {
409417
c.Context = context.WithValue(c.Context, key, val)

0 commit comments

Comments
 (0)