Files
ocis/pkg/command/server.go
T

106 lines
2.4 KiB
Go
Raw Normal View History

2020-01-30 15:55:40 +01:00
package command
import (
"context"
2020-06-30 12:56:46 +02:00
"os"
"os/signal"
"strings"
2020-05-08 19:08:39 +02:00
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-06-30 12:56:46 +02:00
"github.com/owncloud/ocis-accounts/pkg/flagset"
"github.com/owncloud/ocis-accounts/pkg/metrics"
2020-04-14 19:37:57 +02:00
"github.com/owncloud/ocis-accounts/pkg/server/grpc"
2020-06-30 12:56:46 +02:00
"github.com/owncloud/ocis-accounts/pkg/server/http"
svc "github.com/owncloud/ocis-accounts/pkg/service/v0"
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",
2020-05-08 19:08:39 +02:00
Description: "uses an LDAP server as the storage backend",
2020-04-22 15:48:09 +02:00
Flags: flagset.ServerWithConfig(cfg),
2020-06-30 12:56:46 +02:00
Before: func(ctx *cli.Context) error {
if cfg.HTTP.Root != "/" {
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
}
// When running on single binary mode the before hook from the root command won't get called. We manually
// call this before hook from ocis command, so the configuration can be loaded.
return ParseConfig(ctx, cfg)
2020-02-12 14:11:25 +01:00
},
2020-01-30 15:55:40 +01:00
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
2020-01-30 15:55:40 +01:00
2020-06-30 12:56:46 +02:00
var (
gr = run.Group{}
ctx, cancel = context.WithCancel(context.Background())
mtrcs = metrics.New()
2020-02-04 12:37:23 +01:00
)
2020-01-30 15:55:40 +01:00
2020-06-30 12:56:46 +02:00
defer cancel()
{
server := http.Server(
http.Logger(logger),
http.Name(cfg.Server.Name),
http.Context(ctx),
http.Config(cfg),
http.Metrics(mtrcs),
http.Flags(flagset.RootWithConfig(cfg)),
http.Flags(flagset.ServerWithConfig(cfg)),
)
gr.Add(server.Run, func(_ error) {
logger.Info().
Str("server", "http").
Msg("Shutting down server")
cancel()
})
}
{
server := grpc.Server(
grpc.Logger(logger),
grpc.Name(cfg.Server.Name),
grpc.Context(ctx),
grpc.Config(cfg),
grpc.Metrics(mtrcs),
)
gr.Add(func() error {
logger.Info().Str("service", server.Name()).Msg("Reporting settings bundles to settings service")
go svc.RegisterSettingsBundles(&logger)
return server.Run()
}, func(_ error) {
2020-06-18 14:08:41 +02:00
logger.Info().
2020-06-30 12:56:46 +02:00
Str("server", "grpc").
2020-06-18 14:08:41 +02:00
Msg("Shutting down server")
2020-01-30 15:55:40 +01:00
2020-06-30 12:56:46 +02:00
cancel()
})
}
{
stop := make(chan os.Signal, 1)
gr.Add(func() error {
signal.Notify(stop, os.Interrupt)
<-stop
return nil
}, func(err error) {
close(stop)
cancel()
})
}
2020-01-30 15:55:40 +01:00
return gr.Run()
},
}
}