-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
151 lines (129 loc) · 3.62 KB
/
Copy pathmain.go
File metadata and controls
151 lines (129 loc) · 3.62 KB
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
// Package api_show /main.go
package main
import (
"embed"
"fmt"
"os"
"runtime"
"runtime/debug"
"time"
"github.com/0xYeah/api_show/api"
"github.com/0xYeah/api_show/as_config"
"github.com/0xYeah/api_show/auth"
"github.com/0xYeah/api_show/common"
"github.com/0xYeah/api_show/custom_cmd"
"github.com/george012/gtbox"
"github.com/george012/gtbox/gtbox_cmd"
"github.com/george012/gtbox/gtbox_log"
)
//go:embed webroot
var webrootFS embed.FS
var (
mRunMode = ""
mGitCommitHash = ""
mGitCommitTime = ""
mPackageOS = ""
mPackageTime = ""
mGoVersion = ""
)
func setupApp() {
runMode := gtbox.RunModeDebug
switch mRunMode {
case "debug":
runMode = gtbox.RunModeDebug
case "test":
runMode = gtbox.RunModeTest
case "release":
runMode = gtbox.RunModeRelease
default:
runMode = gtbox.RunModeDebug
}
as_config.CurrentApp = as_config.NewApp(
as_config.ProjectName,
as_config.ProjectBundleID,
fmt.Sprintf("%s service",
as_config.ProjectName,
),
runMode,
)
if as_config.CurrentApp.CurrentRunMode == gtbox.RunModeDebug {
cmdMap := map[string]string{
"git_commit_hash": "git show -s --format=%H",
"git_commit_time": "git show -s --format=\"%ci\" | cut -d ' ' -f 1,2 | sed 's/ /_/'",
"build_os": "go env GOOS",
"go_version": "go version | awk '{print $3}'",
}
cmdRes := gtbox_cmd.RunWith(cmdMap)
if cmdRes != nil {
mGitCommitHash = cmdRes["git_commit_hash"]
mGitCommitTime = cmdRes["git_commit_time"]
mPackageOS = cmdRes["build_os"]
mGoVersion = cmdRes["go_version"]
mPackageTime = time.Now().UTC().Format("2006-01-02_15:04:05")
}
}
as_config.CurrentApp.GitCommitHash = mGitCommitHash
as_config.CurrentApp.GitCommitTime = mGitCommitTime
as_config.CurrentApp.GoVersion = mGoVersion
as_config.CurrentApp.PackageOS = mPackageOS
as_config.CurrentApp.PackageTime = mPackageTime
custom_cmd.HandleCustomCmds(os.Args, as_config.CurrentApp)
gtbox.SetupGTBox(as_config.CurrentApp.AppName,
as_config.CurrentApp.CurrentRunMode,
as_config.CurrentApp.AppLogPath,
30,
gtbox_log.GTLogSaveHours,
as_config.CurrentApp.HTTPRequestTimeOut,
)
}
func main() {
runtime.LockOSThread()
setupApp()
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
gtbox_log.LogErrorf("系统崩溃:%v", r)
gtbox_log.LogErrorf("系统崩溃:\n%s", debug.Stack())
}
}()
if as_config.CurrentApp.CurrentRunMode == gtbox.RunModeDebug {
as_config.CurrentApp.AppConfigFilePath = "./example_files/config_local.toml"
} else {
as_config.CurrentApp.AppConfigFilePath = "./conf/config.toml"
}
if err := as_config.SyncConfigFile(as_config.CurrentApp.AppConfigFilePath); err != nil {
gtbox_log.LogErrorf("[sync config error] %s: %v", as_config.CurrentApp.AppConfigFilePath, err)
os.Exit(1)
}
cfg := as_config.GlobalConfig
if err := auth.LoadUsersFromConfig(cfg.Users); err != nil {
gtbox_log.LogErrorf("[api_show] load users: %v", err)
os.Exit(1)
}
if cfg.Secret == "" || len(cfg.Secret) < 16 {
gtbox_log.LogErrorf("[api_show] secret missing or <16 bytes — refusing to start")
os.Exit(1)
}
secret := []byte(cfg.Secret)
dataDir := cfg.DataDir
if dataDir == "" {
dataDir = as_config.DefaultDataDir
}
listenPort := cfg.Port
if listenPort == 0 {
listenPort = as_config.ListenPort
}
api.StartAPIServices(&api.Options{
Deps: auth.HandlerDeps{
Secret: secret,
TOTP: auth.NewTOTPStore(dataDir, secret),
Pwd: auth.NewPwdStore(dataDir, secret),
},
DataDir: dataDir,
ListenPort: listenPort,
UploadToken: cfg.UploadToken,
OnlyLocal: cfg.OnlyLocal,
WebrootFS: webrootFS,
})
common.LoadSigHandle(nil, nil)
}