-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
128 lines (110 loc) · 3.17 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
package main
import (
"fmt"
golog "log"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"time"
"gioui.org/app"
"github.com/crypto-power/cryptopower/libwallet"
"github.com/crypto-power/cryptopower/libwallet/utils"
"github.com/crypto-power/cryptopower/logger"
"github.com/crypto-power/cryptopower/ui"
_ "github.com/crypto-power/cryptopower/ui/assets"
"github.com/crypto-power/cryptopower/ui/load"
)
const (
devBuild = "dev"
prodBuild = "prod"
)
var (
// Version is the application version. It is set using the -ldflags
Version = "2.0.2"
// BuildDate is the date the application was built. It is set using the -ldflags
BuildDate string
// BuildEnv is the build environment. It is set using the -ldflags
BuildEnv = devBuild
)
func main() {
cfg, err := loadConfig()
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return
}
if cfg.Profile > 0 {
go func() {
golog.Printf("Starting profiling server on port %d\n", cfg.Profile)
golog.Println(http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", cfg.Profile), nil))
}()
}
var buildDate time.Time
if BuildEnv == prodBuild {
buildDate, err = time.Parse(time.RFC3339, BuildDate)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return
}
} else {
buildDate = time.Now()
}
initializeAssetsManager := func(netType utils.NetworkType) (*libwallet.AssetsManager, error) {
// Initialize loggers and set log level before the asset manager is
// initialized.
logDir := filepath.Join(cfg.LogDir, string(netType))
initLogRotator(logDir, cfg.MaxLogZips)
if cfg.DebugLevel == "" {
_ = logger.SetLogLevels(utils.DefaultLogLevel)
} else {
_ = logger.SetLogLevels(cfg.DebugLevel)
}
assetsManager, err := libwallet.NewAssetsManager(cfg.HomeDir, logDir, netType, cfg.DEXTestAddr)
if err != nil {
return nil, err
}
// if debuglevel is passed at commandLine persist the option.
if cfg.DebugLevel != "" {
assetsManager.SetLogLevels(cfg.DebugLevel)
} else {
_ = logger.SetLogLevels(assetsManager.GetLogLevels())
}
return assetsManager, nil
}
// Load the app-wide config which stores information such as the netType
// last used by the user.
appCfgFilePath := filepath.Join(cfg.HomeDir, "config.json")
appCfg, err := load.AppConfigFromFile(appCfgFilePath)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return
}
// Init the AssetsManager using the user-selected netType or mainnet if the
// user has not selected a netType from the app's settings page.
appInfo, err := load.StartApp(Version, buildDate, cfg.Network, appCfg, initializeAssetsManager)
if err != nil {
log.Errorf("init assetsManager error: %v", err)
return
}
win, err := ui.CreateWindow(appInfo)
if err != nil {
log.Errorf("Could not initialize window: %s\ns", err)
return
}
go func() {
// Wait until we receive the shutdown request.
<-win.Quit
// Terminate all the backend processes safely.
appInfo.AssetsManager.Shutdown()
// Backend process terminated safely trigger app shutdown now.
win.IsShutdown <- struct{}{}
}()
go func() {
// blocks until the backend processes terminate.
win.HandleEvents()
// Exit the app.
os.Exit(0)
}()
// Start the GUI frontend.
app.Main()
}