-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
158 lines (142 loc) · 3.83 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
b64 "encoding/base64"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
mustardcore "mustard/core"
events "mustard/core/events"
_ "mustard/jobs"
"net/http"
"os"
_ "github.com/joho/godotenv/autoload"
"gopkg.in/yaml.v3"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
type requestPayload struct {
Path string `json:"path"`
}
type widget struct {
Component string `json:"component"`
Class []string `json:"class"`
Props interface{} `json:"props"`
State interface{} `json:"state"`
}
type dashboard struct {
Id string `json:"id"`
Widgets []widget `json:"widgets"`
}
type configuration struct {
Dashboards []dashboard `json:"dashboards"`
Jobs []mustardcore.Schedule `json:"jobs"`
}
func parseArgs() (bool, string) {
gistIDPtr := flag.String("gist", "", "gist id to install")
flag.Parse()
fmt.Println("gist", *gistIDPtr)
return len(*gistIDPtr) > 0, *gistIDPtr
}
func installGist(gistID string) {
gist := mustardcore.Gist{ID: gistID}
gist.Install()
}
func readConfig() configuration {
// if config.yaml is present we use that first
var fileContent *os.File
var byteContent []byte
var result configuration
isYaml := true
_, err := os.Stat("./config/config.yml")
if os.IsNotExist(err) {
fileContent, err = os.Open("./config/config.json")
isYaml = false
} else {
fileContent, err = os.Open("./config/config.yml")
}
if err != nil && !os.IsNotExist(err) {
log.Println(err)
byteContent = []byte("[]")
json.Unmarshal(byteContent, &result)
return result
}
byteContent, err = ioutil.ReadAll(fileContent)
if err != nil {
log.Println(err)
byteContent = []byte("[]")
json.Unmarshal(byteContent, &result)
return result
}
if isYaml {
err = yaml.Unmarshal(byteContent, &result)
if err != nil {
log.Println(err)
}
return result
}
json.Unmarshal(byteContent, &result)
return result
}
func main() {
hasArgs, gistID := parseArgs()
if hasArgs {
installGist(gistID)
return
}
e := echo.New()
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowCredentials: true,
AllowOrigins: []string{"*"},
ExposeHeaders: []string{"*"},
}))
e.Static("/js", "js")
e.Static("/css", "css")
e.File("/dashboard/*", "views/index.html")
eventsManager := mustardcore.GetEventsManager()
eventsManager.Init(e)
eventListener := events.EventListener{URL: os.Getenv("KAFKA_URL"), Topic: os.Getenv("KAFKA_TOPIC"), GroupID: "mustard"}
eventListener.Start()
// read config to get widgets
result := readConfig()
mustardcore.GetFactory().Process(result.Jobs)
defer eventsManager.Destroy()
defer mustardcore.DestroyJobs()
e.POST("/api/nudge", func(c echo.Context) error {
mustardcore.FireImmediately()
return c.String(http.StatusOK, "Hello mustard")
})
e.POST("/api/layout", func(c echo.Context) (err error) {
request := new(requestPayload)
if err = c.Bind(request); err != nil {
return
}
for _, f := range result.Dashboards {
if f.Id == request.Path {
return c.JSON(http.StatusOK, result.Dashboards[0].Widgets)
}
}
return
})
e.POST("/api/webhook", func(c echo.Context) (err error) {
apiKey := os.Getenv("API_KEY")
headerKey := c.Request().Header.Get("X_API_KEY")
if apiKey == "" || headerKey != b64.StdEncoding.EncodeToString([]byte(apiKey)) {
return c.String(http.StatusUnauthorized, "Invalid key")
}
eventMessage := new(mustardcore.EventData)
if err = c.Bind(eventMessage); err != nil || eventMessage.Data == nil {
return c.String(http.StatusUnprocessableEntity, "Invalid request body")
}
mustardcore.GetEventsManager().Notify(*eventMessage)
return c.String(http.StatusAccepted, "Ok")
})
port := os.Getenv("PORT")
if port == "" {
port = "8090"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), e))
}