2020-01-30 15:55:40 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2022-01-07 13:19:39 +01:00
|
|
|
"fmt"
|
2020-05-08 19:08:39 +02:00
|
|
|
|
2020-01-30 15:55:40 +01:00
|
|
|
"github.com/oklog/run"
|
2022-04-12 09:46:55 +02:00
|
|
|
"github.com/owncloud/ocis/extensions/accounts/pkg/config"
|
|
|
|
|
"github.com/owncloud/ocis/extensions/accounts/pkg/config/parser"
|
|
|
|
|
"github.com/owncloud/ocis/extensions/accounts/pkg/logging"
|
|
|
|
|
"github.com/owncloud/ocis/extensions/accounts/pkg/metrics"
|
|
|
|
|
"github.com/owncloud/ocis/extensions/accounts/pkg/server/debug"
|
|
|
|
|
"github.com/owncloud/ocis/extensions/accounts/pkg/server/grpc"
|
|
|
|
|
"github.com/owncloud/ocis/extensions/accounts/pkg/server/http"
|
|
|
|
|
svc "github.com/owncloud/ocis/extensions/accounts/pkg/service/v0"
|
|
|
|
|
"github.com/owncloud/ocis/extensions/accounts/pkg/tracing"
|
2022-01-03 16:24:39 +01:00
|
|
|
"github.com/owncloud/ocis/ocis-pkg/version"
|
2021-09-14 16:05:03 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
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{
|
2022-01-07 13:19:39 +01:00
|
|
|
Name: "server",
|
|
|
|
|
Usage: fmt.Sprintf("start %s extension without runtime (unsupervised mode)", cfg.Service.Name),
|
|
|
|
|
Category: "server",
|
2022-01-03 19:08:11 +01:00
|
|
|
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
|
2020-02-12 14:11:25 +01:00
|
|
|
},
|
2020-01-30 15:55:40 +01:00
|
|
|
Action: func(c *cli.Context) error {
|
2021-12-16 18:53:06 +01:00
|
|
|
logger := logging.Configure(cfg.Service.Name, cfg.Log)
|
2021-08-09 18:03:01 +02:00
|
|
|
err := tracing.Configure(cfg)
|
2021-03-18 10:31:30 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2020-12-04 14:28:59 +01:00
|
|
|
}
|
2021-03-18 10:31:30 +01:00
|
|
|
gr := run.Group{}
|
|
|
|
|
ctx, cancel := defineContext(cfg)
|
|
|
|
|
mtrcs := metrics.New()
|
2020-01-30 15:55:40 +01:00
|
|
|
|
2020-06-30 12:56:46 +02:00
|
|
|
defer cancel()
|
|
|
|
|
|
2022-01-03 16:24:39 +01:00
|
|
|
mtrcs.BuildInfo.WithLabelValues(version.String).Set(1)
|
2020-09-25 13:06:26 +02:00
|
|
|
|
2020-09-11 12:47:43 +02:00
|
|
|
handler, err := svc.New(svc.Logger(logger), svc.Config(cfg))
|
|
|
|
|
if err != nil {
|
2021-03-04 11:51:36 +01:00
|
|
|
logger.Error().Err(err).Msg("handler init")
|
|
|
|
|
return err
|
2020-09-11 12:47:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 10:31:30 +01:00
|
|
|
httpServer := http.Server(
|
|
|
|
|
http.Config(cfg),
|
|
|
|
|
http.Logger(logger),
|
2021-11-24 18:30:20 +01:00
|
|
|
http.Name(cfg.Service.Name),
|
2021-03-18 10:31:30 +01:00
|
|
|
http.Context(ctx),
|
|
|
|
|
http.Metrics(mtrcs),
|
|
|
|
|
http.Handler(handler),
|
|
|
|
|
)
|
2020-06-30 12:56:46 +02:00
|
|
|
|
2021-03-18 10:31:30 +01:00
|
|
|
gr.Add(httpServer.Run, func(_ error) {
|
|
|
|
|
logger.Info().Str("server", "http").Msg("shutting down server")
|
|
|
|
|
cancel()
|
|
|
|
|
})
|
2020-06-30 12:56:46 +02:00
|
|
|
|
2021-03-18 10:31:30 +01:00
|
|
|
grpcServer := grpc.Server(
|
|
|
|
|
grpc.Config(cfg),
|
|
|
|
|
grpc.Logger(logger),
|
2021-11-24 18:30:20 +01:00
|
|
|
grpc.Name(cfg.Service.Name),
|
2021-03-18 10:31:30 +01:00
|
|
|
grpc.Context(ctx),
|
|
|
|
|
grpc.Metrics(mtrcs),
|
|
|
|
|
grpc.Handler(handler),
|
|
|
|
|
)
|
2020-06-30 12:56:46 +02:00
|
|
|
|
2021-03-18 10:31:30 +01:00
|
|
|
gr.Add(grpcServer.Run, func(_ error) {
|
|
|
|
|
logger.Info().Str("server", "grpc").Msg("shutting down server")
|
|
|
|
|
cancel()
|
|
|
|
|
})
|
2020-06-30 12:56:46 +02:00
|
|
|
|
2022-01-03 11:10:33 +01:00
|
|
|
// 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()
|
|
|
|
|
})
|
|
|
|
|
|
2020-01-30 15:55:40 +01:00
|
|
|
return gr.Run()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-18 10:31:30 +01:00
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
}()
|
|
|
|
|
}
|