-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.go
More file actions
146 lines (123 loc) · 3.77 KB
/
main.go
File metadata and controls
146 lines (123 loc) · 3.77 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
package main
import (
"context"
"embed"
"errors"
"fmt"
"os"
"time"
"github.com/jessevdk/go-flags"
"go.uber.org/zap"
"code-kanban/api"
"code-kanban/model"
"code-kanban/utils"
)
//go:embed all:static
var embedStatic embed.FS
var runningAsService bool
//go:generate go run ./model/sqlc_gen/
func main() {
var opts struct {
Version bool `short:"v" long:"version" description:"Show version information"`
Install bool `short:"i" long:"install" description:"Install as system service"`
Uninstall bool `long:"uninstall" description:"Uninstall system service"`
ForceMigrate bool `short:"m" long:"migrate" description:"Force database migration"`
UseHomeData bool `short:"H" long:"home-data" description:"Use home directory for data storage (~/.codekanban)"`
Bind string `short:"b" long:"bind" description:"Bind(host) address (default: 127.0.0.1)"`
Port int `short:"p" long:"port" description:"Server port (default: 3007)"`
}
parser := flags.NewParser(&opts, flags.Default)
parser.Name = PACKAGE_NAME
parser.Usage = "[OPTIONS]"
if _, err := parser.ParseArgs(os.Args); err != nil {
return
}
if opts.Version {
fmt.Printf("%s v%s\n", APPNAME, VERSION.String())
fmt.Printf("Channel: %s\n", APP_CHANNEL)
return
}
if opts.Install {
serviceInstall(true)
return
}
if opts.Uninstall {
serviceInstall(false)
return
}
if opts.UseHomeData {
utils.SetUseHomeData(true)
}
run(opts.ForceMigrate, opts.Bind, opts.Port)
}
func run(forceMigrate bool, bind string, port int) {
dataDir := utils.GetDataDir()
appLock, err := utils.AcquireAppInstanceLock(dataDir)
if err != nil {
var lockedErr *utils.AppInstanceLockedError
if errors.As(err, &lockedErr) {
fmt.Fprintf(os.Stderr, "CodeKanban is already running with data directory %s\n", lockedErr.DataDir)
fmt.Fprintln(os.Stderr, "Close the existing instance or use a different data directory.")
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "Failed to acquire application lock: %v\n", err)
os.Exit(1)
}
defer func() {
if closeErr := appLock.Close(); closeErr != nil {
fmt.Fprintf(os.Stderr, "Failed to release application lock: %v\n", closeErr)
}
}()
// 异步检查版本更新(不阻塞启动)
checker := utils.NewVersionChecker(VERSION.String(), PACKAGE_NAME)
checker.CheckAsync()
cfg := utils.ReadConfig()
if err := utils.EnsureAuthConfig(cfg); err != nil {
fmt.Printf("Failed to initialize auth config: %v\n", err)
os.Exit(1)
}
if forceMigrate {
cfg.AutoMigrate = true
}
// Override config with command line flags if provided
if bind != "" || port != 0 {
if bind == "" {
bind = "127.0.0.1"
}
if port == 0 {
port = 3007
}
cfg.ServeAt = fmt.Sprintf("%s:%d", bind, port)
cfg.Domain = cfg.ServeAt
}
logger, cleanup, err := utils.InitLogger(cfg)
if err != nil {
fmt.Printf("Failed to initialize logger: %v\n", err)
os.Exit(1)
}
defer cleanup()
if err := model.InitWithDSN(cfg.DSN, cfg.DBLogLevel, cfg.AutoMigrate); err != nil {
logger.Fatal("Failed to initialize database", zap.Error(err))
}
defer model.DBClose()
logger.Info("Starting server", zap.String("listen", cfg.ServeAt))
if !runningAsService && !cfg.DisableAutoOpenBrowser {
if url := utils.BuildLaunchURL(cfg); url != "" {
go func(target string) {
time.Sleep(800 * time.Millisecond)
if err := utils.OpenBrowser(target); err != nil {
logger.Warn("Failed to open browser automatically", zap.String("url", target), zap.Error(err))
}
}(url)
}
}
ctx := utils.ContextWithLogger(context.Background(), logger)
if err := api.Init(ctx, cfg, embedStatic, &api.AppInfo{
Name: APPNAME,
Version: VERSION.String(),
Channel: APP_CHANNEL,
PackageName: PACKAGE_NAME,
}); err != nil {
logger.Fatal("Failed to start server", zap.Error(err))
}
}