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 ( import (
//"github.com/alecthomas/repr" //"github.com/alecthomas/repr"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v2"
"github.com/tlinden/anydb/app" "github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg" "github.com/tlinden/anydb/cfg"
) )
@@ -40,19 +40,21 @@ type SingleResponse struct {
Entry *app.DbEntry Entry *app.DbEntry
} }
func RestList(c fiber.Ctx, conf *cfg.Config) error { func RestList(c *fiber.Ctx, conf *cfg.Config) error {
// FIXME: Check for tags and filter attr := new(app.DbAttr)
// FIXME: https://github.com/gofiber/fiber/blob/main/docs/api/bind.md#body
/* if len(c.Body()) > 0 {
setcontext := new(SetContext)
if err := c.Bind().Body(setcontext); err != nil { if err := c.BodyParser(attr); err != nil {
return JsonStatus(c, fiber.StatusForbidden, return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"Unable to parse body: "+err.Error()) "errors": err.Error(),
})
}
} }
*/
// get list // get list
entries, err := conf.DB.List(&app.DbAttr{}) entries, err := conf.DB.List(attr)
if err != nil { if err != nil {
return JsonStatus(c, fiber.StatusForbidden, return JsonStatus(c, fiber.StatusForbidden,
"Unable to list keys: "+err.Error()) "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") == "" { if c.Params("key") == "" {
return JsonStatus(c, fiber.StatusForbidden, return JsonStatus(c, fiber.StatusForbidden,
"key not provided") "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") == "" { if c.Params("key") == "" {
return JsonStatus(c, fiber.StatusForbidden, return JsonStatus(c, fiber.StatusForbidden,
"key not provided") "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 package rest
import ( import (
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v3/middleware/compress" "github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v3/middleware/cors" "github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v3/middleware/logger" "github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v3/middleware/requestid" "github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/tlinden/anydb/cfg" "github.com/tlinden/anydb/cfg"
) )
@@ -39,27 +39,26 @@ func Runserver(conf *cfg.Config, args []string) error {
// public rest api routes // public rest api routes
api := router.Group("/anydb/v1") api := router.Group("/anydb/v1")
{ {
api.Get("/", func(c fiber.Ctx) error { api.Get("/", func(c *fiber.Ctx) error {
return RestList(c, conf) return RestList(c, conf)
}) })
api.Get("/:key", func(c fiber.Ctx) error { api.Get("/:key", func(c *fiber.Ctx) error {
return RestGet(c, conf) return RestGet(c, conf)
}) })
api.Delete("/:key", func(c fiber.Ctx) error { api.Delete("/:key", func(c *fiber.Ctx) error {
return RestDelete(c, conf) return RestDelete(c, conf)
}) })
/*
api.Put("/", nil, func(c *fiber.Ctx) error { api.Put("/", func(c *fiber.Ctx) error {
return RestSet(c, conf) return RestSet(c, conf)
}) })
*/
} }
// public routes // public routes
{ {
router.Get("/", func(c fiber.Ctx) error { router.Get("/", func(c *fiber.Ctx) error {
return c.Send([]byte("Use the REST API")) 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 { func SetupServer(conf *cfg.Config) *fiber.App {
// disable colors
fiber.DefaultColors = fiber.Colors{}
router := fiber.New(fiber.Config{ router := fiber.New(fiber.Config{
CaseSensitive: true, CaseSensitive: true,
StrictRouting: true, StrictRouting: true,
@@ -80,11 +82,12 @@ func SetupServer(conf *cfg.Config) *fiber.App {
router.Use(logger.New(logger.Config{ 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{ router.Use(cors.New(cors.Config{
AllowMethods: []string{"GET", "PUT", "POST", "DELETE"}, AllowMethods: "GET,PUT,POST,DELETE",
ExposeHeaders: []string{"Content-Type", "Accept"}, ExposeHeaders: "Content-Type,Accept",
})) }))
router.Use(compress.New(compress.Config{ 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, Wrapper to respond with proper json status, message and code,
shall be prepared and called by the handlers directly. 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 success := true
if code != fiber.StatusOK { if code != fiber.StatusOK {