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.

111 lines
3.5 KiB

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