-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (105 loc) · 2.46 KB
/
main.go
File metadata and controls
125 lines (105 loc) · 2.46 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime/debug"
"smart-git/config"
"smart-git/database"
"smart-git/gitc"
"strings"
"github.com/WJQSERVER-STUDIO/logger"
)
var (
cfgfile string = "./config/config"
cfg *config.Config
)
var (
logw = logger.Logw
logDump = logger.LogDump
logDebug = logger.LogDebug
logInfo = logger.LogInfo
logWarning = logger.LogWarning
logError = logger.LogError
)
func ReadFlag() {
cfgfilePtr := flag.String("c", "./config/config", "config file path")
cfgfileCompatPtr := flag.String("cfg", "", "config file path (compat alias)")
flag.Parse()
if *cfgfileCompatPtr != "" {
cfgfile = *cfgfileCompatPtr
return
}
cfgfile = *cfgfilePtr
}
func loadConfig() {
var err error
// 初始化配置
cfg, err = config.LoadConfig(cfgfile)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
fmt.Printf("Loaded config: %v\n", cfg)
}
func setMemLimit(cfg *config.Config) {
if cfg.Server.MemLimit > 0 {
debug.SetMemoryLimit((cfg.Server.MemLimit) * 1024 * 1024)
logInfo("Set Memory Limit to %d MB", cfg.Server.MemLimit)
}
}
func bootstrap() error {
for _, arg := range os.Args[1:] {
if strings.HasPrefix(arg, "-test.") || arg == "-test.v" {
return nil
}
}
ReadFlag()
loadConfig()
setMemLimit(cfg)
// 创建根目录 os
err := os.MkdirAll(cfg.Server.BaseDir, 0755)
if err != nil {
return fmt.Errorf("fail to create dir: %w", err)
}
if cfg.Log.LogFilePath != "" {
err = os.MkdirAll(filepath.Dir(cfg.Log.LogFilePath), 0755)
if err != nil {
return fmt.Errorf("fail to create log dir: %w", err)
}
}
if cfg.Database.Path != "" {
err = os.MkdirAll(filepath.Dir(cfg.Database.Path), 0755)
if err != nil {
return fmt.Errorf("fail to create db dir: %w", err)
}
}
err = logger.Init(cfg.Log.LogFilePath, cfg.Log.MaxLogSize)
if err != nil {
return fmt.Errorf("fail to init logger: %w", err)
}
if cfg.Log.Level != "" {
logger.SetLogLevel(cfg.Log.Level)
}
database.SetDBInfo(cfg)
if err := gitc.RecoverPendingRepos(cfg); err != nil {
return fmt.Errorf("fail to recover pending repos: %w", err)
}
return nil
}
func main() {
if err := bootstrap(); err != nil {
log.Fatalf("startup failed: %v", err)
}
if database.DB != nil {
defer database.DB.Close()
}
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
// 运行HTTP Git Server
err := RunHTTP(addr, cfg.Server.BaseDir)
if err != nil {
fmt.Printf("Fail to run http: %v\n", err)
return
}
}