Skip to content

Commit fd7f294

Browse files
committed
[FIX](main) 增加服务注册和文件日志
1 parent 47d15cb commit fd7f294

41 files changed

Lines changed: 5843 additions & 10 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/omsd/main.go

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,114 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"github.com/kardianos/service"
67
log "github.com/sirupsen/logrus"
8+
"io/fs"
79
"oms/internal/config"
810
"oms/internal/models"
911
"oms/internal/server"
1012
"oms/pkg/logger"
1113
"oms/version"
1214
"os"
13-
"os/signal"
14-
"syscall"
15+
"path/filepath"
16+
"runtime"
1517
)
1618

19+
type App struct {
20+
conf *config.Conf
21+
sigs chan os.Signal
22+
}
23+
24+
// NewApp create application
25+
func NewApp(conf *config.Conf) *App {
26+
return &App{
27+
conf: conf,
28+
}
29+
}
30+
31+
// Start application
32+
func (a *App) Start(s service.Service) error {
33+
// run server
34+
srv := server.NewServer(a.conf)
35+
srv.Run()
36+
return nil
37+
}
38+
39+
// Stop application
40+
func (a *App) Stop(s service.Service) error {
41+
return nil
42+
}
43+
1744
func main() {
1845
// flags & init conf
1946

2047
configPath := flag.String("config", "", "path of config")
48+
act := flag.String("action", "", "install or uninstall")
49+
user := flag.String("user", "", "run with user")
2150
flag.Parse()
2251

52+
var depends []string
53+
if runtime.GOOS != "windows" {
54+
depends = append(depends, "After=network.target")
55+
}
56+
var opt service.KeyValue
57+
switch runtime.GOOS {
58+
case "windows":
59+
opt = service.KeyValue{
60+
"StartType": "automatic",
61+
"OnFailure": "restart",
62+
"OnFailureDelayDuration": "5s",
63+
"OnFailureResetPeriod": 10,
64+
}
65+
case "linux":
66+
opt = service.KeyValue{
67+
"LimitNOFILE": 65000,
68+
}
69+
case "darwin":
70+
opt = service.KeyValue{
71+
"SessionCreate": true,
72+
}
73+
}
74+
75+
appCfg := &service.Config{
76+
Name: "omsd",
77+
DisplayName: "omsd",
78+
Description: "nat forward service",
79+
UserName: *user,
80+
Dependencies: depends,
81+
Option: opt,
82+
}
83+
84+
if *configPath != "" {
85+
abs, err := filepath.Abs(*configPath)
86+
if err == nil {
87+
appCfg.Arguments = []string{"--config", abs}
88+
}
89+
}
90+
2391
conf, err := config.NewServerConfig(*configPath)
2492
if err != nil {
2593
panic(err)
2694
}
2795

96+
if conf.App.Logger == "" || conf.App.Logger == "stdout" {
97+
logger.SetOutput(os.Stdout)
98+
} else {
99+
var logPath string
100+
101+
if filepath.IsAbs(conf.App.Logger) {
102+
logPath = conf.App.Logger
103+
} else {
104+
logPath = filepath.Join(conf.App.DataPath, conf.App.Logger)
105+
}
106+
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_RDWR|os.O_APPEND, fs.ModePerm)
107+
if err != nil {
108+
log.Error("打开日志文件失败!")
109+
return
110+
}
111+
logger.SetOutput(logFile)
112+
}
113+
28114
log.Infof("当前版本: %s", version.Version)
29115

30116
// init db
@@ -39,14 +125,25 @@ func main() {
39125
logger.SetLevelAndFormat(logger.InfoLevel, &log.TextFormatter{})
40126
}
41127

42-
// run server
43-
srv := server.NewServer(conf)
44-
srv.Run()
45-
46-
sigs := make(chan os.Signal, 1)
47-
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
128+
app := NewApp(conf)
129+
sv, err := service.New(app, appCfg)
130+
if err != nil {
131+
panic(err)
132+
}
48133

49-
<-sigs
134+
switch *act {
135+
case "install":
136+
err = sv.Install()
137+
log.Infof("服务注册成功")
138+
case "uninstall":
139+
err = sv.Uninstall()
140+
log.Infof("服务取消成功")
141+
default:
142+
err = sv.Run()
143+
log.Info("程序退出")
144+
}
145+
if err != nil {
146+
panic(err)
147+
}
50148

51-
log.Info("程序退出")
52149
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ require (
6161
github.com/jinzhu/now v1.1.5 // indirect
6262
github.com/josharian/intern v1.0.0 // indirect
6363
github.com/json-iterator/go v1.1.12 // indirect
64+
github.com/kardianos/service v1.2.2 // indirect
6465
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
6566
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
6667
github.com/kr/fs v0.1.0 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
182182
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
183183
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
184184
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
185+
github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60=
186+
github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
185187
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
186188
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
187189
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
@@ -404,6 +406,7 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
404406
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
405407
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
406408
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
409+
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
407410
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
408411
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
409412
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

internal/config/config.yaml.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ app:
66
run_start: true
77
data_path: data
88
temp_date: 336h
9+
logger: stdout
910

1011
db:
1112
driver: sqlite

internal/config/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type App struct {
4141
RunStart bool `yaml:"run_start"`
4242
DataPath string `yaml:"data_path"` // db file and tmp path
4343
TempDate time.Duration `yaml:"temp_date"`
44+
Logger string `yaml:"logger"`
4445
}
4546

4647
// NewServerConfig 加载优先级路径 > 当前目录的config.yaml > 打包在可执行文件里的config.yaml.example

pkg/logger/logger.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package logger
33
import (
44
log "github.com/sirupsen/logrus"
55
"io"
6+
"os"
67
)
78

89
type Level uint32
@@ -19,6 +20,7 @@ const (
1920

2021
var (
2122
logLevel Level = DebugLevel
23+
logWriter io.Writer = os.Stdout
2224
logFormat log.Formatter = &log.TextFormatter{}
2325
)
2426

@@ -31,9 +33,15 @@ func SetLevelAndFormat(l Level, formatter log.Formatter) {
3133
logFormat = formatter
3234
}
3335

36+
func SetOutput(writer io.Writer) {
37+
log.SetOutput(writer)
38+
logWriter = writer
39+
}
40+
3441
func NewLogger(service string) *Logger {
3542
l := log.New()
3643
l.SetFormatter(logFormat)
44+
l.SetOutput(logWriter)
3745
logger := &Logger{
3846
entry: l.WithField("service", service),
3947
}

vendor/github.com/kardianos/service/.gitignore

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/kardianos/service/.travis.yml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/kardianos/service/LICENSE

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/kardianos/service/README.md

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)