Initial commit
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package debug
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/owncloud/ocis-pkg/log"
|
||||
)
|
||||
|
||||
// Option defines a single option function.
|
||||
type Option func(o *Options)
|
||||
|
||||
// Options defines the available options for this package.
|
||||
type Options struct {
|
||||
Logger log.Logger
|
||||
Name string
|
||||
Version string
|
||||
Address string
|
||||
Token string
|
||||
Pprof bool
|
||||
Zpages bool
|
||||
Health func(http.ResponseWriter, *http.Request)
|
||||
Ready func(http.ResponseWriter, *http.Request)
|
||||
}
|
||||
|
||||
// newOptions initializes the available default options.
|
||||
func newOptions(opts ...Option) Options {
|
||||
opt := Options{}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&opt)
|
||||
}
|
||||
|
||||
return opt
|
||||
}
|
||||
|
||||
// Logger provides a function to set the logger option.
|
||||
func Logger(l log.Logger) Option {
|
||||
return func(o *Options) {
|
||||
o.Logger = l
|
||||
}
|
||||
}
|
||||
|
||||
// Name provides a function to set the name option.
|
||||
func Name(n string) Option {
|
||||
return func(o *Options) {
|
||||
o.Name = n
|
||||
}
|
||||
}
|
||||
|
||||
// Version provides a function to set the version option.
|
||||
func Version(v string) Option {
|
||||
return func(o *Options) {
|
||||
o.Version = v
|
||||
}
|
||||
}
|
||||
|
||||
// Address provides a function to set the address option.
|
||||
func Address(a string) Option {
|
||||
return func(o *Options) {
|
||||
o.Address = a
|
||||
}
|
||||
}
|
||||
|
||||
// Token provides a function to set the token option.
|
||||
func Token(t string) Option {
|
||||
return func(o *Options) {
|
||||
o.Token = t
|
||||
}
|
||||
}
|
||||
|
||||
// Pprof provides a function to set the pprof option.
|
||||
func Pprof(p bool) Option {
|
||||
return func(o *Options) {
|
||||
o.Pprof = p
|
||||
}
|
||||
}
|
||||
|
||||
// Zpages provides a function to set the zpages option.
|
||||
func Zpages(z bool) Option {
|
||||
return func(o *Options) {
|
||||
o.Pprof = z
|
||||
}
|
||||
}
|
||||
|
||||
// Health provides a function to set the health option.
|
||||
func Health(h func(http.ResponseWriter, *http.Request)) Option {
|
||||
return func(o *Options) {
|
||||
o.Health = h
|
||||
}
|
||||
}
|
||||
|
||||
// Ready provides a function to set the ready option.
|
||||
func Ready(r func(http.ResponseWriter, *http.Request)) Option {
|
||||
return func(o *Options) {
|
||||
o.Ready = r
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package debug
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
|
||||
"github.com/justinas/alice"
|
||||
"github.com/owncloud/ocis-pkg/middleware"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"go.opencensus.io/zpages"
|
||||
)
|
||||
|
||||
// NewService initializes a new debug service.
|
||||
func NewService(opts ...Option) *http.Server {
|
||||
dopts := newOptions(opts...)
|
||||
|
||||
dopts.Logger.Info().
|
||||
Str("transport", "debug").
|
||||
Str("addr", dopts.Address).
|
||||
Msg("Starting server")
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/metrics", alice.New(
|
||||
middleware.Token(
|
||||
dopts.Token,
|
||||
),
|
||||
).Then(
|
||||
promhttp.Handler(),
|
||||
))
|
||||
|
||||
mux.HandleFunc("/healthz", dopts.Health)
|
||||
mux.HandleFunc("/readyz", dopts.Ready)
|
||||
|
||||
if dopts.Pprof {
|
||||
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
||||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
||||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
||||
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
||||
}
|
||||
|
||||
if dopts.Zpages {
|
||||
zpages.Handle(mux, "/debug")
|
||||
}
|
||||
|
||||
return &http.Server{
|
||||
Addr: dopts.Address,
|
||||
Handler: alice.New(
|
||||
middleware.RealIP,
|
||||
middleware.RequestID,
|
||||
middleware.Cache,
|
||||
middleware.Cors,
|
||||
middleware.Secure,
|
||||
middleware.Version(
|
||||
dopts.Name,
|
||||
dopts.Version,
|
||||
),
|
||||
).Then(
|
||||
mux,
|
||||
),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user