diff --git a/ocis/pkg/runtime/runtime.go b/ocis/pkg/runtime/runtime.go index f85e8cc17e..f9f816871b 100644 --- a/ocis/pkg/runtime/runtime.go +++ b/ocis/pkg/runtime/runtime.go @@ -127,6 +127,7 @@ func (r *Runtime) Start() error { 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))) + addServiceToken("frontend", supervisor.Add(storage.NewFrontend(globalCtx, r.c.Storage))) // TODO(refs) debug line with supervised services. go supervisor.ServeBackground() diff --git a/storage/pkg/command/frontend.go b/storage/pkg/command/frontend.go index c657e85aa7..b07634ffed 100644 --- a/storage/pkg/command/frontend.go +++ b/storage/pkg/command/frontend.go @@ -2,6 +2,7 @@ package command import ( "context" + "flag" "fmt" "os" "os/signal" @@ -326,3 +327,43 @@ func loadUserAgent(c *cli.Context, cfg *config.Config) error { return nil } + +// FrontendSutureService allows for the storage-frontend command to be embedded and supervised by a suture supervisor tree. +type FrontendSutureService struct { + ctx context.Context + cancel context.CancelFunc // used to cancel the context go-micro services used to shutdown a service. + cfg *config.Config +} + +// NewFrontendSutureService creates a new frontend.FrontendSutureService +func NewFrontend(ctx context.Context, cfg *config.Config) FrontendSutureService { + sctx, cancel := context.WithCancel(ctx) + cfg.Context = sctx + return FrontendSutureService{ + ctx: sctx, + cancel: cancel, + cfg: cfg, + } +} + +func (s FrontendSutureService) Serve() { + f := &flag.FlagSet{} + for k := range Frontend(s.cfg).Flags { + if err := Frontend(s.cfg).Flags[k].Apply(f); err != nil { + return + } + } + ctx := cli.NewContext(nil, f, nil) + if Frontend(s.cfg).Before != nil { + if err := Frontend(s.cfg).Before(ctx); err != nil { + return + } + } + if err := Frontend(s.cfg).Action(ctx); err != nil { + return + } +} + +func (s FrontendSutureService) Stop() { + s.cancel() +}