123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package toolbox
- import (
- "fmt"
- "io"
- "net/http"
- "net/http/pprof"
- "path"
- "time"
- "gopkg.in/macaron.v1"
- )
- const _VERSION = "0.1.3"
- func Version() string {
- return _VERSION
- }
- type Toolbox interface {
- AddHealthCheck(HealthChecker)
- AddHealthCheckFunc(string, HealthCheckFunc)
- AddStatistics(string, string, time.Duration)
- GetMap(io.Writer)
- JSON(io.Writer)
- }
- type toolbox struct {
- *UrlMap
- healthCheckJobs []*healthCheck
- }
- type Options struct {
-
- URLPrefix string
-
- HealthCheckURL string
-
- HealthCheckers []HealthChecker
-
- HealthCheckFuncs []*HealthCheckFuncDesc
-
- URLMapPrefix string
-
- PprofURLPrefix string
-
- ProfileURLPrefix string
-
- ProfilePath string
- }
- var opt Options
- func prepareOptions(options []Options) {
- if len(options) > 0 {
- opt = options[0]
- }
-
- if len(opt.URLPrefix) == 0 {
- opt.URLPrefix = "/debug"
- }
- if len(opt.HealthCheckURL) == 0 {
- opt.HealthCheckURL = "/healthcheck"
- }
- if len(opt.URLMapPrefix) == 0 {
- opt.URLMapPrefix = "/urlmap.json"
- }
- if len(opt.PprofURLPrefix) == 0 {
- opt.PprofURLPrefix = "/debug/pprof/"
- } else if opt.PprofURLPrefix[len(opt.PprofURLPrefix)-1] != '/' {
- opt.PprofURLPrefix += "/"
- }
- if len(opt.ProfileURLPrefix) == 0 {
- opt.ProfileURLPrefix = "/debug/profile/"
- } else if opt.ProfileURLPrefix[len(opt.ProfileURLPrefix)-1] != '/' {
- opt.ProfileURLPrefix += "/"
- }
- if len(opt.ProfilePath) == 0 {
- opt.ProfilePath = path.Join(macaron.Root, "profile")
- }
- }
- func dashboard(ctx *macaron.Context) string {
- return fmt.Sprintf(`<p>Toolbox Index:</p>
- <ol>
- <li><a href="%s">Pprof Information</a></li>
- <li><a href="%s">Profile Operations</a></li>
- </ol>`, opt.PprofURLPrefix, opt.ProfileURLPrefix)
- }
- var _ Toolbox = &toolbox{}
- func Toolboxer(m *macaron.Macaron, options ...Options) macaron.Handler {
- prepareOptions(options)
- t := &toolbox{
- healthCheckJobs: make([]*healthCheck, 0, len(opt.HealthCheckers)+len(opt.HealthCheckFuncs)),
- }
-
- m.Get(opt.URLPrefix, dashboard)
-
- for _, hc := range opt.HealthCheckers {
- t.AddHealthCheck(hc)
- }
- for _, fd := range opt.HealthCheckFuncs {
- t.AddHealthCheckFunc(fd.Desc, fd.Func)
- }
- m.Route(opt.HealthCheckURL, "HEAD,GET", t.handleHealthCheck)
-
- m.Get(opt.URLMapPrefix, func(rw http.ResponseWriter) {
- t.JSON(rw)
- })
-
- m.Any(path.Join(opt.PprofURLPrefix, "cmdline"), pprof.Cmdline)
- m.Any(path.Join(opt.PprofURLPrefix, "profile"), pprof.Profile)
- m.Any(path.Join(opt.PprofURLPrefix, "symbol"), pprof.Symbol)
- m.Any(opt.PprofURLPrefix, pprof.Index)
- m.Any(path.Join(opt.PprofURLPrefix, "*"), pprof.Index)
-
- profilePath = opt.ProfilePath
- m.Get(opt.ProfileURLPrefix, handleProfile)
-
- t.UrlMap = &UrlMap{
- urlmap: make(map[string]map[string]*Statistics),
- }
- return func(ctx *macaron.Context) {
- ctx.MapTo(t, (*Toolbox)(nil))
- }
- }
|