-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogHandlers.go
72 lines (60 loc) · 1.67 KB
/
logHandlers.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
69
70
71
72
package main
import (
"encoding/json"
"net/http"
)
func createUploadLogHandler(broadcast *chan *message) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var msg anonymousMessage
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&msg)
if err != nil {
w.WriteHeader(500)
return
}
*broadcast <- newMessage(msg.Text)
}
}
func createDeleteLogHandler(messages *[]*message) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var messageId messageIdentifier
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&messageId)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("500 - Y U NO SEND logId?!"))
return
}
for i, msg := range *messages {
if msg.Id == messageId.Id {
msgVal := *messages
*messages = append(msgVal[:i], msgVal[i+1:]...)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("200 - that key is no more"))
return
}
}
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("404 - Key not found"))
}
}
func createLogDownloadHandler(messages *[]*message) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
keys, ok := r.URL.Query()["logId"]
if !ok || len(keys[0]) < 1 {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("500 - Y U NO SEND logId?!"))
return
}
logId := keys[0]
for _, msg := range *messages {
if msg.Id == logId {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(msg.Text))
return
}
}
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("500 - Y U SEND NOT EXISTING logId?!"))
}
}