This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
170 lines (141 loc) · 3.99 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
package main
import (
"embed"
"errors"
"flag"
"fmt"
"net/url"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"github.com/ff14wed/aetherometer/internal/app"
"github.com/ff14wed/aetherometer/internal/reporter"
"github.com/apenwarr/fixconsole"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/windows"
"go.uber.org/zap"
)
//go:embed frontend/dist
var assets embed.FS
var Version = "development"
// Workaround for Windows support for zap from
// https://github.com/uber-go/zap/issues/621
func newWinFileSink(u *url.URL) (zap.Sink, error) {
// Remove leading slash left by url.Parse()
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
}
func NewLogger(outputLogPath string, useStdout bool) (*zap.Logger, error) {
if useStdout {
outputLogPath = "stdout"
} else if runtime.GOOS == "windows" {
err := zap.RegisterSink("winfile", newWinFileSink)
if err != nil {
return nil, fmt.Errorf("couldn't register winfile log sink: %s", err)
}
outputLogPath = "winfile:///" + filepath.ToSlash(outputLogPath)
}
zapCfg := zap.NewDevelopmentConfig()
zapCfg.DisableStacktrace = true
zapCfg.DisableCaller = true
zapCfg.OutputPaths = []string{outputLogPath}
zapCfg.ErrorOutputPaths = []string{outputLogPath}
return zapCfg.Build()
}
func startup() error {
flag.Usage = func() {
fixconsole.FixConsoleIfNeeded()
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
dirPath, err := app.GetAppDirectory()
if err != nil {
return err
}
cfgPath := flag.String("c", filepath.Join(dirPath, "config.toml"), "optional path to TOML config file")
headless := flag.Bool("headless", false, "run Aetherometer in headless mode.")
helpFlag := flag.Bool("h", false, "displays usage information")
versionFlag := flag.Bool("v", false, "displays version information")
flag.Parse()
if *helpFlag {
flag.Usage()
return nil
}
if *versionFlag {
fixconsole.FixConsoleIfNeeded()
fmt.Println("Aetherometer version:", Version)
return nil
}
outputLogPath := filepath.Join(dirPath, "aetherometer.log")
if *headless {
fixconsole.FixConsoleIfNeeded()
}
zapLogger, err := NewLogger(outputLogPath, *headless)
if err != nil {
return fmt.Errorf("can't initialize zap logger: %v", err)
}
defer func() {
_ = zapLogger.Sync()
}()
zap.ReplaceGlobals(zapLogger)
if len(*cfgPath) == 0 {
return errors.New("config path cannot be empty")
}
a := app.NewApp(*cfgPath, Version, zapLogger)
zapLogger.Info("====================================")
zapLogger.Info("Starting Aetherometer...")
err = a.Initialize()
if err != nil {
zapLogger.Error("Could not initialize Aetherometer", zap.Error(err))
return err
}
// Do not start the GUI in headless mode.
if *headless {
ctx := app.HeadlessContext()
a.Startup(ctx)
signals := make(chan os.Signal, 32)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
sig := <-signals
zapLogger.Info("Received signal, shutting down...", zap.Stringer("signal", sig))
a.Shutdown(ctx)
return nil
}
// Start run wails app if not headless mode
err = wails.Run(&options.App{
Title: "aetherometer",
Width: 1280,
Height: 800,
MinWidth: 1024,
MinHeight: 768,
DisableResize: false,
Fullscreen: false,
Frameless: true,
StartHidden: false,
HideWindowOnClose: false,
RGBA: &options.RGBA{R: 33, G: 37, B: 43, A: 255},
Assets: assets,
LogLevel: logger.DEBUG,
OnStartup: a.Startup,
OnDomReady: a.DomReady,
OnShutdown: a.Shutdown,
Bind: []interface{}{
app.GetBindings(a),
},
// Windows platform specific options
Windows: &windows.Options{
WebviewIsTransparent: false,
WindowIsTranslucent: false,
DisableWindowIcon: false,
},
})
return err
}
func main() {
err := startup()
if err != nil {
reporter.FatalError(err)
}
}