replace fiber with http.ServeMux

This commit is contained in:
2025-12-23 14:24:04 +01:00
parent 37c7e808ed
commit b4fa28d0c5
8 changed files with 34 additions and 13 deletions

View File

@@ -569,7 +569,7 @@ Some curl example calls to the API:
Post a new key: Post a new key:
curl -X PUT localhost:8787/anydb/v1/ \ curl -X PUT localhost:8787/anydb/v1/ \
-H 'Content-Type: application/json' \ -H 'Content-Type: application/json' \
-d '{"key":"foo","val":"bar"}' -d '{"key":"foo","data":"bar"}'
Retrieve the value: Retrieve the value:
@@ -579,6 +579,11 @@ List all keys:
curl localhost:8787/anydb/v1/ curl localhost:8787/anydb/v1/
Delete an entry:
curl -s -X DELETE http://localhost:8787/anydb/v1/foo
=head1 BUGS =head1 BUGS
In order to report a bug, unexpected behavior, feature requests In order to report a bug, unexpected behavior, feature requests

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 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 it under the terms of the GNU General Public License as published by
@@ -28,6 +28,7 @@ type DbAttr struct {
Key string Key string
Preview string Preview string
Val []byte Val []byte
Data string // alias
Args []string Args []string
Tags []string Tags []string
File string File string
@@ -63,6 +64,12 @@ func (attr *DbAttr) ParseKV() error {
} }
} }
attr.SetPreview()
return nil
}
func (attr *DbAttr) SetPreview() {
switch { switch {
case attr.Binary: case attr.Binary:
attr.Preview = "<binary-content>" attr.Preview = "<binary-content>"
@@ -82,8 +89,6 @@ func (attr *DbAttr) ParseKV() error {
attr.Preview = string(attr.Val) attr.Preview = string(attr.Val)
} }
} }
return nil
} }
func (attr *DbAttr) GetFileValue() error { func (attr *DbAttr) GetFileValue() error {

View File

@@ -30,7 +30,7 @@ import (
"github.com/tlinden/yadu" "github.com/tlinden/yadu"
) )
var Version string = "v0.2.6" var Version string = "v0.3.0"
type BucketConfig struct { type BucketConfig struct {
Encrypt bool Encrypt bool

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 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 it under the terms of the GNU General Public License as published by
@@ -21,10 +21,10 @@ import (
"os" "os"
"strings" "strings"
"github.com/spf13/cobra"
"codeberg.org/scip/anydb/app" "codeberg.org/scip/anydb/app"
"codeberg.org/scip/anydb/cfg" "codeberg.org/scip/anydb/cfg"
"codeberg.org/scip/anydb/output" "codeberg.org/scip/anydb/output"
"github.com/spf13/cobra"
) )
func Set(conf *cfg.Config) *cobra.Command { func Set(conf *cfg.Config) *cobra.Command {

1
go.mod
View File

@@ -20,6 +20,7 @@ require (
) )
require ( require (
github.com/alecthomas/repr v0.5.2 // indirect
github.com/fatih/color v1.16.0 // indirect github.com/fatih/color v1.16.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect

2
go.sum
View File

@@ -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/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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

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 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 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 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) err = conf.DB.Set(attr)
if err != nil { if err != nil {
JsonStatus(resp, http.StatusForbidden, "Unable to set key: "+err.Error()) JsonStatus(resp, http.StatusForbidden, "Unable to set key: "+err.Error())

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 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 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 // setup api server
mux := http.NewServeMux() 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) 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) 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) 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) { mux.HandleFunc("PUT "+apiprefix, func(w http.ResponseWriter, r *http.Request) {
RestList(w, r, conf) RestSet(w, r, conf)
}) })
logger := LogHandler() logger := LogHandler()