add set method, add list json req support

This commit is contained in:
Thomas von Dein
2024-12-20 22:59:00 +01:00
committed by T.v.Dein
parent 7bc30da609
commit 6e3fb4ef91
2 changed files with 58 additions and 32 deletions

View File

@@ -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,
},
)
}

View File

@@ -17,11 +17,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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 {