This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
forked from adtac/commento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
164 lines (141 loc) · 4.28 KB
/
http.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"strconv"
"strings"
)
// resultContainer stores the results of a request
type resultContainer struct {
Status int `json:"-"`
Success bool `json:"success"`
Message string `json:"message"`
Comments []Comment `json:"comments,omitempty"`
}
// IndexHandler handles GET requests to the root path '/'
func IndexHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintf(w, "")
}
// render writes a resultContainer to a response stream
func (res *resultContainer) render(w http.ResponseWriter) {
if res == nil {
res = &resultContainer{
Status: http.StatusInternalServerError,
Success: false,
Message: "Some internal error occurred",
Comments: nil,
}
}
w.Header().Set("Access-Control-Allow-Origin", "*")
if res.Status == 0 {
res.Status = 200
}
w.WriteHeader(res.Status)
json, err := json.Marshal(res)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"Success":false,"Message":"Internal Server Error"}`))
return
}
w.Write(json)
}
// CreateCommentHandler handles the '/create' endpoint that is used to create a
// new comment. It requires the following POST request body values:
// - name: the name of the comment author
// - parent: ID of the parent comment
// - comment: the comment text itself
// - url: the URL associated with this comment
func CreateCommentHandler(w http.ResponseWriter, r *http.Request) {
result := &resultContainer{}
var err error
if r.Method != "POST" {
result.Status = http.StatusMethodNotAllowed
result.Message = errorList["err.request.method.invalid"].Error()
result.render(w)
return
}
requiredFields := []string{"name", "parent", "comment", "url"}
for _, field := range requiredFields {
if strings.TrimSpace(r.PostFormValue(field)) == "" {
result.Status = http.StatusBadRequest
result.Message = errorList["err.request.field.missing"].Error()
result.render(w)
return
}
}
if r.PostFormValue("gotcha") != "" {
result.Success = true
result.Message = "Comment successfully created"
result.render(w)
return
}
comment := Comment{}
comment.Name = template.HTMLEscapeString(r.PostFormValue("name"))
comment.Comment = template.HTMLEscapeString(r.PostFormValue("comment"))
comment.URL = r.PostFormValue("url")
comment.Parent, err = strconv.Atoi(r.PostFormValue("parent"))
if err != nil || comment.Parent < -1 {
result.Status = http.StatusBadRequest
result.Message = errorList["err.request.field.invalid"].Error()
result.render(w)
return
}
if isSpam := checkSpam(r, comment.URL, comment.Name, comment.Comment); isSpam {
// Silently fail. Don't tell the spammer we detected their comment.
result.Success = true
result.Message = "Comment successfully created"
result.render(w)
return
}
err = db.CreateComment(&comment)
if err != nil {
result.Status = http.StatusInternalServerError
result.Message = errorList["err.internal"].Error()
fmt.Println("Error:", err)
result.render(w)
return
}
result.Success = true
result.Message = "Comment successfully created"
result.render(w)
}
// GetCommentsHandler handles the '/get' endpoint that is used to retrieve
// all the comments for a particular URL. It takes one value:
// - url: the URL associated with this comment
func GetCommentsHandler(w http.ResponseWriter, r *http.Request) {
result := &resultContainer{}
comments := []Comment{}
var err error
if r.Method != "POST" {
result.Status = http.StatusMethodNotAllowed
result.Message = errorList["err.request.method.invalid"].Error()
result.render(w)
return
}
requiredFields := []string{"url"}
for _, field := range requiredFields {
if strings.TrimSpace(r.PostFormValue(field)) == "" {
result.Status = http.StatusBadRequest
result.Message = errorList["err.request.field.missing"].Error()
result.render(w)
return
}
}
comments, err = db.GetComments(r.PostFormValue("url"))
if err != nil {
result.Status = http.StatusInternalServerError
result.Message = errorList["err.internal"].Error()
fmt.Println("Error:", err)
result.render(w)
return
}
for i := range comments {
comments[i].Html = sanitisedHTML(comments[i].Comment)
}
result.Success = true
result.Comments = comments
result.render(w)
}