forked from Equanox/gotron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (116 loc) · 2.79 KB
/
Copy pathmain.go
File metadata and controls
137 lines (116 loc) · 2.79 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os/exec"
"github.com/fatih/color"
"github.com/gorilla/websocket"
"github.com/spf13/viper"
)
//SocketEvent event
type SocketEvent struct {
Event string
Data interface{}
}
//Backend Configuration returned by loadConfig
type configuration struct {
name string //Application Name
server string
port string
}
//Globals
var done = make(chan bool, 1) //Wait for Shutdown signal over websocket
var upgrader = websocket.Upgrader{ //Upgrader for websockets
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Subprotocols: []string{"p0", "p1"},
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// Loads configuration from file
// or inits values with default values
func loadConfig() configuration {
viper.SetConfigName("config")
// Paths to search for a config file
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
color.Set(color.FgRed)
fmt.Println("No configuration file loaded - using defaults")
color.Unset()
}
// default values
viper.SetDefault("name", "")
viper.SetDefault("server", "localhost")
viper.SetDefault("port", "9109")
// Write all params to stdout
color.Set(color.FgGreen)
fmt.Println("Loaded Configuration:")
color.Unset()
// Print config
keys := viper.AllKeys()
for i := range keys {
key := keys[i]
fmt.Println(key + ":" + viper.GetString(key))
}
fmt.Println("---")
return configuration{
name: viper.GetString("name"),
server: viper.GetString("server"),
port: viper.GetString("port")}
}
//Handles msgs to communicate with nodejs electron for rampup & shutdown
func socket(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
var event SocketEvent
_, message, err := c.ReadMessage()
if err != nil {
log.Println("ElectronSocket: [err]", err)
break
}
//Handle Message
err = json.Unmarshal(message, &event)
if err != nil {
log.Println("Unmashal: ", err)
break
}
log.Printf("ElectronSocket: [received] %+v", event)
//Shutdown Event
if event.Event == "shutdown" {
switch t := event.Data.(type) {
case bool:
if t {
done <- true
}
}
}
}
}
func main() {
config := loadConfig()
var addr = config.server + ":" + config.port
http.HandleFunc("/ui", socket) //Endpoint for Electron startup/teardown
go http.ListenAndServe(addr, nil) //Start websockets in goroutine
log.Printf("Starting Electron...")
cmd := exec.Command("electron", "app")
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
color.Set(color.FgGreen)
log.Printf("%s succesfully started", config.name)
color.Unset()
<-done //Wait for shutdown signal
color.Set(color.FgGreen)
log.Printf("Shutting down...")
color.Unset()
}