This repository has been archived on 2019-08-22. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
tamalsaha 05ff0fc766
All checks were successful
continuous-integration/drone/push Build is passing
Use Use github.com/unknwon/com (#4)
2019-08-22 01:31:22 +00:00
.drone.yml add drone support (#3) 2019-08-04 15:18:29 +00:00
.gitignore toolbox: able to turns off debug functionality (#8) 2018-08-18 15:23:02 +08:00
LICENSE first commit 2014-08-05 22:07:28 -04:00
README.md add gomod support (#1) 2019-08-03 07:44:50 +00:00
go.mod Use Use github.com/unknwon/com (#4) 2019-08-22 01:31:22 +00:00
go.sum Use Use github.com/unknwon/com (#4) 2019-08-22 01:31:22 +00:00
healthcheck.go fix import 2015-10-14 00:06:56 -04:00
healthcheck_test.go add gomod support (#1) 2019-08-03 07:44:50 +00:00
profile.go Use Use github.com/unknwon/com (#4) 2019-08-22 01:31:22 +00:00
profile_test.go fix import 2015-10-14 00:06:56 -04:00
statistic.go Merge branch 'master' of github.com:go-macaron/toolbox 2015-11-02 13:50:57 -05:00
statistic_test.go Merge branch 'master' of github.com:go-macaron/toolbox 2015-11-02 13:50:57 -05:00
toolbox.go add gomod support (#1) 2019-08-03 07:44:50 +00:00

toolbox

Middleware toolbox provides health chcek, pprof, profile and statistic services for Macaron.

Build Status API Reference

Installation

go get gitea.com/macaron/toolbox

Usage

// main.go
import (
	"gitea.com/macaron/macaron"
	"gitea.com/macaron/toolbox"
)

func main() {
  	m := macaron.Classic()
  	m.Use(toolbox.Toolboxer(m))
	m.Run()
}

Open your browser and visit http://localhost:4000/debug to see the effects.

Options

toolbox.Toolboxer comes with a variety of configuration options:

type dummyChecker struct {
}

func (dc *dummyChecker) Desc() string {
	return "Dummy checker"
}

func (dc *dummyChecker) Check() error {
	return nil
}

// ...
m.Use(toolbox.Toolboxer(m, toolbox.Options{
	URLPrefix:			"/debug",			// URL prefix for toolbox dashboard
	HealthCheckURL:		"/healthcheck", 	// URL for health check request
	HealthCheckers: []HealthChecker{
		new(dummyChecker),
	},										// Health checkers
	HealthCheckFuncs: []*toolbox.HealthCheckFuncDesc{
		&toolbox.HealthCheckFuncDesc{
			Desc: "Database connection",
			Func: func() error { return "OK" },
		},
	},										// Health check functions
	DisableDebug:		false,				// Turns off all debug functionality when true
	PprofURLPrefix:		"/debug/pprof/", 	// URL prefix of pprof
	ProfileURLPrefix:	"/debug/profile/", 	// URL prefix of profile
	ProfilePath:		"profile",			// Path store profile files
}))
// ...

Route Statistic

Toolbox also comes with a route call statistic functionality:

import (
	"os"
	"time"
	//...
	"gitea.com/macaron/toolbox"
)

func main() {
	//...
	m.Get("/", func(t toolbox.Toolbox) {
		start := time.Now()
		
		// Other operations.
		
		t.AddStatistics("GET", "/", time.Since(start))
	})
	
	m.Get("/dump", func(t toolbox.Toolbox) {
		t.GetMap(os.Stdout)
	})
}

Output take from test:

+---------------------------------------------------+------------+------------------+------------------+------------------+------------------+------------------+
| Request URL                                       | Method     | Times            | Total Used(s)    | Max Used(μs)     | Min Used(μs)     | Avg Used(μs)     |
+---------------------------------------------------+------------+------------------+------------------+------------------+------------------+------------------+
| /api/user                                         | POST       |                2 |         0.000122 |       120.000000 |         2.000000 |        61.000000 |
| /api/user                                         | GET        |                1 |         0.000013 |        13.000000 |        13.000000 |        13.000000 |
| /api/user                                         | DELETE     |                1 |         0.000001 |         1.400000 |         1.400000 |         1.400000 |
| /api/admin                                        | POST       |                1 |         0.000014 |        14.000000 |        14.000000 |        14.000000 |
| /api/user/unknwon                                 | POST       |                1 |         0.000012 |        12.000000 |        12.000000 |        12.000000 |
+---------------------------------------------------+------------+------------------+------------------+------------------+------------------+------------------+

License

This project is under Apache v2 License. See the LICENSE file for the full license text.