This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.go
120 lines (101 loc) · 3.26 KB
/
api.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package gossip
import (
"encoding/json"
"errors"
"fmt"
"html"
"net/http"
"strconv"
"strings"
"github.com/parkr/gossip/response"
"github.com/parkr/gossip/serializer"
)
func (h *Handler) API(w http.ResponseWriter, r *http.Request) {
if err := authenticate(r); err != nil {
http.Error(w, response.New().WithError(err).Json(), http.StatusUnauthorized)
return
}
switch r.URL.Path {
case "/api/messages/latest":
h.FetchLatestMessages(w, r)
case "/api/messages/log":
h.StoreMessage(w, r)
default:
if strings.HasPrefix(r.URL.Path, "/api/messages/") {
h.FindMessageById(w, r)
} else {
http.Error(w, fmt.Sprintf("404 Not Found: %s", r.URL.Path), http.StatusNotFound)
}
}
}
func (h *Handler) FindMessageById(w http.ResponseWriter, r *http.Request) {
idStr := strings.TrimPrefix(r.URL.Path, "/api/messages/")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "You must submit a numerical ID to lookup.", 400)
return
}
message, err := h.DB.Find(id)
if err != nil {
errMsg := fmt.Sprintf("Could not fetch message id=%d: %s", id, err.Error())
http.Error(w, response.New().WithError(errors.New(errMsg)).Json(), 500)
return
}
fmt.Fprint(w, response.New().WithMessage(message).Json())
}
func (h *Handler) FetchLatestMessages(w http.ResponseWriter, r *http.Request) {
limit := 10
limitStr := r.FormValue("limit")
if limitStr != "" {
var err error
limit, err = strconv.Atoi(limitStr)
if err != nil {
LogWithRequestID(r, fmt.Sprintf("Error parsing limit '%s': %+v", limitStr, err))
limit = 10
}
}
LogWithRequestID(r, fmt.Sprintf("Fetching latest %d messages", limit))
messages, err := h.DB.LatestMessages(limit)
if err != nil {
errMsg := fmt.Sprintf("Could not fetch latest messages with limit=%d: %s", limit, err.Error())
http.Error(w, response.New().WithError(errors.New(errMsg)).Json(), 500)
return
}
fmt.Fprint(w, response.New().WithMessages(messages).WithLimit(limit).Json())
}
func (h *Handler) StoreMessage(w http.ResponseWriter, r *http.Request) {
var msg map[string]interface{}
if r.Header.Get("Content-Type") == "application/json" {
if err := json.NewDecoder(r.Body).Decode(&msg); err != nil {
fmt.Fprint(w, response.New().WithError(err).Json())
return
}
} else {
room := r.PostFormValue("room")
if room == "" {
fmt.Fprint(w, response.New().WithError(errors.New("No room specified. Skipping.")).Json())
return
}
msg = map[string]interface{}{
"room": html.EscapeString(r.PostFormValue("room")),
"author": html.EscapeString(r.PostFormValue("author")),
"message": html.EscapeString(r.PostFormValue("message")),
"at": serializer.ParseJavaScriptTime(r.PostFormValue("time")),
}
}
for _, author := range h.SkippedAuthors() {
if msg["author"] == author {
LogWithRequestID(r, fmt.Sprintf("no messages from %s allowed", author))
http.Error(w, response.New().WithError(fmt.Errorf("no messages from %s allowed", author)).Json(), 200)
return
}
}
LogWithRequestID(r, fmt.Sprintf("Inserting %+v", msg))
message, err := h.DB.InsertMessage(msg)
if err != nil {
errMsg := fmt.Sprintf("Could not insert message %s: %s", msg, err.Error())
http.Error(w, response.New().WithError(errors.New(errMsg)).Json(), 500)
return
}
fmt.Fprint(w, response.New().WithMessage(message).Json())
}