-
Notifications
You must be signed in to change notification settings - Fork 31
/
loader.go
86 lines (71 loc) · 1.67 KB
/
loader.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package script
import (
"bytes"
"context"
"io"
"net/url"
"strings"
"time"
"github.com/grab/async"
"github.com/kelindar/loader"
)
const (
luaType = "lua"
pluginType = "plugin"
)
type Loader struct {
loader *loader.Loader // The loader to use to load and watch for code updates
}
type Handler interface {
Load(uriOrCode string) (Handler, error)
String() string
Value(map[string]interface{}) (interface{}, error)
}
type HandlerLoader struct {
hs []Handler
}
func NewHandlerLoader(handlers ...Handler) *HandlerLoader {
s := HandlerLoader{}
for _, h := range handlers {
s.hs = append(s.hs, h)
}
return &s
}
func parseHandler(uriOrCode string) string {
if strings.HasSuffix(uriOrCode, ".so") {
return pluginType
}
return luaType
}
func (l *HandlerLoader) LoadHandler(uriOrCode string) (Handler, error) {
h := parseHandler(uriOrCode)
for _, d := range l.hs {
if d.String() == h {
return d.Load(uriOrCode)
}
}
return nil, nil
}
// Wtch starts watching for script updates
func (l *Loader) watch(uriOrCode string, onUpdate func(io.Reader) error) error {
if _, err := url.Parse(uriOrCode); err != nil {
return onUpdate(strings.NewReader(uriOrCode)) // Assume it's the actual lua code
}
// Start watching on the URL
updates := l.loader.Watch(context.Background(), uriOrCode, 5*time.Minute)
u := <-updates
if u.Err != nil {
return u.Err
}
// Read the updates asynchronously
async.Invoke(context.Background(), func(ctx context.Context) (interface{}, error) {
for u := range updates {
if u.Err == nil {
_ = onUpdate(bytes.NewReader(u.Data))
}
}
return nil, nil
})
// Perform a first update
return onUpdate(bytes.NewReader(u.Data))
}