forked from ibuler/rconsole
/
main.go
252 lines (203 loc) · 6.26 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"encoding/json"
"flag"
"github.com/astaxie/beego"
"github.com/shelmesky/rconsole/client"
"github.com/shelmesky/rconsole/controllers/api"
"github.com/shelmesky/rconsole/controllers/libvirt"
"github.com/shelmesky/rconsole/controllers/primary"
"github.com/shelmesky/rconsole/controllers/spice"
"github.com/shelmesky/rconsole/controllers/websocket"
"github.com/shelmesky/rconsole/mongo"
"github.com/shelmesky/rconsole/utils"
"gopkg.in/alexzorin/libvirt-go.v2"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
"time"
)
// 全局配置
type GlobalConfig struct {
LogFile string `json:"log_file"`
ListenAddress string `json:"listen_address"`
ProfileAddress string `json:"profile_address"`
MongoURL string `json:"mongo_url"`
MongoTimeout int `json:"mongo_timeout"`
ServerProfile bool
ShowCheckConn bool `json:"show_check_conn"`
ServerDebug bool `json:"debug"`
GuacdServer []client.Guacd `json:"guacd"`
}
var (
SignalChan chan os.Signal
Config GlobalConfig
// 命令行配置
ConfigFile = flag.String("config", "./config.json", "")
LogFile = flag.String("log_file", "", "logging file, default: /var/log/rconsole.log")
ProfileAddress = flag.String("profile_address", "", "profile server listen on, default: 0.0.0.0:9998")
ListenAddress = flag.String("listen", "", "server listen on, default: 0.0.0.0:9999")
MongoURL = flag.String("mongo_url", "", "MongoDB URL, default: mongo://127.0.0.1/rconsole")
MongoTimeout = flag.Int("mongo_timeout", 0, "Wait n seconds for connect to MongoDB, default: 1")
ShowCheckConn = flag.Bool("show_check_conn", false, "Show check MongoDB server informatioin")
ServerProfile = flag.Bool("profile", true, "Start web profile interface")
ServerDebug = flag.Bool("debug", false, "Print debug information when server is running.")
)
// 判断文件或目录是否存在
func Exist(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}
/*
判断命令行参数和配置文件
如果配置文件不存在,则会使用命令行的参数
否则直接使用配置文件中的参数
*/
func Init() {
if *ServerProfile == true {
Config.ServerProfile = true
}
if !Exist(*ConfigFile) {
if *LogFile == "" {
*LogFile = "/var/log/rconsole.log"
}
Config.LogFile = *LogFile
if *ProfileAddress == "" {
*ProfileAddress = "0.0.0.0:9998"
}
Config.ProfileAddress = *ProfileAddress
if *ListenAddress == "" {
*ListenAddress = "0.0.0.0:9999"
}
Config.ListenAddress = *ListenAddress
if *MongoURL == "" {
*MongoURL = "mongo://127.0.0.1/rconsole"
}
Config.MongoURL = *MongoURL
if *MongoTimeout == 0 {
*MongoTimeout = 1
}
Config.MongoTimeout = *MongoTimeout
if *ShowCheckConn == true {
*ShowCheckConn = true
}
Config.ShowCheckConn = *ShowCheckConn
if *ServerDebug == true {
*ServerDebug = true
}
Config.ServerDebug = *ServerDebug
} else {
data, err := ioutil.ReadFile(*ConfigFile)
if err != nil {
utils.Println(err)
return
}
err = json.Unmarshal(data, &Config)
if err != nil {
utils.Println(err)
}
if *LogFile != "" {
Config.LogFile = *LogFile
}
if *ProfileAddress != "" {
Config.ProfileAddress = *ProfileAddress
}
if *ListenAddress != "" {
Config.ListenAddress = *ListenAddress
}
if *MongoURL != "" {
Config.MongoURL = *MongoURL
}
if *MongoTimeout != 0 {
Config.MongoTimeout = *MongoTimeout
}
if *ShowCheckConn != false {
Config.ShowCheckConn = *ShowCheckConn
}
if *ServerDebug != false {
Config.ServerDebug = *ServerDebug
}
}
}
// POSIX信号发生时的回调
func SignalCallback() {
for s := range SignalChan {
sig := s.String()
utils.Println("Got Signal: ", sig)
if s == syscall.SIGINT {
utils.Println("RConsole server exit...")
os.Exit(0)
}
}
}
func main() {
ret := libvirt.EventRegisterDefaultImpl()
utils.Println("EventRegisterDefaultImpl ret:", ret)
runtime.GOMAXPROCS(runtime.NumCPU())
defer func() {
if err := recover(); err != nil {
utils.Println("Server Error: ", err.(string))
}
}()
// 解析命令行参数
flag.Parse()
// 自定义初始化
Init()
// 设置guacd客户端连接时的调试信息
client.ClientDebug = Config.ServerDebug
// 初始化guacd服务连接池
client.Pool.Init(Config.GuacdServer)
utils.Println("Try connect to:", Config.MongoURL)
// 初始化MongoDB的连接
timeout := time.Duration(Config.MongoTimeout) * time.Second
err := mongo.InitMongoDB(Config.MongoURL, timeout)
if err != nil {
utils.Println(err)
os.Exit(1)
}
// 启动独立的线程测试MongoDB连接
go mongo.TouchMongoDB(Config.ShowCheckConn)
utils.Println("Connect to MongoDB OK")
// 捕捉信号并设置回调函数
SignalChan = make(chan os.Signal, 1)
signal.Notify(SignalChan,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
syscall.SIGPIPE,
syscall.SIGALRM,
syscall.SIGPIPE)
go SignalCallback()
// 启动性能调试接口
if Config.ServerProfile == true {
go func() {
http.ListenAndServe(Config.ProfileAddress, nil)
}()
}
utils.Printf("http profile server Running on %s\n", Config.ProfileAddress)
addr_splited := strings.Split(Config.ListenAddress, ":")
if len(addr_splited) < 2 {
utils.Println("the format of listen address is invalid!")
os.Exit(1)
}
beego.HttpAddr = addr_splited[0]
beego.HttpPort, err = strconv.Atoi(addr_splited[1])
beego.SetStaticPath("/static", "./static")
beego.Router("/connect", &controllers.MainController{})
beego.Router("/ws", &wscontrollers.WebSocketController{})
beego.Router("/ws/spice", &spicecontrollers.SpiceController{})
beego.Router("/ws/libvirt", &libvirtcontrollers.LibvirtController{})
beego.Router("/api/conn/list", &managercontrollers.ConnectionManagerController{}, "get:ListConnection")
beego.Router("/api/conn/create/:conn_type([a-z]+)", &managercontrollers.ConnectionManagerController{}, "post:CreateConnection")
beego.Router("/api/conn/update/:conn_type([a-z]+)", &managercontrollers.ConnectionManagerController{}, "put:UpdateConnection")
beego.Router("/api/conn/delete", &managercontrollers.ConnectionManagerController{}, "delete:DeleteConnection")
beego.Router("/api/libvirt/host", &libvirtcontrollers.HostController{})
beego.Run()
}