From 8aed339867df376c0464b787393fee39ba66eb7c Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Thu, 4 Mar 2021 14:37:37 +0100 Subject: [PATCH] add webdav --- ocis/pkg/runtime/runtime.go | 4 +++- webdav/cmd/webdav/main.go | 3 ++- webdav/pkg/command/root.go | 37 ++++++++++++++++++++++------ webdav/pkg/command/server.go | 2 -- webdav/pkg/config/config.go | 12 ++++++---- webdav/pkg/flagset/flagset.go | 45 ++++++++++++++--------------------- 6 files changed, 61 insertions(+), 42 deletions(-) diff --git a/ocis/pkg/runtime/runtime.go b/ocis/pkg/runtime/runtime.go index 9443927298..fb043d3f76 100644 --- a/ocis/pkg/runtime/runtime.go +++ b/ocis/pkg/runtime/runtime.go @@ -15,6 +15,7 @@ import ( store "github.com/owncloud/ocis/store/pkg/command" thumbnails "github.com/owncloud/ocis/thumbnails/pkg/command" web "github.com/owncloud/ocis/web/pkg/command" + webdav "github.com/owncloud/ocis/webdav/pkg/command" "github.com/thejerf/suture" @@ -58,7 +59,7 @@ var ( "storage-users", "storage-public-link", "thumbnails", // done - "web", + "web", // done "webdav", } @@ -130,6 +131,7 @@ func (r *Runtime) Start() error { addServiceToken("store", supervisor.Add(store.NewSutureService(globalCtx, r.c.Store))) addServiceToken("thumbnails", supervisor.Add(thumbnails.NewSutureService(globalCtx, r.c.Thumbnails))) addServiceToken("web", supervisor.Add(web.NewSutureService(globalCtx, r.c.Web))) + addServiceToken("webdav", supervisor.Add(webdav.NewSutureService(globalCtx, r.c.WebDAV))) // TODO(refs) debug line with supervised services. go supervisor.ServeBackground() diff --git a/webdav/cmd/webdav/main.go b/webdav/cmd/webdav/main.go index 8131ea901f..fe242ff533 100644 --- a/webdav/cmd/webdav/main.go +++ b/webdav/cmd/webdav/main.go @@ -4,10 +4,11 @@ import ( "os" "github.com/owncloud/ocis/webdav/pkg/command" + "github.com/owncloud/ocis/webdav/pkg/config" ) func main() { - if err := command.Execute(); err != nil { + if err := command.Execute(config.New()); err != nil { os.Exit(1) } } diff --git a/webdav/pkg/command/root.go b/webdav/pkg/command/root.go index 0fb492927a..31d2162648 100644 --- a/webdav/pkg/command/root.go +++ b/webdav/pkg/command/root.go @@ -1,21 +1,19 @@ package command import ( + "context" "os" "strings" "github.com/micro/cli/v2" "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/webdav/pkg/config" - "github.com/owncloud/ocis/webdav/pkg/flagset" "github.com/owncloud/ocis/webdav/pkg/version" "github.com/spf13/viper" ) // Execute is the entry point for the ocis-webdav command. -func Execute() error { - cfg := config.New() - +func Execute(cfg *config.Config) error { app := &cli.App{ Name: "webdav", Version: version.String, @@ -28,9 +26,6 @@ func Execute() error { Email: "support@owncloud.com", }, }, - - Flags: flagset.RootWithConfig(cfg), - Before: func(c *cli.Context) error { cfg.Service.Version = version.String return nil @@ -107,3 +102,31 @@ func ParseConfig(c *cli.Context, cfg *config.Config) error { return nil } + +// SutureService allows for the webdav command to be embedded and supervised by a suture supervisor tree. +type SutureService struct { + ctx context.Context + cancel context.CancelFunc // used to cancel the context go-micro services used to shutdown a service. + cfg *config.Config +} + +// NewSutureService creates a new webdav.SutureService +func NewSutureService(ctx context.Context, cfg *config.Config) SutureService { + sctx, cancel := context.WithCancel(ctx) + cfg.Context = sctx // propagate the context down to the go-micro services. + return SutureService{ + ctx: sctx, + cancel: cancel, + cfg: cfg, + } +} + +func (s SutureService) Serve() { + if err := Execute(s.cfg); err != nil { + return + } +} + +func (s SutureService) Stop() { + s.cancel() +} diff --git a/webdav/pkg/command/server.go b/webdav/pkg/command/server.go index cd3177abf6..3bd86262b9 100644 --- a/webdav/pkg/command/server.go +++ b/webdav/pkg/command/server.go @@ -141,8 +141,6 @@ func Server(cfg *config.Config) *cli.Command { http.Context(ctx), http.Config(cfg), http.Metrics(metrics), - http.Flags(flagset.RootWithConfig(config.New())), - http.Flags(flagset.ServerWithConfig(config.New())), ) if err != nil { diff --git a/webdav/pkg/config/config.go b/webdav/pkg/config/config.go index 9667edc5a8..26e901b294 100644 --- a/webdav/pkg/config/config.go +++ b/webdav/pkg/config/config.go @@ -1,5 +1,7 @@ package config +import "context" + // Log defines the available logging configuration. type Log struct { Level string @@ -17,15 +19,15 @@ type Debug struct { // HTTP defines the available http configuration. type HTTP struct { - Addr string - Root string + Addr string + Root string } // Service defines the available service configuration. type Service struct { - Name string + Name string Namespace string - Version string + Version string } // Tracing defines the available tracing configuration. @@ -45,6 +47,8 @@ type Config struct { HTTP HTTP Tracing Tracing Service Service + + Context context.Context } // New initializes a new configuration with or without defaults. diff --git a/webdav/pkg/flagset/flagset.go b/webdav/pkg/flagset/flagset.go index 6195b274ce..03d73e3214 100644 --- a/webdav/pkg/flagset/flagset.go +++ b/webdav/pkg/flagset/flagset.go @@ -5,33 +5,6 @@ import ( "github.com/owncloud/ocis/webdav/pkg/config" ) -// RootWithConfig applies cfg to the root flagset -func RootWithConfig(cfg *config.Config) []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: "log-level", - Value: "info", - Usage: "Set logging level", - EnvVars: []string{"WEBDAV_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - &cli.BoolFlag{ - Value: true, - Name: "log-pretty", - Usage: "Enable pretty logging", - EnvVars: []string{"WEBDAV_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - &cli.BoolFlag{ - Value: true, - Name: "log-color", - Usage: "Enable colored logging", - EnvVars: []string{"WEBDAV_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - } -} - // HealthWithConfig applies cfg to the root flagset func HealthWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ @@ -48,6 +21,24 @@ func HealthWithConfig(cfg *config.Config) []cli.Flag { // ServerWithConfig applies cfg to the root flagset func ServerWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{ + &cli.StringFlag{ + Name: "log-level", + Usage: "Set logging level", + EnvVars: []string{"WEBDAV_LOG_LEVEL", "OCIS_LOG_LEVEL"}, + Destination: &cfg.Log.Level, + }, + &cli.BoolFlag{ + Name: "log-pretty", + Usage: "Enable pretty logging", + EnvVars: []string{"WEBDAV_LOG_PRETTY", "OCIS_LOG_PRETTY"}, + Destination: &cfg.Log.Pretty, + }, + &cli.BoolFlag{ + Name: "log-color", + Usage: "Enable colored logging", + EnvVars: []string{"WEBDAV_LOG_COLOR", "OCIS_LOG_COLOR"}, + Destination: &cfg.Log.Color, + }, &cli.StringFlag{ Name: "config-file", Value: "",