-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
212 lines (170 loc) · 4.02 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"encoding/json"
"flag"
"github.com/gin-gonic/gin"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
)
var generateConfig = flag.String("generate","",
"generate sample config.json file, Example '--generate config.json' will generate config file in current directory ")
var signalFlag = flag.String("signal","","reload")
type ConfigFileStruct struct {
Port int `json:"port"`
Cmd CmdRoute `json:"cmd_config"`
}
var UserConfig = ConfigFileStruct{}
type OUTPUT_MODE int
const (
ModeDebug OUTPUT_MODE = iota
ModeRelease
)
var GO_OUTPUT_MODE OUTPUT_MODE
var logFile = "go_log.txt"
func main() {
GO_OUTPUT_MODE = ModeRelease
checkFlag()
//pid listen module
setupPidModule()
logConfig()
readConfig()
ginSetup()
}
func checkFlag() {
flag.Parse()
if len(*generateConfig) > 0 {
defer os.Exit(0)
configPath := *generateConfig
file, err := os.Create(configPath)
if err != nil {
Error.Println("create config file : ", configPath,"error : ",err)
return
}
defer file.Close()
_,err = io.WriteString(file,getSampleConfigJsonString())
if err != nil {
Error.Println("write config file error : ",err)
return
}
Info.Println("Finished! ",configPath," file successfully generated.")
}
if len(*signalFlag) > 0 {
if *signalFlag == "reload" {
sendSignal()
os.Exit(0)
}
}
}
func ginSetup() {
if GO_OUTPUT_MODE == ModeDebug {
gin.SetMode(gin.DebugMode)
}else {
gin.SetMode(gin.ReleaseMode)
}
route := gin.Default()
//pprof.Register(route,"debug/pprof")
port := 8888
configRoute(route)
if &UserConfig != nil && UserConfig.Port > 0 {
port = UserConfig.Port
}
portStr := ":"+strconv.Itoa(port)
Info.Println("Server Open on Port ",port)
err := route.Run(portStr)
if err != nil {
Error.Println("Server Open Failed :",err)
}
}
func configRoute(route *gin.Engine) {
//play routes config
if &UserConfig == nil {
Error.Println("Read Route Config Failed")
return;
}
Info.Println("Read Route Config Success")
//config cmd websocket route
configRoute_cmd_websocket(route)
}
func configRoute_cmd_websocket(route gin.IRouter){
if len(UserConfig.Cmd.Web_url) > 0 {
//download web page path
route.GET(UserConfig.Cmd.Web_url, func(c *gin.Context) {
cmdInfoPath := struct {
Socket_Path string
}{UserConfig.Cmd.Socket_url}
tmpl, err := template.New("socket_page").Parse(CmdSocketHTMLPage)
if err != nil {
Error.Println("Cmd Socket Web Page Tmpl Err ", err);
c.String(http.StatusNotFound,"something is wrong in this page")
}else {
c.Status(200)
tmpl.Execute(c.Writer,cmdInfoPath)
}
})
Info.Println("Build Cmd Socket")
route.GET(UserConfig.Cmd.Socket_url, func(c *gin.Context) {
cmdWebSocketHandler(c.Writer,c.Request)
})
}
}
func readConfig() {
filePossiblePath := "config.json"
UserConfig = *readConfigFile(filePossiblePath)
if &UserConfig == nil {
filePossiblePath = "config/config.json"
UserConfig = *readConfigFile(filePossiblePath)
}
//serailize cmd config
serializeCmds(UserConfig.Cmd)
}
func readConfigFile(filePath string) *ConfigFileStruct {
res := ConfigFileStruct{}
fileBlob,fileErr := ioutil.ReadFile(filePath)
if fileErr != nil {
Error.Println("Config Read Error : ",fileErr)
return nil
}
json.Unmarshal(fileBlob,&res)
return &res
}
func logConfig() {
f,err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
Error.Println("Open Log File Failed : ",err);
return;
}
w := io.MultiWriter(os.Stdout,f)
gin.DefaultWriter = w
if GO_OUTPUT_MODE == ModeDebug {
SetLogMode(DebugMode)
}else {
SetLogMode(ReleaseMode)
}
SetWriter(w)
Info.Println("Log File Path : ",filepath.Dir(os.Args[0]) + "/" + logFile)
}
func getSampleConfigJsonString() string {
return `
{
"port":18080,
"Cmd_config": {
"web_url": "/cmdsocket",
"socket_url": "/ws",
"cmds_file": "cmdsFile",
"cmds": [
{
"exec_alias": "pinggithub",
"cmd": "ping",
"param": "github.com"
}
],
"exit_string": "exit"
}
}
`
}