-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_windows.go
171 lines (99 loc) · 2.55 KB
/
main_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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"flag"
"fmt"
"github.com/astaxie/beego"
"github.com/chai2010/winsvc"
"log"
"os"
"path/filepath"
_ "smartapp/initial/common"
_ "smartapp/initial/plugins"
_ "smartapp/routers"
)
var (
serve string="beegoTest"
appPath string
flagServiceName = flag.String("service-name", serve, "Set service name")
flagServiceDesc = flag.String("service-desc", serve+" service", "Set service description")
flagServiceInstall = flag.Bool("service-install", false, "Install service")
flagServiceUninstall = flag.Bool("service-remove", false, "Remove service")
flagServiceStart = flag.Bool("service-start", false, "Start service")
flagServiceStop = flag.Bool("service-stop", false, "Stop service")
//查询服务
flagServiceQuery = flag.Bool("service-query", false, "Query service")
)
func init() {
// change to current dir
var err error
if appPath, err = winsvc.GetAppPath(); err != nil {
log.Fatal(err)
}
if err := os.Chdir(filepath.Dir(appPath)); err != nil {
log.Fatal(err)
}
}
func main() {
flag.Parse()
// install service
if *flagServiceInstall {
params:=""
if err := winsvc.InstallService(appPath, *flagServiceName, *flagServiceDesc,params); err != nil {
log.Fatalf("installService(%s, %s): %v\n", *flagServiceName, *flagServiceDesc, err)
}
fmt.Printf("Done\n")
return
}
//query service
if *flagServiceQuery {
if serName, err := winsvc.QueryService(*flagServiceName); err != nil {
log.Fatalln("removeService:", err)
}else{
fmt.Printf(serName+"\n")
}
fmt.Printf("Done\n")
return
}
// remove service
if *flagServiceUninstall {
if err := winsvc.RemoveService(*flagServiceName); err != nil {
log.Fatalln("removeService:", err)
}
fmt.Printf("Done\n")
return
}
// start service
if *flagServiceStart {
if err := winsvc.StartService(*flagServiceName); err != nil {
log.Fatalln("startService:", err)
}
fmt.Printf("Done\n")
return
}
// stop service
if *flagServiceStop {
if err := winsvc.StopService(*flagServiceName); err != nil {
log.Fatalln("stopService:", err)
}
fmt.Printf("Done\n")
return
}
// run as service
if !winsvc.InServiceMode() {
log.Println("main:", "runService")
if err := winsvc.RunAsService(*flagServiceName, StartServer, StopServer, false); err != nil {
log.Fatalf("svc.Run: %v\n", err)
}
return
}
// run as normal
StartServer()
}
func StartServer() {
//log.Println("StartServer, port = 8080")
beego.Run()
}
func StopServer() {
log.Println("StopServer")
panic("服务被停止")
}