/
sgblip_context.go
49 lines (39 loc) · 1.14 KB
/
sgblip_context.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
package cmd
import (
"fmt"
"net/url"
"github.com/couchbase/go-blip"
"log"
"golang.org/x/net/websocket"
)
type SgBlipContext struct {
BlipContext *blip.Context
BlipSender *blip.Sender
}
func NewSgBlipContext(serverUrl string) (*SgBlipContext, error) {
// Construct URL to connect to blipsync target endpoint
destUrl := fmt.Sprintf("%s/_blipsync", serverUrl)
u, err := url.Parse(destUrl)
if err != nil {
return nil, fmt.Errorf("Error parsing url: %v", destUrl)
}
u.Scheme = "ws"
// Make BLIP/Websocket connection
blipContext := blip.NewContext(BlipCBMobileReplication)
blipContext.Logger = func(eventType blip.LogEventType, fmt string, params ...interface{}) {
log.Printf(fmt, params...)
}
origin := "http://localhost" // TODO: what should be used here?
config, err := websocket.NewConfig(u.String(), origin)
if err != nil {
return nil, fmt.Errorf("Error creating websocket config. Error: %v", err)
}
sender, err := blipContext.DialConfig(config)
if err != nil {
return nil, fmt.Errorf("Error connecting to Sync Gateway. Error: %v", err)
}
return &SgBlipContext{
BlipContext: blipContext,
BlipSender: sender,
}, nil
}