This commit is contained in:
Thomas Müller
2019-12-04 17:15:11 +01:00
commit d9e7f1a986
64 changed files with 6442 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
package debug
import (
"context"
"github.com/owncloud/ocis-graph/pkg/config"
)
func newOptions(opts ...Option) Options {
opt := Options{}
for _, o := range opts {
o(&opt)
}
return opt
}
type Option func(o *Options)
type Options struct {
Context context.Context
Config *config.Config
}
func Context(val context.Context) Option {
return func(o *Options) {
o.Context = val
}
}
func Config(val *config.Config) Option {
return func(o *Options) {
o.Config = val
}
}
+76
View File
@@ -0,0 +1,76 @@
package debug
import (
"io"
"net/http"
"net/http/pprof"
"github.com/justinas/alice"
"github.com/micro/go-micro/util/log"
"github.com/owncloud/ocis-graph/pkg/middleware"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opencensus.io/zpages"
)
type Handler struct {
*http.ServeMux
}
func (h *Handler) healthz(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
io.WriteString(w, http.StatusText(http.StatusOK))
}
func (h *Handler) readyz(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
io.WriteString(w, http.StatusText(http.StatusOK))
}
func Server(opts ...Option) (*http.Server, error) {
options := newOptions(opts...)
log.Infof("Server [debug] listening on [%s]", options.Config.Debug.Addr)
mux := http.NewServeMux()
handler := &Handler{mux}
handler.Handle("/metrics", alice.New(
middleware.Token(
options.Config.Debug.Token,
),
).Then(
promhttp.Handler(),
))
handler.HandleFunc("/healthz", handler.healthz)
handler.HandleFunc("/readyz", handler.readyz)
if options.Config.Debug.Pprof {
handler.HandleFunc("/debug/pprof/", pprof.Index)
handler.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
handler.HandleFunc("/debug/pprof/profile", pprof.Profile)
handler.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
handler.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
if options.Config.Debug.Zpages {
zpages.Handle(mux, "/debug")
}
return &http.Server{
Addr: options.Config.Debug.Addr,
Handler: alice.New(
middleware.RealIP,
middleware.RequestID,
middleware.Cache,
middleware.Cors,
middleware.Secure,
middleware.Version,
).Then(
handler,
),
}, nil
}