From 8e5bfe6e8f8880f8bf1430e42154be8dfb0b3ddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D0=B5=D0=BA=D1=81=D1=96=D0=B9=20=D0=A7=D0=B5?= =?UTF-8?q?=D1=87=D0=B5=D0=BB=D1=8C?= Date: Wed, 31 Mar 2021 00:38:16 +0300 Subject: [PATCH] Add DebugFunc method --- engine.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/engine.go b/engine.go index f4f4b52..5aa4a5f 100644 --- a/engine.go +++ b/engine.go @@ -38,6 +38,8 @@ type ( AppEngine bool // Print debug messages to log Debug bool + + DebugFunc func(*Context, time.Duration) // fasthhtp server Server *fasthttp.Server @@ -60,6 +62,8 @@ type ( Config struct { // Print debug messages to log Debug bool + // DebugFunc is callback function that calls after context + DebugFunc func(*Context, time.Duration) // Extensions to parse template files from. Defaults to [".html"]. TemplatesExtensions []string // Directories to load templates. Default is ["templates"]. @@ -95,6 +99,7 @@ var ( func New(config ...*Config) *Engine { var r *render.Render var cfgDebug bool + var cfgDebugFunc func(*Context, time.Duration) rCfg := &render.Config{} if len(config) != 0 && config[0] != nil { if len(config[0].TemplatesDirs) != 0 { @@ -108,6 +113,7 @@ func New(config ...*Config) *Engine { } } cfgDebug = config[0].Debug + cfgDebugFunc = config[0].DebugFunc } r = render.New(rCfg) @@ -118,6 +124,7 @@ func New(config ...*Config) *Engine { Render: r, RedirectTrailingSlash: true, Debug: cfgDebug, + DebugFunc: cfgDebugFunc, Server: &fasthttp.Server{}, } engine.RouterGroup = *newRouteGroup("", engine, make([]Handler, 0)) @@ -195,6 +202,9 @@ func (engine *Engine) HandleRequest(ctx *fasthttp.RequestCtx) { c.Next() engine.pool.Put(c) engine.debug(fmt.Sprintf("%-21s | %d | %9v | %-7s %-25s ", time.Now().Format("2006/01/02 - 15:04:05"), c.Response.StatusCode(), time.Since(start), string(ctx.Method()), string(ctx.Path()))) + if engine.DebugFunc != nil { + engine.DebugFunc(c, time.Since(start)) + } } fin() }