From b4fa28d0c5fe610af10cc98afc9e14511dbb9f1c Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Tue, 23 Dec 2025 14:24:04 +0100 Subject: [PATCH] replace fiber with http.ServeMux --- anydb.pod | 7 ++++++- app/attr.go | 11 ++++++++--- cfg/config.go | 2 +- cmd/crud.go | 4 ++-- go.mod | 1 + go.sum | 2 ++ rest/handlers.go | 9 ++++++++- rest/serve.go | 11 ++++++----- 8 files changed, 34 insertions(+), 13 deletions(-) diff --git a/anydb.pod b/anydb.pod index d7ab50f..836585e 100644 --- a/anydb.pod +++ b/anydb.pod @@ -569,7 +569,7 @@ Some curl example calls to the API: Post a new key: curl -X PUT localhost:8787/anydb/v1/ \ -H 'Content-Type: application/json' \ - -d '{"key":"foo","val":"bar"}' + -d '{"key":"foo","data":"bar"}' Retrieve the value: @@ -579,6 +579,11 @@ List all keys: curl localhost:8787/anydb/v1/ +Delete an entry: + + curl -s -X DELETE http://localhost:8787/anydb/v1/foo + + =head1 BUGS In order to report a bug, unexpected behavior, feature requests diff --git a/app/attr.go b/app/attr.go index ecc08bf..5866d21 100644 --- a/app/attr.go +++ b/app/attr.go @@ -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 @@ -28,6 +28,7 @@ type DbAttr struct { Key string Preview string Val []byte + Data string // alias Args []string Tags []string File string @@ -63,6 +64,12 @@ func (attr *DbAttr) ParseKV() error { } } + attr.SetPreview() + + return nil +} + +func (attr *DbAttr) SetPreview() { switch { case attr.Binary: attr.Preview = "" @@ -82,8 +89,6 @@ func (attr *DbAttr) ParseKV() error { attr.Preview = string(attr.Val) } } - - return nil } func (attr *DbAttr) GetFileValue() error { diff --git a/cfg/config.go b/cfg/config.go index 0cfe96d..f860ffe 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -30,7 +30,7 @@ import ( "github.com/tlinden/yadu" ) -var Version string = "v0.2.6" +var Version string = "v0.3.0" type BucketConfig struct { Encrypt bool diff --git a/cmd/crud.go b/cmd/crud.go index 5b90920..27f0c8f 100644 --- a/cmd/crud.go +++ b/cmd/crud.go @@ -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 @@ -21,10 +21,10 @@ import ( "os" "strings" - "github.com/spf13/cobra" "codeberg.org/scip/anydb/app" "codeberg.org/scip/anydb/cfg" "codeberg.org/scip/anydb/output" + "github.com/spf13/cobra" ) func Set(conf *cfg.Config) *cobra.Command { diff --git a/go.mod b/go.mod index 4b513e7..6ecfbd5 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( ) require ( + github.com/alecthomas/repr v0.5.2 // indirect github.com/fatih/color v1.16.0 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index dd979eb..f243fcd 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs= +github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/rest/handlers.go b/rest/handlers.go index 15be3e6..15361fc 100644 --- a/rest/handlers.go +++ b/rest/handlers.go @@ -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 @@ -122,6 +122,13 @@ func RestSet(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) { 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()) diff --git a/rest/serve.go b/rest/serve.go index 3fc2d93..78bb7eb 100644 --- a/rest/serve.go +++ b/rest/serve.go @@ -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 @@ -36,15 +36,16 @@ func Runserver(conf *cfg.Config, args []string) error { // setup api server mux := http.NewServeMux() - mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { + // just in case someone tries to access the non-api url + mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { Home(w) }) - mux.HandleFunc("GET "+apiprefix, func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("GET "+apiprefix+"{$}", func(w http.ResponseWriter, r *http.Request) { RestList(w, r, conf) }) - mux.HandleFunc("POST "+apiprefix, func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("POST "+apiprefix+"{$}", func(w http.ResponseWriter, r *http.Request) { RestList(w, r, conf) }) @@ -59,7 +60,7 @@ func Runserver(conf *cfg.Config, args []string) error { }) mux.HandleFunc("PUT "+apiprefix, func(w http.ResponseWriter, r *http.Request) { - RestList(w, r, conf) + RestSet(w, r, conf) }) logger := LogHandler()