mirror of
https://codeberg.org/scip/anydb.git
synced 2026-02-04 09:20:58 +01:00
replace the fiber framework with net/http.ServeMux
This commit is contained in:
110
rest/handlers.go
110
rest/handlers.go
@@ -19,7 +19,9 @@ package rest
|
||||
import (
|
||||
//"github.com/alecthomas/repr"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"codeberg.org/scip/anydb/app"
|
||||
"codeberg.org/scip/anydb/cfg"
|
||||
)
|
||||
@@ -40,101 +42,95 @@ type SingleResponse struct {
|
||||
Entry *app.DbEntry
|
||||
}
|
||||
|
||||
func RestList(c *fiber.Ctx, conf *cfg.Config) error {
|
||||
func RestList(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
|
||||
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(),
|
||||
})
|
||||
|
||||
err := json.NewDecoder(req.Body).Decode(&attr)
|
||||
if err != nil {
|
||||
if err.Error() != `EOF` {
|
||||
http.Error(resp, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// get list
|
||||
entries, err := conf.DB.List(attr, attr.Fulltext)
|
||||
if err != nil {
|
||||
return JsonStatus(c, fiber.StatusForbidden,
|
||||
"Unable to list keys: "+err.Error())
|
||||
JsonStatus(resp, http.StatusForbidden, "Unable to list keys: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(
|
||||
resp.Header().Set("Content-Type", "application/json")
|
||||
|
||||
json.NewEncoder(resp).Encode(
|
||||
ListResponse{
|
||||
Code: http.StatusOK,
|
||||
Success: true,
|
||||
Code: fiber.StatusOK,
|
||||
Entries: entries,
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func RestGet(c *fiber.Ctx, conf *cfg.Config) error {
|
||||
if c.Params("key") == "" {
|
||||
return JsonStatus(c, fiber.StatusForbidden,
|
||||
"key not provided")
|
||||
func RestGet(resp http.ResponseWriter, req *http.Request, key string, conf *cfg.Config) {
|
||||
if key == "" {
|
||||
JsonStatus(resp, http.StatusForbidden, "key not provided")
|
||||
return
|
||||
}
|
||||
|
||||
// get list
|
||||
entry, err := conf.DB.Get(&app.DbAttr{Key: c.Params("key")})
|
||||
entry, err := conf.DB.Get(&app.DbAttr{Key: key})
|
||||
if err != nil {
|
||||
return JsonStatus(c, fiber.StatusForbidden,
|
||||
"Unable to get key: "+err.Error())
|
||||
JsonStatus(resp, http.StatusForbidden, "Unable to get key: "+err.Error())
|
||||
return
|
||||
}
|
||||
if entry.Key == "" {
|
||||
return JsonStatus(c, fiber.StatusForbidden,
|
||||
"Key does not exist")
|
||||
JsonStatus(resp, http.StatusForbidden, "Key does not exist")
|
||||
return
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(
|
||||
resp.Header().Set("Content-Type", "application/json")
|
||||
|
||||
json.NewEncoder(resp).Encode(
|
||||
SingleResponse{
|
||||
Code: http.StatusOK,
|
||||
Success: true,
|
||||
Code: fiber.StatusOK,
|
||||
Entry: entry,
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func RestDelete(c *fiber.Ctx, conf *cfg.Config) error {
|
||||
if c.Params("key") == "" {
|
||||
return JsonStatus(c, fiber.StatusForbidden,
|
||||
"key not provided")
|
||||
func RestDelete(resp http.ResponseWriter, req *http.Request, key string, conf *cfg.Config) {
|
||||
if key == "" {
|
||||
JsonStatus(resp, http.StatusForbidden, "key not provided")
|
||||
return
|
||||
}
|
||||
|
||||
// get list
|
||||
err := conf.DB.Del(&app.DbAttr{Key: c.Params("key")})
|
||||
err := conf.DB.Del(&app.DbAttr{Key: key})
|
||||
if err != nil {
|
||||
return JsonStatus(c, fiber.StatusForbidden,
|
||||
"Unable to delete key: "+err.Error())
|
||||
JsonStatus(resp, http.StatusForbidden, "Unable to delete key: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(
|
||||
Result{
|
||||
Success: true,
|
||||
Code: fiber.StatusOK,
|
||||
Message: "key deleted",
|
||||
},
|
||||
)
|
||||
JsonStatus(resp, http.StatusOK, "key deleted")
|
||||
}
|
||||
|
||||
func RestSet(c *fiber.Ctx, conf *cfg.Config) error {
|
||||
func RestSet(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
|
||||
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)
|
||||
err := json.NewDecoder(req.Body).Decode(&attr)
|
||||
if err != nil {
|
||||
return JsonStatus(c, fiber.StatusForbidden,
|
||||
"Unable to set key: "+err.Error())
|
||||
http.Error(resp, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(
|
||||
Result{
|
||||
Success: true,
|
||||
Code: fiber.StatusOK,
|
||||
},
|
||||
)
|
||||
err = conf.DB.Set(attr)
|
||||
if err != nil {
|
||||
JsonStatus(resp, http.StatusForbidden, "Unable to set key: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
JsonStatus(resp, http.StatusOK, "key added/updated")
|
||||
}
|
||||
|
||||
func Home(resp http.ResponseWriter) {
|
||||
resp.Write([]byte("Use the REST API on " + apiprefix + "\r\n"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user