-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.go
80 lines (69 loc) · 2.08 KB
/
middlewares.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
package main
import (
"database/sql"
"math/rand"
texttospeech "cloud.google.com/go/texttospeech/apiv1"
log "github.com/golang/glog"
"github.com/jasonlvhit/gocron"
upstreamspotify "github.com/jchorl/spotify"
"github.com/valyala/fasthttp"
"github.com/jchorl/gowaker/plugin"
"github.com/jchorl/gowaker/requestcontext"
)
type middleware func(fasthttp.RequestHandler) fasthttp.RequestHandler
func dbMiddleware(db *sql.DB) middleware {
return func(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
requestcontext.SetDB(ctx, db)
handler(ctx)
}
}
}
func schedulerMiddleware(scheduler *gocron.Scheduler) middleware {
return func(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
requestcontext.SetScheduler(ctx, scheduler)
handler(ctx)
}
}
}
func spotifyMiddleware(client upstreamspotify.Client) middleware {
return func(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
requestcontext.SetSpotify(ctx, client)
handler(ctx)
}
}
}
func randMiddleware(r *rand.Rand) middleware {
return func(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
requestcontext.SetRand(ctx, r)
handler(ctx)
}
}
}
func speechMiddleware(client *texttospeech.Client) middleware {
return func(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
requestcontext.SetSpeech(ctx, client)
handler(ctx)
}
}
}
func pluginsMiddleware(plugins ...plugin.Plugin) middleware {
return func(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
requestcontext.SetPlugins(ctx, plugins)
handler(ctx)
}
}
}
func logMiddleware() middleware {
return func(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
handler(ctx)
log.Infof("CANONICAL-REQUEST-LINE method=%s path=%s status_code=%d", ctx.Method(), ctx.Path(), ctx.Response.StatusCode())
}
}
}