Skip to content

Commit

Permalink
fix: Adjust handlers to use Echo signature (#255)
Browse files Browse the repository at this point in the history
Signed-off-by: Marc-Philippe Fuller <marc-philippe.fuller@intel.com>
  • Loading branch information
marcpfuller committed Dec 6, 2023
1 parent c016e0e commit acee94b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 47 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/edgexfoundry/app-functions-sdk-go/v3 v3.2.0-dev.1
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.1
github.com/google/uuid v1.4.0
github.com/gorilla/mux v1.8.0
github.com/labstack/echo/v4 v4.11.3
github.com/stretchr/testify v1.8.4
)

Expand Down Expand Up @@ -46,7 +46,6 @@ require (
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/klauspost/compress v1.17.1 // indirect
github.com/labstack/echo/v4 v4.11.3 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/consul/api v1.25.1 h1:CqrdhYzc8XZuPnhIYZWH45toM0LB9ZeYr/gvpLVI3PE=
Expand Down
82 changes: 39 additions & 43 deletions internal/inventory/app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"edgexfoundry/app-rfid-llrp-inventory/internal/llrp"

"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
"github.com/gorilla/mux"
"github.com/labstack/echo/v4"
)

const (
Expand All @@ -23,7 +23,7 @@ const (
snapshotRoute = common.ApiBase + "/inventory/snapshot"
cmdStartRoute = common.ApiBase + "/command/reading/start"
cmdStopRoute = common.ApiBase + "/command/reading/stop"
behaviorsRoute = common.ApiBase + "/behaviors/{name}"
behaviorsRoute = common.ApiBase + "/behaviors/:name"
)

func (app *InventoryApp) addRoutes() error {
Expand Down Expand Up @@ -59,120 +59,116 @@ func (app *InventoryApp) addRoutes() error {
return nil
}

func (app *InventoryApp) addRoute(path, method string, f http.HandlerFunc) error {
if err := app.service.AddRoute(path, f, method); err != nil {
func (app *InventoryApp) addRoute(path, method string, f echo.HandlerFunc) error {
if err := app.service.AddCustomRoute(path, false, f, method); err != nil {
return fmt.Errorf("failed to add route, path=%s, method=%s: %w", path, method, err)
}
return nil
}

// Routes
func (app *InventoryApp) index(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "static/html/index.html")
func (app *InventoryApp) index(ctx echo.Context) error {
http.ServeFile(ctx.Response().Writer, ctx.Request(), "static/html/index.html")
return nil
}

func (app *InventoryApp) getReaders(w http.ResponseWriter, _ *http.Request) {
func (app *InventoryApp) getReaders(ctx echo.Context) error {
w := ctx.Response().Writer
w.Header().Set("Content-Type", "application/json")
if err := app.defaultGrp.WriteReaders(w); err != nil {
msg := fmt.Sprintf("Failed to write readers list: %v", err)
app.lc.Error(msg)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, msg, http.StatusInternalServerError)
return ctx.String(http.StatusInternalServerError, msg)
}
return nil
}

func (app *InventoryApp) getSnapshot(w http.ResponseWriter, _ *http.Request) {
func (app *InventoryApp) getSnapshot(ctx echo.Context) error {
w := ctx.Response().Writer
w.Header().Set("Content-Type", "application/json")
if err := app.requestInventorySnapshot(w); err != nil {
msg := fmt.Sprintf("Failed to write inventory snapshot: %v", err)
app.lc.Error(msg)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, msg, http.StatusInternalServerError)
return ctx.String(http.StatusInternalServerError, msg)
}
return nil
}

func (app *InventoryApp) startReading(w http.ResponseWriter, _ *http.Request) {
func (app *InventoryApp) startReading(ctx echo.Context) error {
if err := app.defaultGrp.StartAll(app.devService); err != nil {
msg := fmt.Sprintf("Failed to StartAll: %v", err)
app.lc.Error(msg)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, msg, http.StatusInternalServerError)
return ctx.String(http.StatusInternalServerError, msg)
}
return nil
}

func (app *InventoryApp) stopReading(w http.ResponseWriter, _ *http.Request) {
func (app *InventoryApp) stopReading(ctx echo.Context) error {
if err := app.defaultGrp.StopAll(app.devService); err != nil {
msg := fmt.Sprintf("Failed to StopAll: %v", err)
app.lc.Error(msg)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, msg, http.StatusInternalServerError)
return
return ctx.String(http.StatusInternalServerError, msg)
}
return nil
}

func (app *InventoryApp) getBehavior(w http.ResponseWriter, req *http.Request) {
rv := mux.Vars(req)
bName := rv["name"]
func (app *InventoryApp) getBehavior(ctx echo.Context) error {
w := ctx.Response().Writer
bName := ctx.Param("name")
// Currently, only "default" is supported.
if bName != "default" {
msg := fmt.Sprintf("Request to GET unknown behavior. Name: %v", bName)
app.lc.Error(msg)

if _, err := w.Write([]byte("Invalid behavior name.")); err != nil {
app.lc.Error("Error writing failure response.", "error", err)
}
w.WriteHeader(http.StatusNotFound)
http.Error(w, msg, http.StatusNotFound)
return
return ctx.String(http.StatusNotFound, msg)
}

data, err := json.Marshal(app.defaultGrp.Behavior())
if err != nil {
msg := fmt.Sprintf("Failed to marshal behavior: %v", err)
app.lc.Error(msg)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, msg, http.StatusInternalServerError)
return
return ctx.String(http.StatusInternalServerError, msg)
}

if _, err := w.Write(data); err != nil {
msg := fmt.Sprintf("Failed to write behavior data: %v", err)
app.lc.Error(msg)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, msg, http.StatusInternalServerError)
return ctx.String(http.StatusInternalServerError, msg)

}
return nil
}

func (app *InventoryApp) setBehavior(w http.ResponseWriter, req *http.Request) {
rv := mux.Vars(req)
bName := rv["name"]
func (app *InventoryApp) setBehavior(ctx echo.Context) error {
w := ctx.Response().Writer
req := ctx.Request()
bName := ctx.Param("name")
// Currently, only "default" is supported.
if bName != "default" {
msg := fmt.Sprintf("Attempt to PUT unknown behavior. Name %v", bName)
app.lc.Error(msg)
if _, err := w.Write([]byte("Invalid behavior name.")); err != nil {
app.lc.Error("Error writing failure response.", "error", err)
}
w.WriteHeader(http.StatusNotFound)
http.Error(w, msg, http.StatusNotFound)
return
return ctx.String(http.StatusNotFound, msg)
}

data, err := io.ReadAll(io.LimitReader(req.Body, maxBodyBytes))
if err != nil {
msg := fmt.Sprintf("Failed to read behavior data: %v", err)
app.lc.Error(msg)
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, msg, http.StatusInternalServerError)
return
return ctx.String(http.StatusInternalServerError, msg)
}

var b llrp.Behavior
if err := json.Unmarshal(data, &b); err != nil {
msg := fmt.Sprintf("Failed to unmarshal behavior data: %v. Body: %s", err, string(data))
app.lc.Error(msg)
w.WriteHeader(http.StatusBadRequest)
http.Error(w, msg, http.StatusInternalServerError)
return
return ctx.String(http.StatusInternalServerError, msg)
}

if err := app.defaultGrp.SetBehavior(app.devService, b); err != nil {
Expand All @@ -182,9 +178,9 @@ func (app *InventoryApp) setBehavior(w http.ResponseWriter, req *http.Request) {
if _, err := w.Write([]byte(err.Error())); err != nil {
app.lc.Error("Error writing failure response.", "error", err)
}
http.Error(w, msg, http.StatusInternalServerError)
return
return ctx.String(http.StatusInternalServerError, msg)
}

app.lc.Info("Updated behavior.", "name", bName)
return nil
}

0 comments on commit acee94b

Please sign in to comment.