-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.go
56 lines (45 loc) · 1.23 KB
/
default.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
package got
import (
"context"
"sync"
)
type handlerRegistration struct {
gen HandlerGenerator
opts []MuxOption
}
type funcRegistration struct {
gen HandlerFuncGenerator
opts []MuxOption
}
var (
once sync.Once
hRegistrations []*handlerRegistration
fRegistrations []*funcRegistration
)
func RegisterHandler(hg HandlerGenerator, opts ...MuxOption) {
once.Do(func() { hRegistrations = []*handlerRegistration{} })
hRegistrations = append(hRegistrations, &handlerRegistration{hg, opts})
}
func RegisterHandlerFunc(hg HandlerFuncGenerator, opts ...MuxOption) {
once.Do(func() { fRegistrations = []*funcRegistration{} })
fRegistrations = append(fRegistrations, &funcRegistration{hg, opts})
}
func Start(ctx context.Context, token string, opts ...Option) error {
once.Do(func() { hRegistrations = []*handlerRegistration{} })
once.Do(func() { fRegistrations = []*funcRegistration{} })
got, err := New(ctx, token, opts...)
if err != nil {
return err
}
for _, hr := range hRegistrations {
if err := got.RegisterHandler(hr.gen, hr.opts...); err != nil {
return err
}
}
for _, fr := range fRegistrations {
if err := got.RegisterHandlerFunc(fr.gen, fr.opts...); err != nil {
return err
}
}
return got.Start(ctx)
}