-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
62 lines (56 loc) · 1.93 KB
/
middleware.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
package ext
import (
"net/http"
mgo "gopkg.in/mgo.v2"
"github.com/aukbit/pluto/server/router"
"github.com/go-redis/redis"
"github.com/gocql/gocql"
"github.com/rs/zerolog"
)
// CassandraMiddleware creates a Cassandra session and add it to the context
func CassandraMiddleware(name string, cfg *gocql.ClusterConfig) router.Middleware {
return func(h router.HandlerFunc) router.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
s, err := gocql.NewSession(*cfg)
defer s.Close()
if err != nil {
zerolog.Ctx(r.Context()).Error().Msg(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx := WithContextAny(r.Context(), name, s)
// pass execution to the original handler
h.ServeHTTP(w, r.WithContext(ctx))
}
}
}
// MongoDBMiddleware creates a MongoDB session and add it to the context
func MongoDBMiddleware(name string, cfg *mgo.DialInfo) router.Middleware {
return func(h router.HandlerFunc) router.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
s, err := mgo.DialWithInfo(cfg)
defer s.Close()
if err != nil {
zerolog.Ctx(r.Context()).Error().Msg(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctx := WithContextAny(r.Context(), name, s)
// pass execution to the original handler
h.ServeHTTP(w, r.WithContext(ctx))
}
}
}
// RedisMiddleware adds Redis client to the context.
// Note: Caller is responsile to close the redis connection when its done.
// It is rare to Close a Client, as the Client is meant to be
// long-lived and shared between many goroutines.
func RedisMiddleware(name string, clt *redis.Client) router.Middleware {
return func(h router.HandlerFunc) router.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := WithContextAny(r.Context(), name, clt)
// pass execution to the original handler
h.ServeHTTP(w, r.WithContext(ctx))
}
}
}