Skip to content

Commit 31232c2

Browse files
author
Jannis Pohlmann
committed
handler: Add a handler config with a UserFromAuthToken function
1 parent 51b2dbd commit 31232c2

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

handler.go

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ import (
77
log "github.com/sirupsen/logrus"
88
)
99

10+
// HandlerConfig stores the configuration of a GraphQL WebSocket handler.
11+
type HandlerConfig struct {
12+
SubscriptionManager SubscriptionManager
13+
UserFromAuthToken UserFromAuthTokenFunc
14+
}
15+
1016
// NewHandler creates a WebSocket handler for GraphQL WebSocket connections.
1117
// This handler takes a SubscriptionManager and adds/removes subscriptions
1218
// as they are started/stopped by the client.
13-
func NewHandler(subscriptionManager SubscriptionManager) http.Handler {
19+
func NewHandler(config HandlerConfig) http.Handler {
1420
// Create a WebSocket upgrader that requires clients to implement
1521
// the "graphql-ws" protocol
1622
var upgrader = websocket.Upgrader{
@@ -19,6 +25,7 @@ func NewHandler(subscriptionManager SubscriptionManager) http.Handler {
1925
}
2026

2127
logger := NewLogger("handler")
28+
subscriptionManager := config.SubscriptionManager
2229

2330
// Create a map (used like a set) to manage client connections
2431
var connections = make(map[Connection]bool)
@@ -42,40 +49,46 @@ func NewHandler(subscriptionManager SubscriptionManager) http.Handler {
4249
}
4350

4451
// Establish a GraphQL WebSocket connection
45-
conn := NewConnection(ws, &ConnectionEventHandlers{
46-
Close: func(conn Connection) {
47-
logger.WithFields(log.Fields{
48-
"conn": conn.ID(),
49-
}).Debug("Closing connection")
52+
conn := NewConnection(ws, ConnectionConfig{
53+
UserFromAuthToken: config.UserFromAuthToken,
54+
EventHandlers: ConnectionEventHandlers{
55+
Close: func(conn Connection) {
56+
logger.WithFields(log.Fields{
57+
"conn": conn.ID(),
58+
"user": conn.User(),
59+
}).Debug("Closing connection")
5060

51-
subscriptionManager.RemoveSubscriptions(conn)
61+
subscriptionManager.RemoveSubscriptions(conn)
5262

53-
delete(connections, conn)
54-
},
55-
StartOperation: func(
56-
conn Connection,
57-
opID string,
58-
data *StartMessagePayload,
59-
) []error {
60-
logger.WithFields(log.Fields{
61-
"conn": conn.ID(),
62-
"op": opID,
63-
}).Debug("Start operation")
63+
delete(connections, conn)
64+
},
65+
StartOperation: func(
66+
conn Connection,
67+
opID string,
68+
data *StartMessagePayload,
69+
) []error {
70+
logger.WithFields(log.Fields{
71+
"conn": conn.ID(),
72+
"op": opID,
73+
"user": conn.User(),
74+
}).Debug("Start operation")
6475

65-
return subscriptionManager.AddSubscription(conn, &Subscription{
66-
ID: opID,
67-
Query: data.Query,
68-
Variables: data.Variables,
69-
OperationName: data.OperationName,
70-
SendData: func(subscription *Subscription, data *DataMessagePayload) {
71-
conn.SendData(opID, data)
72-
},
73-
})
74-
},
75-
StopOperation: func(conn Connection, opID string) {
76-
subscriptionManager.RemoveSubscription(conn, &Subscription{
77-
ID: opID,
78-
})
76+
return subscriptionManager.AddSubscription(conn, &Subscription{
77+
ID: opID,
78+
Query: data.Query,
79+
Variables: data.Variables,
80+
OperationName: data.OperationName,
81+
Connection: conn,
82+
SendData: func(subscription *Subscription, data *DataMessagePayload) {
83+
conn.SendData(opID, data)
84+
},
85+
})
86+
},
87+
StopOperation: func(conn Connection, opID string) {
88+
subscriptionManager.RemoveSubscription(conn, &Subscription{
89+
ID: opID,
90+
})
91+
},
7992
},
8093
})
8194
connections[conn] = true

0 commit comments

Comments
 (0)