diff --git a/rest/handlers.go b/rest/handlers.go index 8914aad..aec1325 100644 --- a/rest/handlers.go +++ b/rest/handlers.go @@ -19,7 +19,7 @@ package rest import ( //"github.com/alecthomas/repr" - "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v2" "github.com/tlinden/anydb/app" "github.com/tlinden/anydb/cfg" ) @@ -40,19 +40,21 @@ type SingleResponse struct { Entry *app.DbEntry } -func RestList(c fiber.Ctx, conf *cfg.Config) error { - // FIXME: Check for tags and filter - // FIXME: https://github.com/gofiber/fiber/blob/main/docs/api/bind.md#body - /* - setcontext := new(SetContext) - if err := c.Bind().Body(setcontext); err != nil { - return JsonStatus(c, fiber.StatusForbidden, - "Unable to parse body: "+err.Error()) +func RestList(c *fiber.Ctx, conf *cfg.Config) error { + attr := new(app.DbAttr) + + if len(c.Body()) > 0 { + + if err := c.BodyParser(attr); err != nil { + return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{ + "errors": err.Error(), + }) + } - */ + } // get list - entries, err := conf.DB.List(&app.DbAttr{}) + entries, err := conf.DB.List(attr) if err != nil { return JsonStatus(c, fiber.StatusForbidden, "Unable to list keys: "+err.Error()) @@ -67,7 +69,7 @@ func RestList(c fiber.Ctx, conf *cfg.Config) error { ) } -func RestGet(c fiber.Ctx, conf *cfg.Config) error { +func RestGet(c *fiber.Ctx, conf *cfg.Config) error { if c.Params("key") == "" { return JsonStatus(c, fiber.StatusForbidden, "key not provided") @@ -93,7 +95,7 @@ func RestGet(c fiber.Ctx, conf *cfg.Config) error { ) } -func RestDelete(c fiber.Ctx, conf *cfg.Config) error { +func RestDelete(c *fiber.Ctx, conf *cfg.Config) error { if c.Params("key") == "" { return JsonStatus(c, fiber.StatusForbidden, "key not provided") @@ -115,4 +117,25 @@ func RestDelete(c fiber.Ctx, conf *cfg.Config) error { ) } -// FIXME: add RestSet() +func RestSet(c *fiber.Ctx, conf *cfg.Config) error { + attr := new(app.DbAttr) + if err := c.BodyParser(attr); err != nil { + return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{ + "errors": err.Error(), + }) + + } + + err := conf.DB.Set(attr) + if err != nil { + return JsonStatus(c, fiber.StatusForbidden, + "Unable to set key: "+err.Error()) + } + + return c.Status(fiber.StatusOK).JSON( + Result{ + Success: true, + Code: fiber.StatusOK, + }, + ) +} diff --git a/rest/serve.go b/rest/serve.go index fc17806..cea6c68 100644 --- a/rest/serve.go +++ b/rest/serve.go @@ -17,11 +17,11 @@ along with this program. If not, see . package rest import ( - "github.com/gofiber/fiber/v3" - "github.com/gofiber/fiber/v3/middleware/compress" - "github.com/gofiber/fiber/v3/middleware/cors" - "github.com/gofiber/fiber/v3/middleware/logger" - "github.com/gofiber/fiber/v3/middleware/requestid" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/compress" + "github.com/gofiber/fiber/v2/middleware/cors" + "github.com/gofiber/fiber/v2/middleware/logger" + "github.com/gofiber/fiber/v2/middleware/requestid" "github.com/tlinden/anydb/cfg" ) @@ -39,27 +39,26 @@ func Runserver(conf *cfg.Config, args []string) error { // public rest api routes api := router.Group("/anydb/v1") { - api.Get("/", func(c fiber.Ctx) error { + api.Get("/", func(c *fiber.Ctx) error { return RestList(c, conf) }) - api.Get("/:key", func(c fiber.Ctx) error { + api.Get("/:key", func(c *fiber.Ctx) error { return RestGet(c, conf) }) - api.Delete("/:key", func(c fiber.Ctx) error { + api.Delete("/:key", func(c *fiber.Ctx) error { return RestDelete(c, conf) }) - /* - api.Put("/", nil, func(c *fiber.Ctx) error { - return RestSet(c, conf) - }) - */ + + api.Put("/", func(c *fiber.Ctx) error { + return RestSet(c, conf) + }) } // public routes { - router.Get("/", func(c fiber.Ctx) error { + router.Get("/", func(c *fiber.Ctx) error { return c.Send([]byte("Use the REST API")) }) } @@ -68,6 +67,9 @@ func Runserver(conf *cfg.Config, args []string) error { } func SetupServer(conf *cfg.Config) *fiber.App { + // disable colors + fiber.DefaultColors = fiber.Colors{} + router := fiber.New(fiber.Config{ CaseSensitive: true, StrictRouting: true, @@ -79,12 +81,13 @@ func SetupServer(conf *cfg.Config) *fiber.App { router.Use(requestid.New()) router.Use(logger.New(logger.Config{ - Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}​\n", + Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}​\n", + DisableColors: true, })) router.Use(cors.New(cors.Config{ - AllowMethods: []string{"GET", "PUT", "POST", "DELETE"}, - ExposeHeaders: []string{"Content-Type", "Accept"}, + AllowMethods: "GET,PUT,POST,DELETE", + ExposeHeaders: "Content-Type,Accept", })) router.Use(compress.New(compress.Config{ @@ -98,7 +101,7 @@ func SetupServer(conf *cfg.Config) *fiber.App { Wrapper to respond with proper json status, message and code, shall be prepared and called by the handlers directly. */ -func JsonStatus(c fiber.Ctx, code int, msg string) error { +func JsonStatus(c *fiber.Ctx, code int, msg string) error { success := true if code != fiber.StatusOK {