-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmain.go
149 lines (130 loc) · 3.03 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"flag"
"fmt"
"github.com/kardianos/service"
log "github.com/sirupsen/logrus"
"github.com/ssbeatty/oms/internal/config"
"github.com/ssbeatty/oms/internal/models"
"github.com/ssbeatty/oms/internal/server"
"github.com/ssbeatty/oms/pkg/logger"
"github.com/ssbeatty/oms/version"
"io/fs"
"os"
"path/filepath"
"runtime"
)
type App struct {
conf *config.Conf
sigs chan os.Signal
}
// NewApp create application
func NewApp(conf *config.Conf) *App {
return &App{
conf: conf,
}
}
// Start application
func (a *App) Start(s service.Service) error {
// run server
srv := server.NewServer(a.conf)
srv.Run()
return nil
}
// Stop application
func (a *App) Stop(s service.Service) error {
return nil
}
func main() {
// flags & init conf
configPath := flag.String("config", "", "path of config")
act := flag.String("action", "", "install or uninstall")
user := flag.String("user", "", "run with user")
flag.Parse()
var depends []string
if runtime.GOOS != "windows" {
depends = append(depends, "After=network.target")
}
var opt service.KeyValue
switch runtime.GOOS {
case "windows":
opt = service.KeyValue{
"StartType": "automatic",
"OnFailure": "restart",
"OnFailureDelayDuration": "5s",
"OnFailureResetPeriod": 10,
}
case "linux":
opt = service.KeyValue{
"LimitNOFILE": 65000,
}
case "darwin":
opt = service.KeyValue{
"SessionCreate": true,
}
}
appCfg := &service.Config{
Name: "omsd",
DisplayName: "omsd",
Description: "ssh & sftp manager service",
UserName: *user,
Dependencies: depends,
Option: opt,
}
if *configPath != "" {
abs, err := filepath.Abs(*configPath)
if err == nil {
appCfg.Arguments = []string{"--config", abs}
}
}
conf, err := config.NewServerConfig(*configPath)
if err != nil {
panic(err)
}
if conf.App.Logger == "" || conf.App.Logger == "stdout" {
logger.SetOutput(os.Stdout)
} else {
var logPath string
if filepath.IsAbs(conf.App.Logger) {
logPath = conf.App.Logger
} else {
logPath = filepath.Join(conf.App.DataPath, conf.App.Logger)
}
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_RDWR|os.O_APPEND, fs.ModePerm)
if err != nil {
log.Error("打开日志文件失败!")
return
}
logger.SetOutput(logFile)
}
log.Infof("当前版本: %s", version.Version)
// init db
db := conf.Db
if err := models.InitModels(db.Dsn, db.DbName, db.UserName, db.PassWord, db.Driver, conf.App.DataPath); err != nil {
panic(fmt.Sprintf("init db error: %v", err))
}
if conf.App.Mode == "dev" {
logger.SetLevelAndFormat(logger.DebugLevel, &log.TextFormatter{})
} else {
logger.SetLevelAndFormat(logger.InfoLevel, &log.TextFormatter{})
}
app := NewApp(conf)
sv, err := service.New(app, appCfg)
if err != nil {
panic(err)
}
switch *act {
case "install":
err = sv.Install()
log.Infof("服务注册成功")
case "uninstall":
err = sv.Uninstall()
log.Infof("服务取消成功")
default:
err = sv.Run()
log.Info("程序退出")
}
if err != nil {
panic(err)
}
}