2024-12-20 18:59:59 +01:00
|
|
|
/*
|
2025-12-23 14:24:04 +01:00
|
|
|
Copyright © 2024-2025 Thomas von Dein
|
2024-12-20 18:59:59 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
package rest
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
//"github.com/alecthomas/repr"
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
"encoding/json"
|
2025-12-23 14:30:37 +01:00
|
|
|
"log"
|
2025-12-23 13:33:05 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
2025-11-03 09:15:27 +01:00
|
|
|
"codeberg.org/scip/anydb/app"
|
|
|
|
|
"codeberg.org/scip/anydb/cfg"
|
2024-12-20 18:59:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SetContext struct {
|
|
|
|
|
Query string `json:"query" form:"query"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ListResponse struct {
|
|
|
|
|
Success bool
|
|
|
|
|
Code int
|
|
|
|
|
Entries app.DbEntries
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SingleResponse struct {
|
|
|
|
|
Success bool
|
|
|
|
|
Code int
|
|
|
|
|
Entry *app.DbEntry
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
func RestList(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
|
2024-12-20 22:59:00 +01:00
|
|
|
attr := new(app.DbAttr)
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
err := json.NewDecoder(req.Body).Decode(&attr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err.Error() != `EOF` {
|
|
|
|
|
http.Error(resp, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
2024-12-20 18:59:59 +01:00
|
|
|
}
|
2024-12-20 22:59:00 +01:00
|
|
|
}
|
2024-12-20 18:59:59 +01:00
|
|
|
|
|
|
|
|
// get list
|
2025-01-01 18:08:43 +01:00
|
|
|
entries, err := conf.DB.List(attr, attr.Fulltext)
|
2024-12-20 18:59:59 +01:00
|
|
|
if err != nil {
|
2025-12-23 13:33:05 +01:00
|
|
|
JsonStatus(resp, http.StatusForbidden, "Unable to list keys: "+err.Error())
|
|
|
|
|
return
|
2024-12-20 18:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
2025-12-23 14:30:37 +01:00
|
|
|
err = json.NewEncoder(resp).Encode(
|
2024-12-20 18:59:59 +01:00
|
|
|
ListResponse{
|
2025-12-23 13:33:05 +01:00
|
|
|
Code: http.StatusOK,
|
2024-12-20 18:59:59 +01:00
|
|
|
Success: true,
|
|
|
|
|
Entries: entries,
|
2025-12-23 13:33:05 +01:00
|
|
|
})
|
2025-12-23 14:30:37 +01:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2024-12-20 18:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
func RestGet(resp http.ResponseWriter, req *http.Request, key string, conf *cfg.Config) {
|
|
|
|
|
if key == "" {
|
|
|
|
|
JsonStatus(resp, http.StatusForbidden, "key not provided")
|
|
|
|
|
return
|
2024-12-20 18:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get list
|
2025-12-23 13:33:05 +01:00
|
|
|
entry, err := conf.DB.Get(&app.DbAttr{Key: key})
|
2024-12-20 18:59:59 +01:00
|
|
|
if err != nil {
|
2025-12-23 13:33:05 +01:00
|
|
|
JsonStatus(resp, http.StatusForbidden, "Unable to get key: "+err.Error())
|
|
|
|
|
return
|
2024-12-20 18:59:59 +01:00
|
|
|
}
|
|
|
|
|
if entry.Key == "" {
|
2025-12-23 13:33:05 +01:00
|
|
|
JsonStatus(resp, http.StatusForbidden, "Key does not exist")
|
|
|
|
|
return
|
2024-12-20 18:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
2025-12-23 14:30:37 +01:00
|
|
|
err = json.NewEncoder(resp).Encode(
|
2024-12-20 18:59:59 +01:00
|
|
|
SingleResponse{
|
2025-12-23 13:33:05 +01:00
|
|
|
Code: http.StatusOK,
|
2024-12-20 18:59:59 +01:00
|
|
|
Success: true,
|
|
|
|
|
Entry: entry,
|
2025-12-23 13:33:05 +01:00
|
|
|
})
|
2025-12-23 14:30:37 +01:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2024-12-20 18:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
func RestDelete(resp http.ResponseWriter, req *http.Request, key string, conf *cfg.Config) {
|
|
|
|
|
if key == "" {
|
|
|
|
|
JsonStatus(resp, http.StatusForbidden, "key not provided")
|
|
|
|
|
return
|
2024-12-20 18:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get list
|
2025-12-23 13:33:05 +01:00
|
|
|
err := conf.DB.Del(&app.DbAttr{Key: key})
|
2024-12-20 18:59:59 +01:00
|
|
|
if err != nil {
|
2025-12-23 13:33:05 +01:00
|
|
|
JsonStatus(resp, http.StatusForbidden, "Unable to delete key: "+err.Error())
|
|
|
|
|
return
|
2024-12-20 18:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
JsonStatus(resp, http.StatusOK, "key deleted")
|
2024-12-20 18:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
func RestSet(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
|
2024-12-20 22:59:00 +01:00
|
|
|
attr := new(app.DbAttr)
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
err := json.NewDecoder(req.Body).Decode(&attr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(resp, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
2024-12-20 22:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-23 14:24:04 +01:00
|
|
|
// 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()
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
err = conf.DB.Set(attr)
|
2024-12-20 22:59:00 +01:00
|
|
|
if err != nil {
|
2025-12-23 13:33:05 +01:00
|
|
|
JsonStatus(resp, http.StatusForbidden, "Unable to set key: "+err.Error())
|
|
|
|
|
return
|
2024-12-20 22:59:00 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-23 13:33:05 +01:00
|
|
|
JsonStatus(resp, http.StatusOK, "key added/updated")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Home(resp http.ResponseWriter) {
|
2025-12-23 14:33:35 +01:00
|
|
|
_, err := resp.Write([]byte("Use the REST API on " + apiprefix + "\r\n"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2024-12-20 22:59:00 +01:00
|
|
|
}
|