Files
ocis/graph/pkg/command/server.go
T

107 lines
2.5 KiB
Go
Raw Normal View History

2019-12-04 17:15:11 +01:00
package command
import (
"context"
2019-12-09 14:47:38 +01:00
"strings"
2019-12-04 17:15:11 +01:00
"github.com/oklog/run"
2021-02-04 11:46:32 +01:00
"github.com/owncloud/ocis/graph/pkg/config"
"github.com/owncloud/ocis/graph/pkg/flagset"
"github.com/owncloud/ocis/graph/pkg/metrics"
"github.com/owncloud/ocis/graph/pkg/server/debug"
"github.com/owncloud/ocis/graph/pkg/server/http"
"github.com/owncloud/ocis/graph/pkg/tracing"
2021-03-15 15:17:29 +00:00
"github.com/owncloud/ocis/ocis-pkg/sync"
"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{
2019-12-04 17:15:11 +01:00
Name: "server",
Usage: "Start integrated server",
Flags: flagset.ServerWithConfig(cfg),
2021-03-15 15:17:29 +00:00
Before: func(ctx *cli.Context) error {
logger := NewLogger(cfg)
2019-12-09 14:47:38 +01:00
if cfg.HTTP.Root != "/" {
cfg.HTTP.Root = strings.TrimSuffix(cfg.HTTP.Root, "/")
}
2021-03-15 15:17:29 +00:00
// 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.
if !cfg.Supervised {
return ParseConfig(ctx, cfg)
}
logger.Debug().Str("service", "graph").Msg("ignoring config file parsing when running supervised")
return nil
2019-12-09 14:47:38 +01:00
},
2019-12-04 17:15:11 +01:00
Action: func(c *cli.Context) error {
2019-12-06 13:14:21 +01:00
logger := NewLogger(cfg)
2021-08-09 18:03:01 +02:00
if err := tracing.Configure(cfg); err != nil {
return err
2019-12-04 17:15:11 +01:00
}
gr := run.Group{}
ctx, cancel := func() (context.Context, context.CancelFunc) {
if cfg.Context == nil {
return context.WithCancel(context.Background())
}
return context.WithCancel(cfg.Context)
}()
mtrcs := metrics.New()
2019-12-04 17:15:11 +01:00
2019-12-06 13:14:21 +01:00
defer cancel()
2021-03-15 15:17:29 +00:00
mtrcs.BuildInfo.WithLabelValues(cfg.Server.Version).Set(1)
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),
2019-12-04 17:15:11 +01:00
)
if err != nil {
2021-03-15 15:17:29 +00:00
logger.Info().Err(err).Str("transport", "http").Msg("Failed to initialize server")
2019-12-04 17:15:11 +01:00
return err
}
gr.Add(func() error {
return server.Run()
}, func(_ error) {
2019-12-06 13:14:21 +01:00
logger.Info().
Str("transport", "http").
Msg("Shutting down server")
2019-12-04 17:15:11 +01:00
cancel()
})
}
{
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
}
2021-03-15 15:17:29 +00:00
gr.Add(server.ListenAndServe, func(_ error) {
_ = server.Shutdown(ctx)
cancel()
2019-12-04 17:15:11 +01:00
})
}
2021-03-15 15:17:29 +00:00
if !cfg.Supervised {
sync.Trap(&gr, cancel)
2019-12-04 17:15:11 +01:00
}
return gr.Run()
},
}
}