From 169fb5e17e54b76558b31249ec55ed03a90325dd Mon Sep 17 00:00:00 2001 From: Alex Goodisman Date: Thu, 4 Apr 2024 16:13:24 -0400 Subject: [PATCH] stuff --- go.mod | 2 +- lib/denylist/http.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index c55ba9cc..83dac2b6 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/alicebob/miniredis v2.5.0+incompatible github.com/deckarep/golang-set v1.7.1 github.com/go-redis/redis/v7 v7.4.1 + github.com/go-redis/redis/v8 v8.11.5 github.com/gorilla/websocket v1.4.2 github.com/juju/mgo/v2 v2.0.0-20210302023703-70d5d206e208 github.com/juju/replicaset v0.0.0-20210302050932-0303c8575745 @@ -29,7 +30,6 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect - github.com/go-redis/redis/v8 v8.11.5 // indirect github.com/golang/protobuf v1.4.3 // indirect github.com/golang/snappy v0.0.1 // indirect github.com/gomodule/redigo v1.8.5 // indirect diff --git a/lib/denylist/http.go b/lib/denylist/http.go index 0206b135..43d2b510 100644 --- a/lib/denylist/http.go +++ b/lib/denylist/http.go @@ -1,7 +1,9 @@ package denylist import ( + "encoding/json" "net/http" + "strings" ) // CollectionEndpoint serves the endpoints for the whole Denylist at /denylist @@ -42,8 +44,32 @@ func createDenylistEntry(response http.ResponseWriter, request *http.Request, de // GET /denylist/... func getDenylistEntry(response http.ResponseWriter, request *http.Request, denylist *Denylist) { + id := request.URL.Path + entry := denylist.GetEntry(id) + if entry == nil { + http.Error(response, "denylist entry not found with that id", http.StatusNotFound) + return + } + + payload := map[string]string{ + "keys": strings.Join(entry.Keys, KeysSeparator), + "regex": entry.Regex.String(), + } + + response.Header().Set("Content-Type", "application/json") + json.NewEncoder(response).Encode(payload) + response.WriteHeader(http.StatusOK) } // DELETE /denylist/... func deleteDenylistEntry(response http.ResponseWriter, request *http.Request, denylist *Denylist) { + id := request.URL.Path + deleted := denylist.DeleteEntry(id) + + if !deleted { + http.Error(response, "denylist entry not found with that id", http.StatusNotFound) + return + } + + response.WriteHeader(http.StatusNoContent) }