Files

89 lines
2.6 KiB
Go
Raw Permalink Normal View History

2019-12-04 17:15:11 +01:00
package command
import (
"context"
2022-01-07 13:19:39 +01:00
"fmt"
2024-05-02 14:10:32 +02:00
"os/signal"
2019-12-04 17:15:11 +01:00
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
2024-05-02 14:10:32 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/runner"
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
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/graph/pkg/config"
"github.com/owncloud/ocis/v2/services/graph/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/graph/pkg/logging"
"github.com/owncloud/ocis/v2/services/graph/pkg/metrics"
"github.com/owncloud/ocis/v2/services/graph/pkg/server/debug"
"github.com/owncloud/ocis/v2/services/graph/pkg/server/http"
"github.com/urfave/cli/v2"
2019-12-04 17:15:11 +01:00
)
// Server is the entrypoint for the server command.
2020-02-10 12:06:52 +01:00
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
2022-01-07 13:19:39 +01:00
Name: "server",
2022-08-18 10:59:35 +02:00
Usage: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name),
2022-01-07 13:19:39 +01:00
Category: "server",
Before: func(c *cli.Context) error {
2022-08-22 08:08:18 +02:00
return configlog.ReturnFatal(parser.ParseConfig(cfg))
2019-12-09 14:47:38 +01:00
},
2019-12-04 17:15:11 +01:00
Action: func(c *cli.Context) error {
2021-12-16 19:40:23 +01:00
logger := logging.Configure(cfg.Service.Name, cfg.Log)
traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name)
if err != nil {
return err
}
2019-12-04 17:15:11 +01:00
2024-05-02 14:10:32 +02:00
var cancel context.CancelFunc
if cfg.Context == nil {
cfg.Context, cancel = signal.NotifyContext(context.Background(), runner.StopSignals...)
2024-05-02 14:10:32 +02:00
defer cancel()
}
ctx := cfg.Context
2019-12-06 13:14:21 +01:00
2024-05-02 14:10:32 +02:00
mtrcs := metrics.New()
2022-06-13 10:41:14 +02:00
mtrcs.BuildInfo.WithLabelValues(version.GetString()).Set(1)
2021-03-15 15:17:29 +00:00
2024-05-02 14:10:32 +02:00
gr := runner.NewGroup()
2019-12-04 17:15:11 +01:00
{
server, err := http.Server(
2019-12-06 13:14:21 +01:00
http.Logger(logger),
2019-12-04 17:15:11 +01:00
http.Context(ctx),
http.Config(cfg),
2021-03-15 15:17:29 +00:00
http.Metrics(mtrcs),
http.TraceProvider(traceProvider),
2019-12-04 17:15:11 +01:00
)
if err != nil {
logger.Error().Err(err).Str("transport", "http").Msg("Failed to initialize server")
2019-12-04 17:15:11 +01:00
return err
}
2025-05-27 13:02:34 +02:00
gr.Add(runner.NewGoMicroHttpServerRunner(cfg.Service.Name+".http", server))
2019-12-04 17:15:11 +01:00
}
{
server, err := debug.Server(
2019-12-06 13:14:21 +01:00
debug.Logger(logger),
2019-12-04 17:15:11 +01:00
debug.Context(ctx),
debug.Config(cfg),
)
if err != nil {
2021-03-15 15:17:29 +00:00
logger.Info().Err(err).Str("transport", "debug").Msg("Failed to initialize server")
2019-12-04 17:15:11 +01:00
return err
}
2025-05-27 13:02:34 +02:00
gr.Add(runner.NewGolangHttpServerRunner(cfg.Service.Name+".debug", server))
2019-12-04 17:15:11 +01:00
}
2025-06-10 18:21:49 +02:00
logger.Warn().Msgf("starting service %s", cfg.Service.Name)
2024-05-02 14:10:32 +02:00
grResults := gr.Run(ctx)
2025-06-10 18:21:49 +02:00
if err := runner.ProcessResults(grResults); err != nil {
logger.Error().Err(err).Msgf("service %s stopped with error", cfg.Service.Name)
return err
2024-05-02 14:10:32 +02:00
}
2025-06-10 18:21:49 +02:00
logger.Warn().Msgf("service %s stopped without error", cfg.Service.Name)
2024-05-02 14:10:32 +02:00
return nil
2019-12-04 17:15:11 +01:00
},
}
}