-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_windows.go
112 lines (88 loc) · 2.03 KB
/
service_windows.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
package main
import (
"flag"
"log"
"os"
"path/filepath"
"encoding/json"
"github.com/kardianos/service"
"gopkg.in/natefinch/lumberjack.v2" // logging
)
func (s *appServer) Start(svc service.Service) error {
// Start should not block. Do the actual work async.
LogInfo("start servico")
go s.run()
return nil
}
func (s *appServer) Stop(svc service.Service) error {
LogInfo("stop servico")
s.stop()
return nil
}
func serviceMain(appPath string, cfg *appServerConfig) {
svcConfig := &service.Config{
Name: "fast HTTP Service",
/*DisplayName: "XXXXX",
Description: "yYYYYYYYY",*/
}
prg := &appServer{appPath: appPath, cfg: cfg}
s, err := service.New(prg, svcConfig)
if err != nil {
LogError(err.Error())
}
err = s.Run()
if err != nil {
LogCritical(err.Error())
}
}
func main() {
var noservice bool
var err error
var raw []byte
var ex string
var cfg *appServerConfig
flag.BoolVar(&noservice, "noservice", false, "como processo autonomo")
flag.Parse()
ex, err = os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
raw, err = os.ReadFile(exPath + "\\config.json")
if err != nil {
log.SetOutput(&lumberjack.Logger{
Filename: exPath + "\\paniclogger.txt",
MaxSize: LOG_MAXSIZE,
MaxBackups: LOG_MAXBACKUPS,
MaxAge: LOG_MAXAGE,
})
LogCriticalf("Startup: %s", err.Error())
} else {
cfg = &appServerConfig{}
err = json.Unmarshal(raw, cfg)
if err != nil {
log.SetOutput(&lumberjack.Logger{
Filename: exPath + "\\paniclogger.txt",
MaxSize: LOG_MAXSIZE,
MaxBackups: LOG_MAXBACKUPS,
MaxAge: LOG_MAXAGE,
})
LogCriticalf("Startup, unmarshal error, config file %s: %v \n", filepath.Base("config.json"), err.Error())
} else {
log.SetOutput(&lumberjack.Logger{
Filename: exPath + cfg.Generic.Logpath,
MaxSize: LOG_MAXSIZE,
MaxBackups: LOG_MAXBACKUPS,
MaxAge: LOG_MAXAGE,
})
}
}
if err == nil {
//noservice = true
if noservice {
timedserve(exPath, cfg)
} else {
serviceMain(exPath, cfg)
}
}
}