diff --git a/g/net/ghttp/ghttp_server_admin.go b/g/net/ghttp/ghttp_server_admin.go new file mode 100644 index 000000000..3f5778e68 --- /dev/null +++ b/g/net/ghttp/ghttp_server_admin.go @@ -0,0 +1,57 @@ +// Copyright 2018 gf Author(https://gitee.com/johng/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://gitee.com/johng/gf. +// pprof封装. + +package ghttp + +import ( + "strings" + "gitee.com/johng/gf/g/os/gview" +) + +// 用于服务管理的对象 +type utilAdmin struct {} + +// 服务管理首页 +func (p *utilAdmin) Index(r *Request) { + data := map[string]interface{}{ + "uri" : strings.TrimRight(r.URL.Path, "/"), + } + buffer, _ := gview.ParseContent(` + + + gf ghttp admin + + +

restart

+

shutdown

+ + + `, data) + r.Response.Write(buffer) +} + +// 服务重启 +func (p *utilAdmin) Restart(r *Request) { + r.Response.Write("restart server") + r.Server.Restart() +} + +// 服务关闭 +func (p *utilAdmin) Shutdown(r *Request) { + r.Response.Write("shutdown server") + r.Server.Shutdown() +} + + +// 开启服务管理支持 +func (s *Server) EnableAdmin(pattern...string) { + p := "/debug/admin" + if len(pattern) > 0 { + p = pattern[0] + } + s.BindObject(p, &utilAdmin{}) +} \ No newline at end of file diff --git a/g/net/ghttp/ghttp_server_auto.go b/g/net/ghttp/ghttp_server_auto.go deleted file mode 100644 index f889b1ed3..000000000 --- a/g/net/ghttp/ghttp_server_auto.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2018 gf Author(https://gitee.com/johng/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://gitee.com/johng/gf. -// 异步工作协程. - -package ghttp - -// 开启异步队列处理循环,该异步线程与Server同生命周期 -func (s *Server) startCloseQueueLoop() { - go func() { - for { - if v := s.closeQueue.PopFront(); v != nil { - r := v.(*Request) - s.callHookHandler(r, "BeforeClose") - // 关闭当前会话的Cookie - r.Cookie.Close() - // 更新Session会话超时时间 - r.Session.UpdateExpire() - s.callHookHandler(r, "AfterClose") - } - } - }() -} \ No newline at end of file diff --git a/g/net/ghttp/ghttp_server_handler.go b/g/net/ghttp/ghttp_server_handler.go index 9550fc9cb..1d10478cc 100644 --- a/g/net/ghttp/ghttp_server_handler.go +++ b/g/net/ghttp/ghttp_server_handler.go @@ -16,8 +16,8 @@ import ( "net/url" "net/http" "gitee.com/johng/gf/g/os/gfile" - "gitee.com/johng/gf/g/encoding/ghtml" "gitee.com/johng/gf/g/os/gtime" + "gitee.com/johng/gf/g/encoding/ghtml" ) // 默认HTTP Server处理入口,http包底层默认使用了gorutine异步处理请求,所以这里不再异步执行 @@ -169,3 +169,20 @@ func (s *Server)listDir(r *Request, f http.File) { } r.Response.Write("\n") } + +// 开启异步队列处理循环,该异步线程与Server同生命周期 +func (s *Server) startCloseQueueLoop() { + go func() { + for { + if v := s.closeQueue.PopFront(); v != nil { + r := v.(*Request) + s.callHookHandler(r, "BeforeClose") + // 关闭当前会话的Cookie + r.Cookie.Close() + // 更新Session会话超时时间 + r.Session.UpdateExpire() + s.callHookHandler(r, "AfterClose") + } + } + }() +} \ No newline at end of file diff --git a/g/net/ghttp/ghttp_server_pprof.go b/g/net/ghttp/ghttp_server_pprof.go index b5043394e..0205a0b3a 100644 --- a/g/net/ghttp/ghttp_server_pprof.go +++ b/g/net/ghttp/ghttp_server_pprof.go @@ -11,13 +11,13 @@ import ( "strings" runpprof "runtime/pprof" netpprof "net/http/pprof" - "gitee.com/johng/gf/g/frame/gins" + "gitee.com/johng/gf/g/os/gview" ) // 用于pprof的对象 -type utilpprof struct {} +type utilPprof struct {} -func (p *utilpprof) Index(r *Request) { +func (p *utilPprof) Index(r *Request) { profiles := runpprof.Profiles() action := r.Get("action") data := map[string]interface{}{ @@ -25,8 +25,7 @@ func (p *utilpprof) Index(r *Request) { "profiles" : profiles, } if len(action) == 0 { - view := gins.View() - buffer, _ := view.ParseContent(` + buffer, _ := gview.ParseContent(` gf ghttp pprof @@ -52,19 +51,19 @@ func (p *utilpprof) Index(r *Request) { } } -func (p *utilpprof) Cmdline(r *Request) { +func (p *utilPprof) Cmdline(r *Request) { netpprof.Cmdline(r.Response.Writer, &r.Request) } -func (p *utilpprof) Profile(r *Request) { +func (p *utilPprof) Profile(r *Request) { netpprof.Profile(r.Response.Writer, &r.Request) } -func (p *utilpprof) Symbol(r *Request) { +func (p *utilPprof) Symbol(r *Request) { netpprof.Symbol(r.Response.Writer, &r.Request) } -func (p *utilpprof) Trace(r *Request) { +func (p *utilPprof) Trace(r *Request) { netpprof.Trace(r.Response.Writer, &r.Request) } @@ -74,7 +73,7 @@ func (s *Server) EnablePprof(pattern...string) { if len(pattern) > 0 { p = pattern[0] } - up := &utilpprof{} + up := &utilPprof{} _, _, uri, _ := s.parsePattern(p) uri = strings.TrimRight(uri, "/") s.BindHandler(uri + "/*action", up.Index) diff --git a/geg/net/ghttp/hot_restart/admin.go b/geg/net/ghttp/hot_restart/admin.go new file mode 100644 index 000000000..55438e229 --- /dev/null +++ b/geg/net/ghttp/hot_restart/admin.go @@ -0,0 +1,12 @@ +package main + +import ( + "gitee.com/johng/gf/g" +) + +func main() { + s := g.Server() + s.EnableAdmin() + s.SetPort(8199) + s.Run() +} \ No newline at end of file