init
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package http
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/micro/go-micro/util/log"
|
||||
"github.com/micro/go-micro/web"
|
||||
"github.com/owncloud/ocis-graph/pkg/config"
|
||||
"github.com/owncloud/ocis-graph/pkg/flagset"
|
||||
"github.com/owncloud/ocis-graph/pkg/version"
|
||||
)
|
||||
|
||||
func Server(opts ...Option) (web.Service, error) {
|
||||
options := newOptions(opts...)
|
||||
log.Infof("Server [http] listening on [%s]", options.Config.HTTP.Addr)
|
||||
|
||||
// &cli.StringFlag{
|
||||
// Name: "http-addr",
|
||||
// Value: "0.0.0.0:8380",
|
||||
// Usage: "Address to bind http server",
|
||||
// EnvVar: "GRAPH_HTTP_ADDR",
|
||||
// Destination: &cfg.HTTP.Addr,
|
||||
// },
|
||||
|
||||
service := web.NewService(
|
||||
web.Name("go.micro.web.graph"),
|
||||
web.Version(version.String),
|
||||
web.RegisterTTL(time.Second*30),
|
||||
web.RegisterInterval(time.Second*10),
|
||||
web.Context(options.Context),
|
||||
web.Flags(append(
|
||||
flagset.RootWithConfig(config.New()),
|
||||
flagset.ServerWithConfig(config.New())...,
|
||||
)...),
|
||||
)
|
||||
|
||||
service.Init()
|
||||
return service, nil
|
||||
}
|
||||
Reference in New Issue
Block a user