Restructure based on hello reference

This commit is contained in:
Thomas Boerger committed 2019-12-06 13:18:26 +01:00
1 parent ac202d5458
commit dfa78aff7c
53 files changed
+780 -3047

No files matched your search

+19 -5
View File
@@ -4,8 +4,20 @@ import (
"context"
"github.com/owncloud/ocis-graph/pkg/config"
"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
Context context.Context
Config *config.Config
}
// newOptions initializes the available default options.
func newOptions(opts ...Option) Options {
opt := Options{}
@@ -16,19 +28,21 @@ func newOptions(opts ...Option) Options {
return opt
}
type Option func(o *Options)
type Options struct {
Context context.Context
Config *config.Config
// Logger provides a function to set the logger option.
func Logger(val log.Logger) Option {
return func(o *Options) {
o.Logger = val
}
}
// Context provides a function to set the context option.
func Context(val context.Context) Option {
return func(o *Options) {
o.Context = val
}
}
// Config provides a function to set the config option.
func Config(val *config.Config) Option {
return func(o *Options) {
o.Config = val
+39 -64
View File
@@ -3,74 +3,49 @@ 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"
"github.com/owncloud/ocis-graph/pkg/config"
"github.com/owncloud/ocis-graph/pkg/version"
"github.com/owncloud/ocis-pkg/service/debug"
)
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))
}
// Server initializes the debug service and server.
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
return debug.NewService(
debug.Logger(options.Logger),
debug.Name("graph"),
debug.Version(version.String),
debug.Address(options.Config.Debug.Addr),
debug.Token(options.Config.Debug.Token),
debug.Pprof(options.Config.Debug.Pprof),
debug.Zpages(options.Config.Debug.Zpages),
debug.Health(health(options.Config)),
debug.Ready(ready(options.Config)),
), nil
}
// health implements the health check.
func health(cfg *config.Config) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
// TODO(tboerger): check if services are up and running
io.WriteString(w, http.StatusText(http.StatusOK))
}
}
// ready implements the ready check.
func ready(cfg *config.Config) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
// TODO(tboerger): check if services are up and running
io.WriteString(w, http.StatusText(http.StatusOK))
}
}