mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-01 01:41:13 -06:00
107 lines
2.9 KiB
Go
107 lines
2.9 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/oklog/run"
|
|
"github.com/owncloud/ocis/accounts/pkg/config"
|
|
"github.com/owncloud/ocis/accounts/pkg/config/parser"
|
|
"github.com/owncloud/ocis/accounts/pkg/logging"
|
|
"github.com/owncloud/ocis/accounts/pkg/metrics"
|
|
"github.com/owncloud/ocis/accounts/pkg/server/debug"
|
|
"github.com/owncloud/ocis/accounts/pkg/server/grpc"
|
|
"github.com/owncloud/ocis/accounts/pkg/server/http"
|
|
svc "github.com/owncloud/ocis/accounts/pkg/service/v0"
|
|
"github.com/owncloud/ocis/accounts/pkg/tracing"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
// Server is the entry point for the server command.
|
|
func Server(cfg *config.Config) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "server",
|
|
Usage: "Start ocis accounts service",
|
|
Description: "uses an LDAP server as the storage backend",
|
|
Before: func(ctx *cli.Context) error {
|
|
if err := parser.ParseConfig(cfg); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
},
|
|
Action: func(c *cli.Context) error {
|
|
logger := logging.Configure(cfg.Service.Name, cfg.Log)
|
|
err := tracing.Configure(cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
gr := run.Group{}
|
|
ctx, cancel := defineContext(cfg)
|
|
mtrcs := metrics.New()
|
|
|
|
defer cancel()
|
|
|
|
mtrcs.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1)
|
|
|
|
handler, err := svc.New(svc.Logger(logger), svc.Config(cfg))
|
|
if err != nil {
|
|
logger.Error().Err(err).Msg("handler init")
|
|
return err
|
|
}
|
|
|
|
httpServer := http.Server(
|
|
http.Config(cfg),
|
|
http.Logger(logger),
|
|
http.Name(cfg.Service.Name),
|
|
http.Context(ctx),
|
|
http.Metrics(mtrcs),
|
|
http.Handler(handler),
|
|
)
|
|
|
|
gr.Add(httpServer.Run, func(_ error) {
|
|
logger.Info().Str("server", "http").Msg("shutting down server")
|
|
cancel()
|
|
})
|
|
|
|
grpcServer := grpc.Server(
|
|
grpc.Config(cfg),
|
|
grpc.Logger(logger),
|
|
grpc.Name(cfg.Service.Name),
|
|
grpc.Context(ctx),
|
|
grpc.Metrics(mtrcs),
|
|
grpc.Handler(handler),
|
|
)
|
|
|
|
gr.Add(grpcServer.Run, func(_ error) {
|
|
logger.Info().Str("server", "grpc").Msg("shutting down server")
|
|
cancel()
|
|
})
|
|
|
|
// prepare a debug server and add it to the group run.
|
|
debugServer, err := debug.Server(debug.Logger(logger), debug.Context(ctx), debug.Config(cfg))
|
|
if err != nil {
|
|
logger.Error().Err(err).Str("server", "debug").Msg("Failed to initialize server")
|
|
return err
|
|
}
|
|
|
|
gr.Add(debugServer.ListenAndServe, func(_ error) {
|
|
_ = debugServer.Shutdown(ctx)
|
|
cancel()
|
|
})
|
|
|
|
return gr.Run()
|
|
},
|
|
}
|
|
}
|
|
|
|
// defineContext sets the context for the extension. If there is a context configured it will create a new child from it,
|
|
// if not, it will create a root context that can be cancelled.
|
|
func defineContext(cfg *config.Config) (context.Context, context.CancelFunc) {
|
|
return func() (context.Context, context.CancelFunc) {
|
|
if cfg.Context == nil {
|
|
return context.WithCancel(context.Background())
|
|
}
|
|
return context.WithCancel(cfg.Context)
|
|
}()
|
|
}
|