Replace fiber with net.http.ServeMux (#23)

This commit is contained in:
T. von Dein
2025-12-23 14:38:45 +01:00
parent 5a705b0af0
commit 3b37a67e02
10 changed files with 306 additions and 183 deletions

View File

@@ -1,5 +1,5 @@
/*
Copyright © 2024 Thomas von Dein
Copyright © 2024-2025 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -19,7 +19,10 @@ package rest
import (
//"github.com/alecthomas/repr"
"github.com/gofiber/fiber/v2"
"encoding/json"
"log"
"net/http"
"codeberg.org/scip/anydb/app"
"codeberg.org/scip/anydb/cfg"
)
@@ -40,101 +43,113 @@ 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")
err = 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")
}
// get list
entry, err := conf.DB.Get(&app.DbAttr{Key: c.Params("key")})
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
"Unable to get key: "+err.Error())
}
if entry.Key == "" {
return JsonStatus(c, fiber.StatusForbidden,
"Key does not exist")
}
return c.Status(fiber.StatusOK).JSON(
SingleResponse{
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")
}
// get list
err := conf.DB.Del(&app.DbAttr{Key: c.Params("key")})
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
"Unable to delete key: "+err.Error())
}
return c.Status(fiber.StatusOK).JSON(
Result{
Success: true,
Code: fiber.StatusOK,
Message: "key deleted",
},
)
}
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())
log.Fatal(err)
}
}
func RestGet(resp http.ResponseWriter, req *http.Request, key string, conf *cfg.Config) {
if key == "" {
JsonStatus(resp, http.StatusForbidden, "key not provided")
return
}
return c.Status(fiber.StatusOK).JSON(
Result{
// get list
entry, err := conf.DB.Get(&app.DbAttr{Key: key})
if err != nil {
JsonStatus(resp, http.StatusForbidden, "Unable to get key: "+err.Error())
return
}
if entry.Key == "" {
JsonStatus(resp, http.StatusForbidden, "Key does not exist")
return
}
resp.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(resp).Encode(
SingleResponse{
Code: http.StatusOK,
Success: true,
Code: fiber.StatusOK,
},
)
Entry: entry,
})
if err != nil {
log.Fatal(err)
}
}
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: key})
if err != nil {
JsonStatus(resp, http.StatusForbidden, "Unable to delete key: "+err.Error())
return
}
JsonStatus(resp, http.StatusOK, "key deleted")
}
func RestSet(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
attr := new(app.DbAttr)
err := json.NewDecoder(req.Body).Decode(&attr)
if err != nil {
http.Error(resp, err.Error(), http.StatusBadRequest)
return
}
// attr.Data is a string, thus the decoder doesn't expect it to be
// base64 encoded. However, internally we need []byte, therefore
// we copy a cast to .Val. We also need to setup the .Preview
// value here.
attr.Val = []byte(attr.Data)
attr.SetPreview()
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) {
_, err := resp.Write([]byte("Use the REST API on " + apiprefix + "\r\n"))
if err != nil {
log.Fatal(err)
}
}