Files
ocis/extensions/nats/pkg/command/server.go
T

74 lines
1.6 KiB
Go
Raw Normal View History

2022-02-11 17:14:48 +01:00
package command
import (
2022-02-17 16:26:37 +01:00
"context"
2022-02-11 17:14:48 +01:00
"fmt"
2022-02-17 16:26:37 +01:00
"github.com/oklog/run"
2022-02-11 17:14:48 +01:00
2022-04-12 10:54:11 +02:00
"github.com/owncloud/ocis/extensions/nats/pkg/config"
"github.com/owncloud/ocis/extensions/nats/pkg/config/parser"
"github.com/owncloud/ocis/extensions/nats/pkg/logging"
"github.com/owncloud/ocis/extensions/nats/pkg/server/nats"
2022-02-11 17:14:48 +01:00
"github.com/urfave/cli/v2"
)
// Server is the entrypoint for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
Usage: fmt.Sprintf("start %s extension without runtime (unsupervised mode)", cfg.Service.Name),
Category: "server",
Before: func(c *cli.Context) error {
2022-04-28 15:52:26 +02:00
err := parser.ParseConfig(cfg)
if err != nil {
fmt.Printf("%v", err)
}
return err
2022-02-11 17:14:48 +01:00
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
2022-02-17 16:26:37 +01:00
gr := run.Group{}
ctx, cancel := func() (context.Context, context.CancelFunc) {
if cfg.Context == nil {
return context.WithCancel(context.Background())
}
2022-02-17 16:26:37 +01:00
return context.WithCancel(cfg.Context)
}()
2022-02-11 17:14:48 +01:00
2022-02-17 16:26:37 +01:00
defer cancel()
2022-02-11 17:14:48 +01:00
2022-02-18 11:44:26 +01:00
natsServer, err := nats.NewNATSServer(
2022-02-18 11:51:56 +01:00
ctx,
2022-02-21 17:01:38 +01:00
logging.NewLogWrapper(logger),
2022-03-24 08:10:07 +01:00
nats.Host(cfg.Nats.Host),
nats.Port(cfg.Nats.Port),
nats.ClusterID(cfg.Nats.ClusterID),
nats.StoreDir(cfg.Nats.StoreDir),
2022-02-18 11:44:26 +01:00
)
if err != nil {
return err
}
2022-02-11 17:14:48 +01:00
2022-02-17 16:26:37 +01:00
gr.Add(func() error {
2022-02-18 11:44:26 +01:00
err := make(chan error)
2022-02-17 16:26:37 +01:00
select {
case <-ctx.Done():
return nil
2022-02-18 11:44:26 +01:00
case err <- natsServer.ListenAndServe():
return <-err
2022-02-17 16:26:37 +01:00
}
}, func(_ error) {
logger.Info().
Msg("Shutting down server")
natsServer.Shutdown()
cancel()
})
return gr.Run()
},
}
2022-02-11 17:14:48 +01:00
}