forked from axolotl-chat/axolotl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (162 loc) · 5.43 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"flag"
_ "image/jpeg"
_ "image/png"
"os"
"runtime"
"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"
"github.com/signal-golang/textsecure/crayfish"
)
func init() {
flag.StringVar(&config.MainQml, "qml", "qml/phoneui/main.qml", "The qml file to load.")
flag.StringVar(&config.Gui, "e", "", "Specify runtime environment. 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, "Open electron development console")
flag.BoolVar(&config.PrintVersion, "version", false, "Print version info")
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 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)
}
endAxolotlGracefully()
return nil
}
func endAxolotlGracefully() {
log.Infoln("[axolotl] ending axolotl")
err := crayfish.Instance.Stop()
if err != nil {
log.Errorf("[axolotl] error stopping crayfish: %s", err)
}
os.Exit(0)
}
func runElectron() {
defer endAxolotlGracefully()
l := log.New()
electronPath := os.Getenv("SNAP_USER_DATA")
if len(electronPath) == 0 {
electronPath = config.ConfigDir + "/electron"
}
electronSwitches := []string{"--disable-dev-shm-usage", "--no-sandbox"}
if os.Getenv("XDG_SESSION_TYPE") == "wayland" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
electronSwitches = append(electronSwitches, "--enable-features=UseOzonePlatform", "--ozone-platform=wayland")
}
log.Infoln("[axolotl-electron] creating astilelectron with the following switches:", electronSwitches)
var a, err = 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: "16.0.0",
VersionAstilectron: "0.50.0",
SingleInstance: true,
ElectronSwitches: electronSwitches})
if err != nil {
log.Errorln(errors.Wrap(err, "[axolotl-electron]: creating astilectron failed"))
}
defer a.Close()
// Start astilectron
a.HandleSignals()
if err = a.Start(); err != nil {
log.Errorln(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
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.EventNameAppClose, func(e astilectron.Event) (deleteListener bool) {
log.Errorln("[axolotl-electron] Electron App was closed")
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 endAxolotlGracefully()
log.Printf("[axolotl] Axolotl server started")
// Fetch the URL.
webserver.Run()
}
var wg sync.WaitGroup
func main() {
setup()
wg.Add(1)
go 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()
log.Println("[axolotl] Axolotl stopped")
}