diff --git a/internal/core/command/router.go b/internal/core/command/router.go index 9c55144664..2f511be7da 100644 --- a/internal/core/command/router.go +++ b/internal/core/command/router.go @@ -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) } diff --git a/internal/pkg/correlation/middleware.go b/internal/pkg/correlation/middleware.go index 3d3e66606c..0b75d501ec 100644 --- a/internal/pkg/correlation/middleware.go +++ b/internal/pkg/correlation/middleware.go @@ -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) + } } }