-
Notifications
You must be signed in to change notification settings - Fork 4
/
responses.go
68 lines (49 loc) · 1.61 KB
/
responses.go
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
66
67
68
package web
import (
"encoding/json"
"net/http"
"strconv"
"github.com/asalih/http-auto-responder/responder"
)
func init() {
HTTPHandlerFuncs["/http-auto-responder/save-response"] = func(w http.ResponseWriter, r *http.Request, ar responder.AutoResponder) {
w.Header().Set("Content-Type", "application/json")
decoder := json.NewDecoder(r.Body)
var response responder.Response
err := decoder.Decode(&response)
if err != nil {
panic(err)
}
ar.AddOrUpdateResponse(&response)
w.Write([]byte(`{"status": "OK"}`))
}
HTTPHandlerFuncs["/http-auto-responder/get-responses"] = func(w http.ResponseWriter, r *http.Request, ar responder.AutoResponder) {
w.Header().Set("Content-Type", "application/json")
responses := ar.GetResponses()
encoder := json.NewEncoder(w)
encoder.Encode(responses)
}
HTTPHandlerFuncs["/http-auto-responder/get-response"] = func(w http.ResponseWriter, r *http.Request, ar responder.AutoResponder) {
w.Header().Set("Content-Type", "application/json")
idq := r.URL.Query().Get("id")
id, err := strconv.ParseUint(idq, 0, 64)
if err != nil || idq == "" {
http.NotFound(w, r)
return
}
response := ar.GetResponse(id)
encoder := json.NewEncoder(w)
encoder.Encode(response)
}
HTTPHandlerFuncs["/http-auto-responder/remove-response"] = func(w http.ResponseWriter, r *http.Request, ar responder.AutoResponder) {
w.Header().Set("Content-Type", "application/json")
idq := r.URL.Query().Get("id")
id, err := strconv.ParseUint(idq, 0, 64)
if err != nil {
http.NotFound(w, r)
return
}
ar.RemoveResponse(id)
w.Write([]byte(`{"status": "OK"}`))
}
}