-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
110 lines (94 loc) · 2.68 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
package main
import (
"context"
"embed"
"fmt"
"github.com/rainu/ask-mai/config"
"github.com/rainu/ask-mai/controller"
"github.com/rainu/ask-mai/health"
cmdchain "github.com/rainu/go-command-chain"
"github.com/wailsapp/wails/v2"
"log/slog"
"os"
"runtime"
"slices"
"strings"
)
const (
lastStateEnv = "_ASK_MAI_LAST_STATE"
)
//go:embed frontend/dist
var assets embed.FS
//go:embed build/appicon.png
var icon []byte
// this variable will be set correctly in built-time
var windowMode = "true"
func init() {
if runtime.GOOS == "windows" && windowMode == "true" {
// in windows there is no stdout and stderr in window-mode
// so we need to redirect the log output to a file
os.Stdout, _ = os.OpenFile("ask-mai.out.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
os.Stderr, _ = os.OpenFile("ask-mai.err.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
}
}
// this function will be set by debug.go:init() - if "debug" flag is available
var onStartUp func(c *config.Config)
func main() {
buildMode := slices.ContainsFunc(os.Environ(), func(s string) bool {
return strings.HasPrefix(s, "tsprefix=")
})
cfg := config.Parse(os.Args[1:], os.Environ())
if cfg.Debug.PrintVersion {
fmt.Fprintln(os.Stderr, versionLine())
os.Exit(0)
return
}
if !buildMode {
if err := cfg.Validate(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
return
}
slog.SetLogLoggerLevel(slog.Level(cfg.Debug.LogLevel))
if onStartUp != nil {
onStartUp(cfg)
}
}
defer cfg.Printer.Close()
ctrl, err := controller.BuildFromConfig(cfg, os.Getenv(lastStateEnv), buildMode)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(2)
return
}
// Create application with options
if cfg.Debug.WebKit.HttpServerAddress != "" {
// the underlying webview library will use this environment variable to start the inspector server
os.Setenv("WEBKIT_INSPECTOR_HTTP_SERVER", cfg.Debug.WebKit.HttpServerAddress)
}
if cfg.Debug.EnableCrashDetection {
oCtx, oCancel := context.WithCancel(context.Background())
health.ObserveProcess(oCtx, 98.0, func() {
if ctrl.IsAppMounted() {
slog.Warn("Restarting application because of high CPU usage: Seems like a freeze.")
ctrl.TriggerRestart()
oCancel() //prevent multiple restarts
}
})
}
err = wails.Run(controller.GetOptions(ctrl, icon, assets))
if !buildMode && ctrl.GetLastState() != "" {
ae := map[any]any{
lastStateEnv: ctrl.GetLastState(),
}
cmdchain.Builder().WithInput(os.Stdin).
Join(os.Args[0], os.Args[1:]...).WithAdditionalEnvironmentMap(ae).
Finalize().WithError(os.Stderr).WithOutput(os.Stdout).
Run()
}
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(3)
return
}
}