Files
ocis/services/search/pkg/command/server.go
T

88 lines
2.1 KiB
Go
Raw Normal View History

2022-04-08 10:49:29 +02:00
package command
import (
"context"
"fmt"
"os"
2022-04-08 10:49:29 +02:00
"github.com/oklog/run"
2022-05-04 14:49:59 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/version"
2022-06-20 16:49:06 +02:00
"github.com/owncloud/ocis/v2/services/search/pkg/config"
"github.com/owncloud/ocis/v2/services/search/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/search/pkg/logging"
"github.com/owncloud/ocis/v2/services/search/pkg/metrics"
"github.com/owncloud/ocis/v2/services/search/pkg/server/debug"
"github.com/owncloud/ocis/v2/services/search/pkg/server/grpc"
"github.com/owncloud/ocis/v2/services/search/pkg/tracing"
2022-04-08 10:49:29 +02: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-05-02 14:19:08 +00:00
err := parser.ParseConfig(cfg)
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
2022-05-02 14:19:08 +00:00
}
return err
2022-04-08 10:49:29 +02:00
},
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 := func() (context.Context, context.CancelFunc) {
if cfg.Context == nil {
return context.WithCancel(context.Background())
}
return context.WithCancel(cfg.Context)
}()
defer cancel()
2022-04-13 15:22:02 +00:00
mtrcs := metrics.New()
2022-06-13 10:41:14 +02:00
mtrcs.BuildInfo.WithLabelValues(version.GetString()).Set(1)
2022-04-08 10:49:29 +02:00
grpcServer := grpc.Server(
grpc.Config(cfg),
grpc.Logger(logger),
grpc.Name(cfg.Service.Name),
grpc.Context(ctx),
grpc.Metrics(mtrcs),
)
2022-04-08 10:49:29 +02:00
gr.Add(grpcServer.Run, func(_ error) {
2022-07-05 08:41:21 +02:00
logger.Error().
Err(err).
Str("server", "grpc").
Msg("shutting down server")
cancel()
})
2022-04-08 10:49:29 +02:00
server, err := debug.Server(
debug.Logger(logger),
debug.Context(ctx),
debug.Config(cfg),
)
2022-04-08 10:49:29 +02:00
if err != nil {
logger.Info().Err(err).Str("transport", "debug").Msg("Failed to initialize server")
return err
2022-04-08 10:49:29 +02:00
}
gr.Add(server.ListenAndServe, func(_ error) {
_ = server.Shutdown(ctx)
cancel()
})
2022-04-08 10:49:29 +02:00
return gr.Run()
},
}
}