-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
82 lines (72 loc) · 1.81 KB
/
app.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
package gogadgets
import (
"encoding/json"
"log"
"os"
)
// App holds all the gadgets and handles passing Messages
// to them, and receiving Messages from them. It is the
// central part of Gadgets system.
type App struct {
gadgets []Gadgeter
}
// New creates a new Gadgets system. The cfg argument can be a
// path to a json file or a Config object itself.
func New(cfg interface{}, gadgets ...Gadgeter) *App {
config := GetConfig(cfg)
return &App{
gadgets: config.CreateGadgets(gadgets...),
}
}
// Start is the main entry point for a Gadget system. It takes
// a chan in case the system is started as a goroutine,
// but it can just be called directly.
func (a *App) Start() {
x := make(chan Message)
a.GoStart(x)
}
// GoStart enables a gadgets system to be started
// by either a test suite that needs it to run
// as a goroutine or a client app that starts
// gogadget systems upon a command from a central
// web app.
func (a *App) GoStart(input <-chan Message) {
collect := make(chan Message)
channels := make(map[string]chan Message)
for _, gadget := range a.gadgets {
out := make(chan Message)
channels[gadget.GetUID()] = out
go gadget.Start(out, collect)
}
log.Println("started gagdgets")
b := NewBroker(channels, input, collect)
b.Start()
}
func GetConfig(config interface{}) *Config {
var c *Config
switch v := config.(type) {
case string:
c = getConfigFromFile(v)
case *Config:
c = v
default:
log.Fatal("invalid config")
}
if c.Port == 0 {
c.Port = 6111
}
return c
}
func getConfigFromFile(pth string) *Config {
c := &Config{}
f, err := os.Open(pth)
if err != nil {
log.Fatalf("unable to open config path %s: %s", pth, err)
}
defer f.Close()
err = json.NewDecoder(f).Decode(c)
if err != nil {
log.Fatalf("unable to parse json from config path %s: %s", pth, err)
}
return c
}