mirror of
https://codeberg.org/scip/anydb.git
synced 2026-10-08 03:04:20 +02:00
update linter config, satisfy linter (#28)
This commit is contained in:
@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package rest
|
||||
|
||||
import (
|
||||
//"github.com/alecthomas/repr"
|
||||
// "github.com/alecthomas/repr"
|
||||
|
||||
"encoding/json"
|
||||
"log"
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
)
|
||||
|
||||
type SetContext struct {
|
||||
Query string `json:"query" form:"query"`
|
||||
Query string `form:"query" json:"query"`
|
||||
}
|
||||
|
||||
type ListResponse struct {
|
||||
@@ -50,6 +50,7 @@ func RestList(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
|
||||
if err != nil {
|
||||
if err.Error() != `EOF` {
|
||||
http.Error(resp, err.Error(), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -58,6 +59,7 @@ func RestList(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
|
||||
entries, err := conf.DB.List(attr, attr.Fulltext)
|
||||
if err != nil {
|
||||
JsonStatus(resp, http.StatusForbidden, "Unable to list keys: "+err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -69,7 +71,6 @@ func RestList(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
|
||||
Success: true,
|
||||
Entries: entries,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -78,6 +79,7 @@ func RestList(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
|
||||
func RestGet(resp http.ResponseWriter, req *http.Request, key string, conf *cfg.Config) {
|
||||
if key == "" {
|
||||
JsonStatus(resp, http.StatusForbidden, "key not provided")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -85,10 +87,13 @@ func RestGet(resp http.ResponseWriter, req *http.Request, key string, conf *cfg.
|
||||
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 == "" {
|
||||
|
||||
if entry.GetKey() == "" {
|
||||
JsonStatus(resp, http.StatusForbidden, "Key does not exist")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -100,7 +105,6 @@ func RestGet(resp http.ResponseWriter, req *http.Request, key string, conf *cfg.
|
||||
Success: true,
|
||||
Entry: entry,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -109,6 +113,7 @@ func RestGet(resp http.ResponseWriter, req *http.Request, key string, conf *cfg.
|
||||
func RestDelete(resp http.ResponseWriter, req *http.Request, key string, conf *cfg.Config) {
|
||||
if key == "" {
|
||||
JsonStatus(resp, http.StatusForbidden, "key not provided")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -116,6 +121,7 @@ func RestDelete(resp http.ResponseWriter, req *http.Request, key string, conf *c
|
||||
err := conf.DB.Del(&app.DbAttr{Key: key})
|
||||
if err != nil {
|
||||
JsonStatus(resp, http.StatusForbidden, "Unable to delete key: "+err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -128,6 +134,7 @@ func RestSet(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
|
||||
err := json.NewDecoder(req.Body).Decode(&attr)
|
||||
if err != nil {
|
||||
http.Error(resp, err.Error(), http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -141,6 +148,7 @@ func RestSet(resp http.ResponseWriter, req *http.Request, conf *cfg.Config) {
|
||||
err = conf.DB.Set(attr)
|
||||
if err != nil {
|
||||
JsonStatus(resp, http.StatusForbidden, "Unable to set key: "+err.Error())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
package rest
|
||||
|
||||
/*
|
||||
This logging middleware is based on
|
||||
|
||||
@@ -7,7 +9,6 @@ https://github.com/elithrar/admission-control/blob/v0.6.3/request_logger.go
|
||||
|
||||
I am using slog and added a couple of small modifications.
|
||||
*/
|
||||
package rest
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
// written HTTP status code to be captured for logging.
|
||||
type responseWriter struct {
|
||||
http.ResponseWriter
|
||||
|
||||
status int
|
||||
size int
|
||||
wroteHeader bool
|
||||
@@ -48,7 +50,6 @@ func (rw *responseWriter) WriteHeader(code int) {
|
||||
}
|
||||
|
||||
func (rw *responseWriter) Write(data []byte) (int, error) {
|
||||
|
||||
written, err := rw.ResponseWriter.Write(data)
|
||||
rw.size += written
|
||||
|
||||
@@ -75,7 +76,9 @@ func LogHandler() func(http.Handler) http.Handler {
|
||||
next.ServeHTTP(wrapped, req)
|
||||
|
||||
header := wrapped.Header()["Content-Type"]
|
||||
contenttype := ""
|
||||
|
||||
var contenttype string
|
||||
|
||||
if header == nil {
|
||||
contenttype = "text/plain"
|
||||
} else {
|
||||
|
||||
@@ -85,7 +85,6 @@ func JsonStatus(resp http.ResponseWriter, code int, msg string) {
|
||||
Message: msg,
|
||||
Success: success,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user