-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.go
358 lines (301 loc) · 8.65 KB
/
app.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"bytes"
"database/sql"
"encoding/json"
"encoding/xml"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"sync"
"time"
"fknsrs.biz/p/bcache"
"github.com/GeertJohan/go.rice"
"github.com/Sirupsen/logrus"
"github.com/boltdb/bolt"
"github.com/gorilla/sessions"
"github.com/pkg/errors"
"github.com/timewasted/go-accept-headers"
"fknsrs.biz/p/don/activitystreams"
"fknsrs.biz/p/don/pubsub"
"fknsrs.biz/p/don/react"
)
type App struct {
SQLDB DB
BoltDB *bolt.DB
Store sessions.Store
Renderer react.Renderer
Template *template.Template
BuildBox *rice.Box
listeners map[chan *ActivityEvent]struct{}
listenerLock sync.RWMutex
AccountURLCache *bcache.Cache
FeedCache *bcache.Cache
}
func NewApp(sqlDB *sql.DB, boltDB *bolt.DB, store sessions.Store, renderer react.Renderer, template *template.Template, buildBox *rice.Box) (*App, error) {
db := &dbLogger{db: sqlDB}
a := &App{
SQLDB: db,
BoltDB: boltDB,
Store: store,
Renderer: renderer,
Template: template,
BuildBox: buildBox,
listeners: make(map[chan *ActivityEvent]struct{}),
}
if *sqlQueryLog {
db.l = append(db.l, func(begin time.Time, dur time.Duration, name, file string, line int, transactionID string, sql string, vars []interface{}) {
fmt.Printf("%s (%s) %s:%s:%d [tx/%s]\n%s\n", begin.Format(time.RFC3339), dur.String(), name, file, line, transactionID, printQuery(sql, vars))
})
}
a.AccountURLCache = bcache.New(
"account_url",
bcache.SetDB(boltDB),
bcache.SetWorker(fetchAccountURL),
bcache.SetMaxAge(time.Hour*24*7),
bcache.SetHighMark(32*1024),
bcache.SetLowMark(30*1024),
bcache.SetStrategy(bcache.StrategyLFU()),
bcache.SetKeepErrors(true),
)
if err := a.AccountURLCache.ForceInit(); err != nil {
return nil, err
}
a.FeedCache = bcache.New(
"feed",
bcache.SetDB(boltDB),
bcache.SetWorker(func(key string, _ interface{}) ([]byte, error) {
res, err := http.Get(key)
if err != nil {
return nil, errors.Wrap(err, "feedFetch: couldn't make request")
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, errors.Errorf("feedFetch: invalid status code; expected 200 but got %d", res.StatusCode)
}
d, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "feedFetch: couldn't make request")
}
return d, nil
}),
bcache.SetMaxAge(time.Minute),
bcache.SetHighMark(1024),
bcache.SetLowMark(1000),
bcache.SetStrategy(bcache.StrategyLFU()),
bcache.SetKeepErrors(true),
)
if err := a.FeedCache.ForceInit(); err != nil {
return nil, err
}
return a, nil
}
func (a *App) AddListener(ch chan *ActivityEvent) {
a.listenerLock.Lock()
defer a.listenerLock.Unlock()
a.listeners[ch] = struct{}{}
}
func (a *App) RemoveListener(ch chan *ActivityEvent) {
a.listenerLock.Lock()
defer a.listenerLock.Unlock()
delete(a.listeners, ch)
}
func (a *App) Emit(ev *ActivityEvent) error {
if ev.JSON == nil {
d, err := json.Marshal(ev.Activity)
if err != nil {
return err
}
ev.JSON = d
}
a.listenerLock.RLock()
defer a.listenerLock.RUnlock()
for ch := range a.listeners {
select {
case ch <- ev:
// nothing
default:
close(ch)
}
}
return nil
}
func (a *App) OnMessage(id string, s *pubsub.Subscription, rd io.ReadCloser) {
var f activitystreams.Feed
if *recordDocuments {
d, err := ioutil.ReadAll(rd)
if err != nil {
logrus.WithField("id", id).WithError(err).Debug("pubsub: couldn't read message")
return
}
if err := xml.NewDecoder(bytes.NewReader(d)).Decode(&f); err != nil {
logrus.WithField("id", id).WithError(err).Debug("pubsub: couldn't parse body")
return
}
if _, err := a.SQLDB.Exec("insert into pubsub_documents (created_at, xml) values ($1, $2)", time.Now(), string(d)); err != nil {
logrus.WithField("id", id).WithError(err).Debug("pubsub: couldn't save document")
return
}
} else {
if err := xml.NewDecoder(rd).Decode(&f); err != nil {
logrus.WithField("id", id).WithError(err).Debug("pubsub: couldn't parse body")
return
}
}
var l *logrus.Entry
if s == nil {
l = logrus.WithField("id", id)
} else {
l = logrus.WithFields(logrus.Fields{
"id": s.ID,
"hub": s.Hub,
"topic": s.Topic,
})
}
for _, e := range f.Activities {
if err := a.saveActivity(&e); err != nil {
l.WithError(err).Debug("pubsub: couldn't save entry")
} else {
l.Debug("pubsub: saved entry")
}
}
}
func (a *App) getSessionAndUserFromRequest(r *http.Request) (*sessions.Session, *User, error) {
s, err := a.Store.Get(r, "login")
if err != nil {
return nil, nil, errors.Wrap(err, "App.getSessionAndUserFromRequest")
}
userIDValue, ok := s.Values["user_id"]
if !ok {
return s, nil, nil
}
userID, ok := userIDValue.(string)
if !ok {
return s, nil, errors.Errorf("App.getSessionAndUserFromRequest: invalid type %T for user_id", userIDValue)
}
var u User
if err := a.SQLDB.QueryRow("select id, created_at, username, email, display_name, avatar from users where id = $1", userID).Scan(&u.ID, &u.CreatedAt, &u.Username, &u.Email, &u.DisplayName, &u.Avatar); err != nil {
if err == sql.ErrNoRows {
return s, nil, nil
}
return s, nil, errors.Wrap(err, "App.getSessionAndUserFromRequest")
}
return s, &u, nil
}
type AppHandlerFunc func(r *http.Request, ar *AppResponse) *AppResponse
func (a *App) HandlerFor(fn AppHandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
ar, err := a.StandardContext(rw, r)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
if err := a.SendResponse(rw, r, fn(r, ar)); err != nil {
logrus.WithError(err).Warn("error sending response")
}
}
}
func (a *App) StandardContext(rw http.ResponseWriter, r *http.Request) (*AppResponse, error) {
s, u, err := a.getSessionAndUserFromRequest(r)
if err != nil {
return nil, errors.Wrap(err, "App.StandardContext")
}
return NewAppResponse().WithSession(s).WithUser(u).ShallowMergeState(map[string]interface{}{
"authentication": map[string]interface{}{
"loading": false,
"error": nil,
"user": u,
},
}), nil
}
func (a *App) SendResponse(rw http.ResponseWriter, r *http.Request, ar *AppResponse) error {
acceptable := accept.Parse(r.Header.Get("accept"))
ct, err := acceptable.Negotiate("text/html", "application/json")
if err != nil {
ar = ar.WithError(err)
}
if ar.Session != nil {
if ar.User == nil {
delete(ar.Session.Values, "user_id")
} else {
ar.Session.Values["user_id"] = ar.User.ID
}
if err := ar.Session.Save(r, rw); err != nil {
ar = ar.WithError(err)
}
}
if ar.Error != nil {
ar = ar.ShallowMergeState(map[string]interface{}{
"server": map[string]interface{}{
"error": ar.Error.Error(),
},
})
} else {
ar = ar.ShallowMergeState(map[string]interface{}{
"server": map[string]interface{}{
"error": nil,
},
})
}
if ar.Error != nil && ct == "application/json" {
status := http.StatusSeeOther
if ar.Status != 0 {
status = ar.Status
}
http.Error(rw, ar.Error.Error(), status)
return nil
}
if ar.Redirect != "" && (ct == "text/html" || ct == "") {
status := http.StatusSeeOther
if ar.Status != 0 {
status = ar.Status
}
http.Redirect(rw, r, ar.Redirect, status)
return nil
}
d, err := json.Marshal(ar.State)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return errors.Wrap(err, "App.render")
}
switch ct {
case "application/json":
rw.Header().Set("content-type", "application/json; charset=utf8")
if ar.Status != 0 {
rw.WriteHeader(ar.Status)
}
if _, err := io.Copy(rw, bytes.NewReader(d)); err != nil {
return errors.Wrap(err, "App.render")
}
case "text/html", "":
html, err := a.Renderer.Render(a.BuildBox.MustString("entry-server-bundle.js"), r.URL.String(), string(d))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return errors.Wrap(err, "App.render")
}
data := map[string]interface{}{
"HTML": template.HTML(html),
"JSON": template.JS(d),
"CSSFiles": []string{"/build/vendor-styles.css", "/build/entry-client-styles.css"},
"JSFiles": []string{"/build/vendor-bundle.js", "/build/entry-client-bundle.js"},
"Meta": map[string]interface{}{
"Title": ar.GetMeta("title", "DON"),
"Description": ar.GetMeta("description", "A very basic StatusNet node. Kind of like Mastodon, but worse."),
},
}
if *externalJS != "" {
data["CSSFiles"] = []string{}
data["JSFiles"] = []string{*externalJS}
}
rw.Header().Set("content-type", "text/html; charset=utf-8")
if ar.Status != 0 {
rw.WriteHeader(ar.Status)
}
if a.Template.Execute(rw, data); err != nil {
return errors.Wrap(err, "App.render")
}
}
return nil
}