This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
server.go
231 lines (205 loc) · 6.07 KB
/
server.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/labstack/echo"
"github.com/labstack/gommon/log"
)
var TEST_MODE = false
type Template struct{ templates *template.Template }
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
e := echo.New()
e.Logger.SetLevel(log.DEBUG)
db, err := sql.Open("sqlite3", "./database.sqlite")
if err != nil {
e.Logger.Error(err)
}
defer db.Close()
TEST_MODE = os.Getenv("TEST_MODE") != ""
adsFName := os.Getenv("ADS")
var ads template.HTML
if adsFName != "" {
data, err := ioutil.ReadFile(adsFName)
if err != nil {
e.Logger.Errorf("couldn't read file %s: %v", adsFName, err)
}
ads = mdTmplHTML(data)
}
go flushStatsLoop(e.Logger, db)
e.Renderer = &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))}
e.File("/favicon.ico", "assets/public/favicon.ico")
e.File("/robots.txt", "assets/public/robots.txt")
e.File("/style.css", "assets/public/style.css")
e.File("/new.js", "assets/public/new.js")
e.File("/note.js", "assets/public/note.js")
e.File("/index.html", "assets/public/index.html")
e.File("/", "assets/public/index.html")
e.GET("/TOS.md", func(c echo.Context) error {
n, code := md2html(c, "TOS")
if code != http.StatusOK {
c.String(code, statuses[code])
}
return c.Render(code, "Page", n)
})
e.GET("/:id", func(c echo.Context) error {
id := c.Param("id")
n, code := load(c, db)
if code != http.StatusOK {
c.Logger().Errorf("/%s failed (code: %d)", id, code)
return c.String(code, statuses[code])
}
defer incViews(n, db)
n.Ads = ads
c.Logger().Debugf("/%s delivered (fraud: %t)", id, n.Fraud())
return c.Render(code, "Note", n)
})
e.GET("/:id/export", func(c echo.Context) error {
id := c.Param("id")
n, code := load(c, db)
var content string
if code == http.StatusOK {
defer incViews(n, db)
if n.Fraud() {
code = http.StatusForbidden
content = statuses[code]
c.Logger().Warnf("/%s/export failed (code: %d)", id, code)
} else {
content = n.Text
c.Logger().Debugf("/%s/export delivered", id)
}
}
return c.String(code, content)
})
e.GET("/:id/stats", func(c echo.Context) error {
id := c.Param("id")
n, code := load(c, db)
if code != http.StatusOK {
c.Logger().Errorf("/%s/stats failed (code: %d)", id, code)
return c.String(code, statuses[code])
}
stats := fmt.Sprintf("Published: %s\n Views: %d", n.Published, n.Views)
if !n.Edited.IsZero() {
stats = fmt.Sprintf("Published: %s\n Edited: %s\n Views: %d", n.Published, n.Edited, n.Views)
}
return c.String(code, stats)
})
e.GET("/:id/edit", func(c echo.Context) error {
id := c.Param("id")
n, code := load(c, db)
if code != http.StatusOK {
c.Logger().Errorf("/%s/edit failed (code: %d)", id, code)
return c.String(code, statuses[code])
}
c.Logger().Debugf("/%s/edit delivered", id)
return c.Render(code, "Form", n)
})
e.POST("/:id/report", func(c echo.Context) error {
report := c.FormValue("report")
if report != "" {
id := c.Param("id")
c.Logger().Infof("note %s was reported: %s", id, report)
if err := email(id, report); err != nil {
c.Logger().Errorf("couldn't send email: %v", err)
}
}
return c.NoContent(http.StatusNoContent)
})
e.GET("/new", func(c echo.Context) error {
c.Logger().Debug("/new opened")
return c.Render(http.StatusOK, "Form", nil)
})
type postResp struct {
Success bool
Payload string
}
e.POST("/", func(c echo.Context) error {
c.Logger().Debug("POST /")
if !TEST_MODE && !checkRecaptcha(c, c.FormValue("token")) {
code := http.StatusForbidden
return c.JSON(code, postResp{false, statuses[code] + ": robot check failed"})
}
if c.FormValue("tos") != "on" {
code := http.StatusPreconditionFailed
c.Logger().Errorf("POST / error: %d", code)
return c.JSON(code, postResp{false, statuses[code]})
}
id := c.FormValue("id")
text := c.FormValue("text")
l := len(text)
if (id == "" || id != "" && l != 0) && (10 > l || l > 50000) {
code := http.StatusBadRequest
c.Logger().Errorf("POST / error: %d", code)
return c.JSON(code, postResp{false, statuses[code] + ": note length not accepted"})
}
n := &Note{
ID: id,
Text: text,
Password: c.FormValue("password"),
}
n, err = save(c, db, n)
if err != nil {
c.Logger().Error(err)
code := http.StatusServiceUnavailable
if err == errorUnathorised {
code = http.StatusUnauthorized
} else if err == errorBadRequest {
code = http.StatusBadRequest
}
c.Logger().Errorf("POST / error: %d", code)
return c.JSON(code, postResp{false, statuses[code] + ": " + err.Error()})
}
if id == "" {
c.Logger().Infof("note %s created", n.ID)
return c.JSON(http.StatusCreated, postResp{true, n.ID})
} else if text == "" {
c.Logger().Infof("note %s deleted", n.ID)
} else {
c.Logger().Infof("note %s updated", n.ID)
}
return c.JSON(http.StatusOK, postResp{true, n.ID})
})
s := &http.Server{
Addr: ":3000",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
e.Logger.Fatal(e.StartServer(s))
}
func checkRecaptcha(c echo.Context, captchaResp string) bool {
resp, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify", url.Values{
"secret": []string{os.Getenv("RECAPTCHA_SECRET")},
"response": []string{captchaResp},
"remoteip": []string{c.Request().RemoteAddr},
})
if err != nil {
c.Logger().Errorf("captcha response verification failed: %v", err)
return false
}
defer resp.Body.Close()
respJson := &struct {
Success bool `json:"success"`
ErrorCodes []string `json:"error-codes"`
}{}
s, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(s, respJson)
if err != nil {
c.Logger().Errorf("captcha response parse recaptcha response: %v", err)
return false
}
if !respJson.Success {
c.Logger().Warnf("captcha validation failed: %v", respJson.ErrorCodes)
}
return respJson.Success
}