-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdashing.go
119 lines (103 loc) · 2.68 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package dashing
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
"github.com/GeertJohan/go.rice"
)
// 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
}
func NewEvent(id string, data map[string]interface{}, target string) *Event {
data["id"] = id
data["updatedAt"] = int32(time.Now().Unix())
return &Event{
ID: id,
Body: data,
Target: target,
}
}
// 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 {
d.initFolders()
if d.Router == nil {
d.Router = d.Server.NewRouter()
}
d.Broker.Start()
d.Worker.Start()
d.started = true
}
return d
}
func (d *Dashing) initFolders() {
// Si dashboards n'existe pas
if ok, _ := exists(d.Worker.webroot + "dashboards"); !ok {
os.MkdirAll(d.Worker.webroot+"dashboards", 0777)
d1, _ := rice.MustFindBox("assets/dashboards").String("sample.gerb")
ioutil.WriteFile(d.Worker.webroot+"dashboards"+string(filepath.Separator)+"sample.gerb", []byte(d1), 0644)
d2, _ := rice.MustFindBox("assets/dashboards").String("layout.gerb")
ioutil.WriteFile(d.Worker.webroot+"dashboards"+string(filepath.Separator)+"layout.gerb", []byte(d2), 0644)
}
// Si jobs n'existe pas
if ok, _ := exists(d.Worker.webroot + "jobs"); !ok {
os.MkdirAll(d.Worker.webroot+"jobs", 0777)
jobbox := rice.MustFindBox("assets/jobs")
jobbox.Walk("", func(path string, f os.FileInfo, err error) error {
content, _ := jobbox.String(path)
ioutil.WriteFile(d.Worker.webroot+"jobs"+string(filepath.Separator)+filepath.Base(path), []byte(content), 0644)
return nil
})
}
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
// NewDashing sets up the event broker, workers and webservice.
func NewDashing(root string, port string, token string) *Dashing {
broker := NewBroker()
worker := NewWorker(broker)
server := NewServer(broker)
server.webroot = root
worker.webroot = root
worker.url = "http://127.0.0.1:" + port
worker.token = token
if os.Getenv("DEV") != "" {
server.dev = true
}
server.dev = true
return &Dashing{
started: false,
Broker: broker,
Worker: worker,
Server: server,
}
}