Skip to content

Commit

Permalink
fix: Update UrlDecodeMiddleware to use echo format
Browse files Browse the repository at this point in the history
- Update UrlDecodeMiddleware to use echo format
- Redefine routes by group
- Apply the authenticationHook on the group level and execute first.

Signed-off-by: Lindsey Cheng <beckysocute@gmail.com>
  • Loading branch information
lindseysimple committed Aug 3, 2023
1 parent f0b0b88 commit 84ead09
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
21 changes: 10 additions & 11 deletions internal/core/command/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@ func LoadRestRoutes(r *echo.Echo, dic *di.Container, serviceName string) {
// Common
_ = controller.NewCommonController(dic, r, serviceName, edgex.Version)

g := r.Group(common.ApiBase)
g.Use(authenticationHook)
g.Use(correlation.ManageHeader)
g.Use(correlation.LoggingMiddleware(container.LoggingClientFrom(dic.Get)))
g.Use(correlation.UrlDecodeMiddleware(container.LoggingClientFrom(dic.Get)))

// Command
cmd := commandController.NewCommandController(dic)

// create a route group with /api/v3/device as prefix, which applies the same authenticationHook middleware
deviceRoutes := r.Group(common.ApiDeviceRoute)
deviceRoutes.Use(authenticationHook)

deviceRoutes.GET("/"+common.All, cmd.AllCommands)
deviceRoutes.GET("/"+common.Name+"/:"+common.Name, cmd.CommandsByDeviceName)
deviceRoutes.GET("/"+common.Name+"/:"+common.Name+"/:"+common.Command, cmd.IssueGetCommandByName)
deviceRoutes.PUT("/"+common.Name+"/:"+common.Name+"/:"+common.Command, cmd.IssueSetCommandByName)

r.Use(correlation.ManageHeader)
r.Use(correlation.LoggingMiddleware(container.LoggingClientFrom(dic.Get)))
g.GET("/"+common.Device+"/"+common.All, cmd.AllCommands)
g.GET("/"+common.Device+"/"+common.Name+"/:"+common.Name, cmd.CommandsByDeviceName)
g.GET("/"+common.Device+"/"+common.Name+"/:"+common.Name+"/:"+common.Command, cmd.IssueGetCommandByName)
g.PUT("/"+common.Device+"/"+common.Name+"/:"+common.Name+"/:"+common.Command, cmd.IssueSetCommandByName)
}
28 changes: 17 additions & 11 deletions internal/pkg/correlation/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,33 @@ func LoggingMiddleware(lc logger.LoggingClient) echo.MiddlewareFunc {
lc.Trace("Begin request", common.CorrelationHeader, correlationId, "path", r.URL.Path)
next(c)
lc.Trace("Response complete", common.CorrelationHeader, correlationId, "duration", time.Since(begin).String())
return nil
}
return nil
return next(c)
}
}
}

// UrlDecodeMiddleware decode the path variables
// After invoking the router.UseEncodedPath() func, the path variables needs to decode before passing to the controller
func UrlDecodeMiddleware(lc logger.LoggingClient) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
for k, v := range vars {
func UrlDecodeMiddleware(lc logger.LoggingClient) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
var unescapedParams []string
// Retrieve all the url path param names
paramNames := c.ParamNames()

// Retrieve all the url path param values and decode
for k, v := range c.ParamValues() {
unescape, err := url.PathUnescape(v)
if err != nil {
lc.Debugf("failed to decode the %s from the value %s", k, v)
return
lc.Debugf("failed to decode the %s from the value %s", paramNames[k], v)
return err
}
vars[k] = unescape
unescapedParams = append(unescapedParams, unescape)
}
next.ServeHTTP(w, r)
})
c.SetParamValues(unescapedParams...)
return next(c)
}
}
}

0 comments on commit 84ead09

Please sign in to comment.