gf/net/ghttp/ghttp_server_admin.go

93 lines
2.1 KiB
Go
Raw Normal View History

// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// 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,
// You can obtain one at https://github.com/gogf/gf.
// 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"
)
// 服务管理首页
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(`
<html>
<head>
<title>GoFrame Web Server Admin</title>
</head>
<body>
<p>PID: {{.pid}}</p>
<p><a href="{{$.uri}}/restart">Restart</a></p>
<p><a href="{{$.uri}}/shutdown">Shutdown</a></p>
</body>
</html>
`, data)
2019-06-19 09:06:52 +08:00
r.Response.Write(buffer)
}
// 服务重启
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())
}
}
// 服务关闭
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())
}
}
// 开启服务管理支持
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{})
}
// 关闭当前Web Server
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
}