Skip to content

Commit

Permalink
stub out http
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-goodisman committed Apr 4, 2024
1 parent 3630f2c commit 889f52d
Showing 1 changed file with 0 additions and 59 deletions.
59 changes: 0 additions & 59 deletions lib/denylist/http.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package denylist

import (
"encoding/json"
"net/http"
"strings"
)

// CollectionEndpoint serves the endpoints for the whole Denylist at /denylist
Expand Down Expand Up @@ -36,73 +34,16 @@ func SingleEndpoint(denylist *Denylist) func(http.ResponseWriter, *http.Request)

// GET /denylist
func listDenylistKeys(response http.ResponseWriter, denylist *Denylist) {
keys := denylist.GetKeys()

response.Header().Set("Content-Type", "application/json")
json.NewEncoder(response).Encode(keys)
response.WriteHeader(http.StatusOK)
}

// PUT /denylist
func createDenylistEntry(response http.ResponseWriter, request *http.Request, denylist *Denylist) {
if request.Header.Get("Content-Type") != "application/json" {
http.Error(response, "request must be JSON", http.StatusBadRequest)
return
}
decoder := json.NewDecoder(request.Body)
var payload map[string]string
err := decoder.Decode(&payload)
if err != nil {
http.Error(response, err.Error(), http.StatusBadRequest)
return
}

unparsedKeys, keysOk := payload["keys"]
unparsedRegex, regexOk := payload["regex"]
if !keysOk || !regexOk {
http.Error(response, "request body must contain `keys` and `regex`", http.StatusBadRequest)
return
}

id, err := denylist.AppendEntry(unparsedKeys, unparsedRegex)
if err != nil {
http.Error(response, err.Error(), http.StatusBadRequest)
return
}

response.Header().Set("Content-Type", "application/json")
json.NewEncoder(response).Encode(id)
response.WriteHeader(http.StatusCreated)
}

// 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)
}

0 comments on commit 889f52d

Please sign in to comment.