2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-05-13 14:23:14 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-05-13 14:23:14 +08:00
|
|
|
|
// pprof封装.
|
|
|
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/os/gproc"
|
|
|
|
|
"github.com/gogf/gf/os/gtimer"
|
|
|
|
|
"github.com/gogf/gf/os/gview"
|
2018-05-16 22:32:56 +08:00
|
|
|
|
)
|
|
|
|
|
|
2018-05-13 14:23:14 +08:00
|
|
|
|
// 服务管理首页
|
|
|
|
|
func (p *utilAdmin) Index(r *Request) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"pid": gproc.Pid(),
|
|
|
|
|
"uri": strings.TrimRight(r.URL.Path, "/"),
|
|
|
|
|
}
|
|
|
|
|
buffer, _ := gview.ParseContent(`
|
2018-05-13 14:23:14 +08:00
|
|
|
|
<html>
|
|
|
|
|
<head>
|
2018-12-08 18:18:05 +08:00
|
|
|
|
<title>GoFrame Web Server Admin</title>
|
2018-05-13 14:23:14 +08:00
|
|
|
|
</head>
|
|
|
|
|
<body>
|
2018-12-08 18:18:05 +08:00
|
|
|
|
<p>PID: {{.pid}}</p>
|
|
|
|
|
<p><a href="{{$.uri}}/restart">Restart</a></p>
|
|
|
|
|
<p><a href="{{$.uri}}/shutdown">Shutdown</a></p>
|
2018-05-13 14:23:14 +08:00
|
|
|
|
</body>
|
|
|
|
|
</html>
|
2018-05-15 18:34:00 +08:00
|
|
|
|
`, data)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r.Response.Write(buffer)
|
2018-05-13 14:23:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-01 18:19:54 +08:00
|
|
|
|
// 服务重启
|
2018-05-13 14:23:14 +08:00
|
|
|
|
func (p *utilAdmin) Restart(r *Request) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
var err error = nil
|
|
|
|
|
// 必须检查可执行文件的权限
|
|
|
|
|
path := r.GetQueryString("newExeFilePath")
|
|
|
|
|
if path == "" {
|
|
|
|
|
path = os.Args[0]
|
|
|
|
|
}
|
|
|
|
|
// 执行重启操作
|
|
|
|
|
if len(path) > 0 {
|
|
|
|
|
err = RestartAllServer(path)
|
|
|
|
|
} else {
|
|
|
|
|
err = RestartAllServer()
|
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
r.Response.Write("server restarted")
|
|
|
|
|
} else {
|
|
|
|
|
r.Response.Write(err.Error())
|
|
|
|
|
}
|
2018-05-13 14:23:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 服务关闭
|
|
|
|
|
func (p *utilAdmin) Shutdown(r *Request) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r.Server.Shutdown()
|
|
|
|
|
if err := ShutdownAllServer(); err == nil {
|
|
|
|
|
r.Response.Write("server shutdown")
|
|
|
|
|
} else {
|
|
|
|
|
r.Response.Write(err.Error())
|
|
|
|
|
}
|
2018-05-13 14:23:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 开启服务管理支持
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (s *Server) EnableAdmin(pattern ...string) {
|
|
|
|
|
p := "/debug/admin"
|
|
|
|
|
if len(pattern) > 0 {
|
|
|
|
|
p = pattern[0]
|
|
|
|
|
}
|
|
|
|
|
s.BindObject(p, &utilAdmin{})
|
2018-05-16 22:32:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-28 23:57:20 +08:00
|
|
|
|
// 关闭当前Web Server
|
2018-05-16 22:32:56 +08:00
|
|
|
|
func (s *Server) Shutdown() error {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
// 非终端信号下,异步1秒后再执行关闭,
|
|
|
|
|
// 目的是让接口能够正确返回结果,否则接口会报错(因为web server关闭了)
|
|
|
|
|
gtimer.SetTimeout(time.Second, func() {
|
|
|
|
|
// 只关闭当前的Web Server
|
|
|
|
|
for _, v := range s.servers {
|
|
|
|
|
v.close()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return nil
|
2018-06-01 18:19:54 +08:00
|
|
|
|
}
|