-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (53 loc) · 1.58 KB
/
Copy pathmain.go
File metadata and controls
65 lines (53 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/rromulos/go-clean-sensitive-data/internal"
)
type RequestData struct {
Fields string `json:"fields"`
Nodes []string `json:"nodes"`
Data map[string]interface{} `json:"data"`
}
func main() {
http.HandleFunc("/filter", filterHandler)
log.Fatal(http.ListenAndServe(":7777", nil))
}
func filterHandler(w http.ResponseWriter, r *http.Request) {
// Check if the http method is POST
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Read the body of the request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}
// Decode the request body into a struct
var requestData RequestData
if err := json.Unmarshal(body, &requestData); err != nil {
http.Error(w, "invalid JSON", http.StatusBadRequest)
return
}
// Checks if the "data" field was provided
if requestData.Data == nil {
http.Error(w, "'data' field not found", http.StatusBadRequest)
return
}
// Filter JSON
filteredData := internal.FilterJSON(requestData.Data, requestData.Fields, requestData.Nodes)
// Converts the filtered JSON back to []byte
filteredBytes, err := json.Marshal(filteredData)
if err != nil {
http.Error(w, "Error converting filtered JSON", http.StatusInternalServerError)
return
}
// Sets the content-type of the response to application/json
w.Header().Set("Content-Type", "application/json")
// Write the response
w.Write(filteredBytes)
}