Monorepo for Tangled
tangled.org
1package eventconsumer
2
3import (
4 "net/url"
5 "strconv"
6)
7
8type Kind string
9
10const (
11 KindKnot Kind = "knot"
12 KindSpindle Kind = "spindle"
13)
14
15type Source struct {
16 Kind Kind
17 Host string
18}
19
20func NewKnotSource(host string) Source { return Source{Kind: KindKnot, Host: host} }
21func NewSpindleSource(host string) Source { return Source{Kind: KindSpindle, Host: host} }
22
23func (s Source) Key() string { return string(s.Kind) + ":" + s.Host }
24
25func DefaultURL(dev bool) func(Source, int64) (*url.URL, error) {
26 scheme := "wss"
27 if dev {
28 scheme = "ws"
29 }
30 return func(s Source, cursor int64) (*url.URL, error) {
31 u, err := url.Parse(scheme + "://" + s.Host + "/events")
32 if err != nil {
33 return nil, err
34 }
35 if cursor != 0 {
36 q := url.Values{}
37 q.Add("cursor", strconv.FormatInt(cursor, 10))
38 u.RawQuery = q.Encode()
39 }
40 return u, nil
41 }
42}