You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.4 KiB

  1. toolbox
  2. =======
  3. Middleware toolbox provides health chcek, pprof, profile and statistic services for [Macaron](https://github.com/go-macaron/macaron).
  4. [API Reference](https://gowalker.org/github.com/go-macaron/toolbox)
  5. ### Installation
  6. go get github.com/go-macaron/toolbox
  7. ## Usage
  8. ```go
  9. // main.go
  10. import (
  11. "gopkg.in/macaron.v1"
  12. "github.com/go-macaron/toolbox"
  13. )
  14. func main() {
  15. m := macaron.Classic()
  16. m.Use(toolbox.Toolboxer(m))
  17. m.Run()
  18. }
  19. ```
  20. Open your browser and visit `http://localhost:4000/debug` to see the effects.
  21. ## Options
  22. `toolbox.Toolboxer` comes with a variety of configuration options:
  23. ```go
  24. type dummyChecker struct {
  25. }
  26. func (dc *dummyChecker) Desc() string {
  27. return "Dummy checker"
  28. }
  29. func (dc *dummyChecker) Check() error {
  30. return nil
  31. }
  32. // ...
  33. m.Use(toolbox.Toolboxer(m, toolbox.Options{
  34. URLPrefix: "/debug", // URL prefix for toolbox dashboard.
  35. HealthCheckURL: "/healthcheck", // URL for health check request.
  36. HealthCheckers: []HealthChecker{
  37. new(dummyChecker),
  38. }, // Health checkers.
  39. HealthCheckFuncs: []*toolbox.HealthCheckFuncDesc{
  40. &toolbox.HealthCheckFuncDesc{
  41. Desc: "Database connection",
  42. Func: func() error { return "OK" },
  43. },
  44. }, // Health check functions.
  45. PprofURLPrefix: "/debug/pprof/", // URL prefix of pprof.
  46. ProfileURLPrefix: "/debug/profile/", // URL prefix of profile.
  47. ProfilePath: "profile", // Path store profile files.
  48. }))
  49. // ...
  50. ```
  51. ## Route Statistic
  52. Toolbox also comes with a route call statistic functionality:
  53. ```go
  54. import (
  55. "os"
  56. "time"
  57. //...
  58. "github.com/go-macaron/toolbox"
  59. )
  60. func main() {
  61. //...
  62. m.Get("/", func(t toolbox.Toolbox) {
  63. start := time.Now()
  64. // Other operations.
  65. t.AddStatistics("GET", "/", time.Since(start))
  66. })
  67. m.Get("/dump", func(t toolbox.Toolbox) {
  68. t.GetMap(os.Stdout)
  69. })
  70. }
  71. ```
  72. Output take from test:
  73. ```
  74. +---------------------------------------------------+------------+------------------+------------------+------------------+------------------+------------------+
  75. | Request URL | Method | Times | Total Used(s) | Max Used(μs) | Min Used(μs) | Avg Used(μs) |
  76. +---------------------------------------------------+------------+------------------+------------------+------------------+------------------+------------------+
  77. | /api/user | POST | 2 | 0.000122 | 120.000000 | 2.000000 | 61.000000 |
  78. | /api/user | GET | 1 | 0.000013 | 13.000000 | 13.000000 | 13.000000 |
  79. | /api/user | DELETE | 1 | 0.000001 | 1.400000 | 1.400000 | 1.400000 |
  80. | /api/admin | POST | 1 | 0.000014 | 14.000000 | 14.000000 | 14.000000 |
  81. | /api/user/unknwon | POST | 1 | 0.000012 | 12.000000 | 12.000000 | 12.000000 |
  82. +---------------------------------------------------+------------+------------------+------------------+------------------+------------------+------------------+
  83. ```
  84. ## License
  85. This project is under Apache v2 License. See the [LICENSE](LICENSE) file for the full license text.