improve package ghttp

This commit is contained in:
John Guo
2021-01-19 19:18:39 +08:00
parent e1418a2caa
commit fe80881ec9
3 changed files with 22 additions and 8 deletions

View File

@ -33,7 +33,7 @@ type Request struct {
Router *Router // Matched Router for this request. Note that it's not available in HOOK handler.
EnterTime int64 // Request starting time in microseconds.
LeaveTime int64 // Request ending time in microseconds.
Middleware *Middleware // Middleware manager.
Middleware *middleware // Middleware manager.
StaticFile *StaticFile // Static file object for static file serving.
context context.Context // Custom context for internal usage purpose.
handlers []*handlerParsedItem // All matched handlers containing handler, hook and middleware for this request.
@ -75,7 +75,7 @@ func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
request.Cookie = GetCookie(request)
request.Session = s.sessionManager.New(request.GetSessionId())
request.Response.Request = request
request.Middleware = &Middleware{
request.Middleware = &middleware{
request: request,
}
// Custom session id creating function.

View File

@ -14,8 +14,8 @@ import (
"github.com/gogf/gf/util/gutil"
)
// Middleware is the plugin for request workflow management.
type Middleware struct {
// middleware is the plugin for request workflow management.
type middleware struct {
served bool // Is the request served, which is used for checking response status 404.
request *Request // The request object pointer.
handlerIndex int // Index number for executing sequence purpose for handler items.
@ -24,7 +24,7 @@ type Middleware struct {
// Next calls the next workflow handler.
// It's an important function controlling the workflow of the server request execution.
func (m *Middleware) Next() {
func (m *middleware) Next() {
var item *handlerParsedItem
var loop = true
for loop {

View File

@ -18,9 +18,18 @@ import (
type utilPProf struct{}
const (
defaultPProfPattern = "/debug/pprof"
defaultPProfServerName = "pprof-server"
defaultPProfPattern = "/debug/pprof"
)
// StartPProfServer starts and runs a new server for pprof.
func StartPProfServer(port int, pattern ...string) {
s := GetServer(defaultPProfServerName)
s.EnablePProf()
s.SetPort(port)
s.Run()
}
// EnablePProf enables PProf feature for server.
func (s *Server) EnablePProf(pattern ...string) {
s.Domain(defaultDomainName).EnablePProf(pattern...)
@ -56,13 +65,18 @@ func (p *utilPProf) Index(r *Request) {
buffer, _ := gview.ParseContent(`
<html>
<head>
<title>gf ghttp pprof</title>
<title>GoFrame PProf</title>
</head>
{{$uri := .uri}}
<body>
profiles:<br>
<table>
{{range .profiles}}<tr><td align=right>{{.Count}}<td><a href="{{$uri}}{{.Name}}?debug=1">{{.Name}}</a>{{end}}
{{range .profiles}}
<tr>
<td align=right>{{.Count}}</td>
<td><a href="{{$uri}}{{.Name}}?debug=1">{{.Name}}</a></td>
<tr>
{{end}}
</table>
<br><a href="{{$uri}}goroutine?debug=2">full goroutine stack dump</a><br>
</body>