websocket-based lrcproto server
1package lrcd
2
3import (
4 "context"
5 "errors"
6 "io"
7
8 lrcpb "github.com/rachel-mp4/lrcproto/gen/go"
9)
10
11type InitChanMsg struct {
12 Init lrcpb.Event_Init
13 ResolvedId *string
14}
15
16type MediaInitChanMsg struct {
17 Mediainit lrcpb.Event_Mediainit
18 ResolvedId *string
19}
20
21type options struct {
22 uri string
23 secret string
24 welcome *string
25 writer *io.Writer
26 verbose bool
27 pubChan chan PubEvent
28 initChan chan InitChanMsg
29 mediainitChan chan MediaInitChanMsg
30 initialID *uint32
31 resolver func(externalID string, ctx context.Context) *string
32}
33
34type Option func(option *options) error
35
36func WithResolver(f func(externalID string, ctx context.Context) *string) Option {
37 return func(options *options) error {
38 if f == nil {
39 return errors.New("resolver must be non nil")
40 }
41 options.resolver = f
42 return nil
43 }
44}
45
46func WithWelcome(welcome string) Option {
47 return func(options *options) error {
48 options.welcome = &welcome
49 return nil
50 }
51}
52
53func WithInitialID(id uint32) Option {
54 return func(options *options) error {
55 options.initialID = &id
56 return nil
57 }
58}
59
60func WithLogging(w io.Writer, verbose bool) Option {
61 return func(options *options) error {
62 if w == nil {
63 return errors.New("must provide a writer to log to")
64 }
65 options.writer = &w
66 options.verbose = verbose
67 return nil
68 }
69}
70
71func WithInitChannel(initChan chan InitChanMsg) Option {
72 return func(options *options) error {
73 if initChan == nil {
74 return errors.New("must provide a channel")
75 }
76 options.initChan = initChan
77 return nil
78 }
79}
80
81func WithMediainitChannel(mediainitChan chan MediaInitChanMsg) Option {
82 return func(options *options) error {
83 if mediainitChan == nil {
84 return errors.New("must provide a channel")
85 }
86 options.mediainitChan = mediainitChan
87 return nil
88 }
89}
90
91func WithPubChannel(pubChan chan PubEvent) Option {
92 return func(options *options) error {
93 if pubChan == nil {
94 return errors.New("must provide a channel")
95 }
96 options.pubChan = pubChan
97 return nil
98 }
99}
100
101func WithServerURIAndSecret(uri string, secret string) Option {
102 return func(options *options) error {
103 options.secret = secret
104 options.uri = uri
105 return nil
106 }
107}