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
+42
View File
@@ -0,0 +1,42 @@
package command
import (
"fmt"
"net/http"
"github.com/micro/cli"
"github.com/micro/go-micro/util/log"
"github.com/owncloud/ocis-graph/pkg/config"
"github.com/owncloud/ocis-graph/pkg/flagset"
)
// Health is the entrypoint for the health command.
func Health(cfg *config.Config) cli.Command {
return cli.Command{
Name: "health",
Usage: "Check health status",
Flags: flagset.HealthWithConfig(cfg),
Action: func(c *cli.Context) error {
resp, err := http.Get(
fmt.Sprintf(
"http://%s/healthz",
cfg.Debug.Addr,
),
)
if err != nil {
log.Fatalf("Failed to request health check: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("Health check responds with [%d]", resp.StatusCode)
}
log.Debugf("Health got good state with [%d]", resp.StatusCode)
return nil
},
}
}
+107
View File
@@ -0,0 +1,107 @@
package command
import (
"os"
"strings"
"github.com/micro/cli"
"github.com/micro/go-micro/util/log"
"github.com/owncloud/ocis-graph/pkg/config"
"github.com/owncloud/ocis-graph/pkg/flagset"
"github.com/owncloud/ocis-graph/pkg/version"
"github.com/spf13/viper"
)
// Execute is the entry point for the ocis-graph command.
func Execute() error {
cfg := config.New()
app := &cli.App{
Name: "ocis-graph",
Version: version.String,
Usage: "Example service for Reva/oCIS",
Compiled: version.Compiled(),
Authors: []cli.Author{
{
Name: "ownCloud GmbH",
Email: "support@owncloud.com",
},
},
Flags: flagset.RootWithConfig(cfg),
Before: func(c *cli.Context) error {
NewLogger(cfg)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("graph")
viper.AutomaticEnv()
if c.IsSet("config-file") {
viper.SetConfigFile(c.String("config-file"))
} else {
viper.SetConfigName("graph")
viper.AddConfigPath("/etc/ocis")
viper.AddConfigPath("$HOME/.ocis")
viper.AddConfigPath("./config")
}
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
log.Info("Continue without config")
case viper.UnsupportedConfigError:
log.Fatalf("Unsupported config type: %w", err)
default:
log.Fatalf("Failed to read config: %w", err)
}
}
if err := viper.Unmarshal(&cfg); err != nil {
log.Fatalf("Failed to parse config: %w", err)
}
return nil
},
Commands: []cli.Command{
Server(cfg),
Health(cfg),
},
}
cli.HelpFlag = &cli.BoolFlag{
Name: "help,h",
Usage: "Show the help",
}
cli.VersionFlag = &cli.BoolFlag{
Name: "version,v",
Usage: "Print the version",
}
return app.Run(os.Args)
}
func NewLogger(cfg *config.Config) {
switch strings.ToLower(cfg.Log.Level) {
case "fatal":
log.SetLevel(log.LevelFatal)
case "error":
log.SetLevel(log.LevelError)
case "info":
log.SetLevel(log.LevelInfo)
case "warn":
log.SetLevel(log.LevelWarn)
case "debug":
log.SetLevel(log.LevelDebug)
case "trace":
log.SetLevel(log.LevelTrace)
default:
log.SetLevel(log.LevelInfo)
}
log.Name("graph")
}
+185
View File
@@ -0,0 +1,185 @@
package command
import (
"context"
"os"
"os/signal"
"time"
"contrib.go.opencensus.io/exporter/jaeger"
"contrib.go.opencensus.io/exporter/ocagent"
"contrib.go.opencensus.io/exporter/zipkin"
"github.com/micro/cli"
"github.com/micro/go-micro/util/log"
"github.com/oklog/run"
openzipkin "github.com/openzipkin/zipkin-go"
zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
"github.com/owncloud/ocis-graph/pkg/config"
"github.com/owncloud/ocis-graph/pkg/flagset"
"github.com/owncloud/ocis-graph/pkg/server/debug"
"github.com/owncloud/ocis-graph/pkg/server/http"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
)
// Server is the entrypoint for the server command.
func Server(cfg *config.Config) cli.Command {
return cli.Command{
Name: "server",
Usage: "Start integrated server",
Flags: flagset.ServerWithConfig(cfg),
Action: func(c *cli.Context) error {
if cfg.Tracing.Enabled {
switch t := cfg.Tracing.Type; t {
case "agent":
exporter, err := ocagent.NewExporter(
ocagent.WithReconnectionPeriod(5*time.Second),
ocagent.WithAddress(cfg.Tracing.Endpoint),
ocagent.WithServiceName(cfg.Tracing.Service),
)
if err != nil {
log.Error(
"Failed to create agent tracing on [%s] endpoint and [%s] collector: %w",
cfg.Tracing.Endpoint,
cfg.Tracing.Collector,
err,
)
return err
}
trace.RegisterExporter(exporter)
view.RegisterExporter(exporter)
case "jaeger":
exporter, err := jaeger.NewExporter(
jaeger.Options{
AgentEndpoint: cfg.Tracing.Endpoint,
CollectorEndpoint: cfg.Tracing.Collector,
ServiceName: cfg.Tracing.Service,
},
)
if err != nil {
log.Error(
"Failed to create jaeger tracing on [%s] endpoint and [%s] collector: %w",
cfg.Tracing.Endpoint,
cfg.Tracing.Collector,
err,
)
return err
}
trace.RegisterExporter(exporter)
case "zipkin":
endpoint, err := openzipkin.NewEndpoint(
cfg.Tracing.Service,
cfg.Tracing.Endpoint,
)
if err != nil {
log.Error(
"Failed to create zipkin tracing on [%s] endpoint and [%s] collector: %w",
cfg.Tracing.Endpoint,
cfg.Tracing.Collector,
err,
)
return err
}
exporter := zipkin.NewExporter(
zipkinhttp.NewReporter(
cfg.Tracing.Collector,
),
endpoint,
)
trace.RegisterExporter(exporter)
default:
log.Warnf("Unknown tracing backend [%s]", t)
}
trace.ApplyConfig(
trace.Config{
DefaultSampler: trace.AlwaysSample(),
},
)
} else {
log.Debug("Tracing is not enabled")
}
var (
gr = run.Group{}
ctx, cancel = context.WithCancel(context.Background())
)
{
server, err := http.Server(
http.Context(ctx),
http.Config(cfg),
)
if err != nil {
log.Errorf("Server [http] failed to initialize: %w", err)
return err
}
gr.Add(func() error {
return server.Run()
}, func(_ error) {
log.Infof("Server [http] shutting down")
cancel()
})
}
{
server, err := debug.Server(
debug.Context(ctx),
debug.Config(cfg),
)
if err != nil {
log.Errorf("Server [debug] failed to initialize: %w", err)
return err
}
gr.Add(func() error {
return server.ListenAndServe()
}, func(_ error) {
ctx, timeout := context.WithTimeout(ctx, 5*time.Second)
defer timeout()
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Errorf("Server [debug] shutdown failed: %w", err)
} else {
log.Infof("Server [debug] shutting down")
}
})
}
{
stop := make(chan os.Signal, 1)
gr.Add(func() error {
signal.Notify(stop, os.Interrupt)
<-stop
return nil
}, func(err error) {
close(stop)
cancel()
})
}
return gr.Run()
},
}
}