forked from axolotl-chat/axolotl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (148 loc) · 4.58 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
159
160
package main
import (
"bufio"
"flag"
_ "image/jpeg"
_ "image/png"
"io"
"os"
"sync"
astilectron "github.com/asticode/go-astilectron"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/nanu-c/axolotl/app/config"
"github.com/nanu-c/axolotl/app/helpers"
"github.com/nanu-c/axolotl/app/push"
"github.com/nanu-c/axolotl/app/ui"
"github.com/nanu-c/axolotl/app/webserver"
"github.com/nanu-c/axolotl/app/worker"
)
var e string
func init() {
flag.StringVar(&config.MainQml, "qml", "qml/phoneui/main.qml", "The qml file to load.")
flag.StringVar(&config.Gui, "e", "", "use either electron, ut, lorca, qt or server")
flag.StringVar(&config.AxolotlWebDir, "axolotlWebDir", "./axolotl-web/dist", "Specify the directory to use for axolotl-web")
flag.BoolVar(&config.ElectronDebug, "eDebug", false, "use to show development console in electron")
flag.StringVar(&config.ServerHost, "host", "127.0.0.1", "Host to serve UI from.")
flag.StringVar(&config.ServerPort, "port", "9080", "Port to serve UI from.")
}
func print(stdout io.ReadCloser) {
scanner := bufio.NewScanner(stdout)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
m := scanner.Text()
log.Println("[axolotl] ", m)
}
}
func setup() {
helpers.SetupLogging()
config.SetupConfig()
log.SetLevel(log.DebugLevel)
log.Infoln("[axolotl] Starting Signal for Ubuntu version", config.AppVersion)
}
func runBackend() {
go worker.RunBackend()
if config.IsPushHelper {
push.PushHelperProcess()
}
}
func runUI() error {
defer wg.Done()
if config.Gui != "ut" && config.Gui != "lorca" && config.Gui != "qt" {
ui.RunUi(config.Gui)
runElectron()
} else {
ui.RunUi(config.Gui)
}
os.Exit(0)
return nil
}
func runElectron() {
log.Infoln("[axolotl] Start electron")
l := log.New()
electronPath := os.Getenv("SNAP_USER_DATA")
if len(electronPath) == 0 {
electronPath = config.ConfigDir + "/electron"
}
var a, _ = astilectron.New(l, astilectron.Options{
AppName: "axolotl",
AppIconDefaultPath: "axolotl-web/public/axolotl.png", // If path is relative, it must be relative to the data directory
AppIconDarwinPath: "axolotl-web/public/axolotl.png", // Same here
BaseDirectoryPath: electronPath,
VersionElectron: "11.1.1",
SingleInstance: true,
ElectronSwitches: []string{"--disable-dev-shm-usage", "--no-sandbox"}})
defer a.Close()
// Start astilectron
a.HandleSignals()
if err := a.Start(); err != nil {
log.Debugln(errors.Wrap(err, "[axolotl-electron] main: starting astilectron failed"))
}
a.On(astilectron.EventNameAppCrash, func(e astilectron.Event) (deleteListener bool) {
log.Errorln("[axolotl-electron] Electron App has crashed", e)
return
})
a.HandleSignals()
// New window
var w *astilectron.Window
var err error
center := true
height := 800
width := 600
if w, err = a.NewWindow("http://"+config.ServerHost+":"+config.ServerPort, &astilectron.WindowOptions{
Center: ¢er,
Height: &height,
Width: &width,
}); err != nil {
log.Debugln("[axolotl-electron]", errors.Wrap(err, "main: new window failed"))
}
w.On(astilectron.EventNameAppCrash, func(e astilectron.Event) (deleteListener bool) {
log.Errorln("[axolotl-electron] Electron App has crashed")
return
})
w.On(astilectron.EventNameWindowEventDidFinishLoad, func(e astilectron.Event) (deleteListener bool) {
log.Infoln("[axolotl-electron] Electron App load")
w.ExecuteJavaScript("window.onToken = function(token){window.location='http://"+config.ServerHost+":"+config.ServerPort+"/?token='+token;};")
return
})
w.On(astilectron.EventNameWindowEventWillNavigate, func(e astilectron.Event) (deleteListener bool) {
log.Infoln("[axolotl-electron] Electron navigation")
return
})
w.On(astilectron.EventNameWindowEventWebContentsExecutedJavaScript, func(e astilectron.Event) (deleteListener bool) {
log.Infoln("[axolotl-electron] Electron navigation js")
return
})
// Create windows
if err = w.Create(); err != nil {
log.Debugln("[axolotl-electron]", errors.Wrap(err, "main: creating window failed"))
}
log.Debugln("[axolotl-electron] open dev tools", config.ElectronDebug)
if config.ElectronDebug {
w.OpenDevTools()
}
w.Session.ClearCache()
// Blocking pattern
a.Wait()
}
func runWebserver() {
defer wg.Done()
log.Printf("[axolotl] Axolotl server started")
// Fetch the URL.
webserver.Run()
}
var wg sync.WaitGroup
func main() {
setup()
runBackend()
log.Println("[axolotl] Setup completed")
wg.Add(1)
go runWebserver()
if config.Gui != "server" {
wg.Add(1)
go runUI()
} else {
log.Printf("[axolotl] Axolotl frontend is at http://" + config.ServerHost + ":" + config.ServerPort + "/")
}
wg.Wait()
}