Files
ocis/pkg/command/server.go
T

96 lines
2.5 KiB
Go
Raw Normal View History

2020-01-30 15:55:40 +01:00
package command
import (
"context"
"fmt"
"syscall"
2020-02-04 10:09:38 +01:00
"github.com/micro/cli/v2"
2020-01-30 15:55:40 +01:00
"github.com/oklog/run"
2020-02-04 10:16:12 +01:00
"github.com/owncloud/ocis-accounts/pkg/config"
2020-01-30 15:55:40 +01:00
"github.com/owncloud/ocis-accounts/pkg/micro/grpc"
2020-02-12 14:11:25 +01:00
oclog "github.com/owncloud/ocis-pkg/v2/log"
)
var (
logger oclog.Logger
2020-01-30 15:55:40 +01:00
)
// Server is the entry point for the server command.
2020-02-04 10:16:12 +01:00
func Server(cfg *config.Config) *cli.Command {
2020-02-04 10:09:38 +01:00
return &cli.Command{
2020-02-07 09:54:43 +01:00
Name: "server",
Usage: "Start ocis accounts service",
Description: "an accounts backend manager (driver) needs to be specified. By default the service uses the filesystem as storage",
2020-02-04 11:54:45 +01:00
Flags: []cli.Flag{
&cli.StringFlag{
Name: "manager",
DefaultText: "filesystem",
2020-02-07 09:54:43 +01:00
Usage: "accounts backend manager",
2020-02-04 11:54:45 +01:00
Value: "filesystem",
EnvVars: []string{"OCIS_ACCOUNTS_MANAGER"},
Destination: &cfg.Manager,
},
&cli.StringFlag{
Name: "mount-path",
2020-02-07 09:54:43 +01:00
Usage: "mounting point (necessary when manager=filesystem)",
2020-02-04 11:54:45 +01:00
EnvVars: []string{"OCIS_ACCOUNTS_MOUNT_PATH"},
Destination: &cfg.MountPath,
},
2020-02-04 12:42:16 +01:00
&cli.StringFlag{
Name: "name",
Value: "accounts",
2020-02-07 09:54:43 +01:00
DefaultText: "accounts",
Usage: "service name",
2020-02-04 12:42:20 +01:00
EnvVars: []string{"OCIS_ACCOUNTS_NAME"},
2020-02-07 09:54:43 +01:00
Destination: &cfg.Server.Name,
2020-02-04 12:42:16 +01:00
},
&cli.StringFlag{
Name: "namespace",
2020-02-07 09:54:43 +01:00
Aliases: []string{"ns"},
2020-02-04 12:42:16 +01:00
Value: "com.owncloud",
2020-02-07 09:54:43 +01:00
DefaultText: "com.owncloud",
Usage: "namespace",
2020-02-04 12:42:20 +01:00
EnvVars: []string{"OCIS_ACCOUNTS_NAMESPACE"},
2020-02-07 09:54:43 +01:00
Destination: &cfg.Server.Namespace,
2020-02-04 12:42:16 +01:00
},
&cli.StringFlag{
Name: "address",
2020-02-07 09:54:43 +01:00
Aliases: []string{"addr"},
2020-02-07 14:08:11 +01:00
Value: "localhost:9180",
DefaultText: "localhost:9180",
2020-02-07 09:54:43 +01:00
Usage: "service endpoint",
2020-02-04 12:42:20 +01:00
EnvVars: []string{"OCIS_ACCOUNTS_ADDRESS"},
2020-02-07 09:54:43 +01:00
Destination: &cfg.Server.Address,
2020-02-04 12:42:16 +01:00
},
2020-02-04 11:54:45 +01:00
},
2020-02-12 14:11:25 +01:00
Before: func(c *cli.Context) error {
logger = oclog.NewLogger(oclog.Name(cfg.Server.Name))
return ParseConfig(c, cfg)
2020-02-12 14:11:25 +01:00
},
2020-01-30 15:55:40 +01:00
Action: func(c *cli.Context) error {
gr := run.Group{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2020-02-04 12:37:23 +01:00
service := grpc.NewService(
2020-02-12 14:11:25 +01:00
grpc.Logger(logger),
2020-02-04 12:37:23 +01:00
grpc.Context(ctx),
grpc.Config(cfg),
2020-02-04 12:42:16 +01:00
grpc.Name(cfg.Server.Name),
grpc.Namespace(cfg.Server.Namespace),
grpc.Address(cfg.Server.Address),
2020-02-04 12:37:23 +01:00
)
2020-01-30 15:55:40 +01:00
gr.Add(func() error {
return service.Run()
}, func(_ error) {
fmt.Println("shutting down grpc server")
cancel()
})
run.SignalHandler(ctx, syscall.SIGKILL)
return gr.Run()
},
}
}