-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdashing.go
66 lines (58 loc) · 1.23 KB
/
dashing.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
package dashing
import (
"net/http"
"os"
"path/filepath"
)
// An Event contains the widget ID, a body of data,
// and an optional target (only "dashboard" for now).
type Event struct {
ID string
Body map[string]interface{}
Target string
}
// Dashing struct definition.
type Dashing struct {
started bool
Broker *Broker
Worker *Worker
Server *Server
Router http.Handler
}
// ServeHTTP implements the HTTP Handler.
func (d *Dashing) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !d.started {
panic("dashing.Start() has not been called")
}
d.Router.ServeHTTP(w, r)
}
// Start actives the broker and workers.
func (d *Dashing) Start() *Dashing {
if !d.started {
if d.Router == nil {
d.Router = d.Server.NewRouter()
}
d.Broker.Start()
d.Worker.Start()
d.started = true
}
return d
}
// NewDashing sets up the event broker, workers and webservice.
func NewDashing() *Dashing {
broker := NewBroker()
worker := NewWorker(broker)
server := NewServer(broker)
if os.Getenv("WEBROOT") != "" {
server.webroot = filepath.Clean(os.Getenv("WEBROOT")) + "/"
}
if os.Getenv("DEV") != "" {
server.dev = true
}
return &Dashing{
started: false,
Broker: broker,
Worker: worker,
Server: server,
}
}