-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
66 lines (60 loc) · 2.11 KB
/
handler.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
package gqlws
import (
"context"
"errors"
"net/http"
)
// Subscriber lets you use any GraphQL implementation you want
type Subscriber func(ctx context.Context, query string, operationName string, variables map[string]interface{}) (<-chan interface{}, error)
// Config for the GraphQL subscriptions over WebSocket
type Config struct {
// CheckOrigin can check the origin of a request, by default it allows everything
CheckOrigin func(*http.Request) bool
// UpgradeRule lets you define your own rule to limit which request you want to upgrade to subscriptions and which you do not
UpgradeRule func(*http.Request) bool
// Subscriber is a function from the GraphQL implementation you use to provide a result channer for gqlws
Subscriber Subscriber
// OnConnect is called at the initialization of a new connection with the request context and the init message's payload that potentially contains the "authToken"
// and other fields specified in the client
OnConnect func(context.Context, map[string]interface{}) (context.Context, error)
}
// New returns a new handler with the given config
func New(c Config, next http.Handler) http.Handler {
h := &handler{
conf: Config{
CheckOrigin: func(r *http.Request) bool { return true },
UpgradeRule: func(r *http.Request) bool { return true },
Subscriber: func(ctx context.Context, query string, operationName string, variables map[string]interface{}) (<-chan interface{}, error) {
return nil, errors.New("no subscriber function provided")
},
OnConnect: func(ctx context.Context, _ map[string]interface{}) (context.Context, error) {
return ctx, nil
},
},
next: next,
}
if c.CheckOrigin != nil {
h.conf.CheckOrigin = c.CheckOrigin
}
if c.UpgradeRule != nil {
h.conf.UpgradeRule = c.UpgradeRule
}
if c.Subscriber != nil {
h.conf.Subscriber = c.Subscriber
}
if c.OnConnect != nil {
h.conf.OnConnect = c.OnConnect
}
return h
}
type handler struct {
conf Config
next http.Handler
}
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if shouldUpgrade(r) && h.conf.UpgradeRule(r) {
h.subscription(w, r)
return
}
h.next.ServeHTTP(w, r)
}