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

115 lines
2.7 KiB
Go
Raw Normal View History

2020-01-22 17:49:46 +01:00
package command
import (
"context"
"strings"
"github.com/oklog/run"
2021-02-04 12:23:11 +01:00
"github.com/owncloud/ocis/graph-explorer/pkg/config"
"github.com/owncloud/ocis/graph-explorer/pkg/flagset"
"github.com/owncloud/ocis/graph-explorer/pkg/metrics"
"github.com/owncloud/ocis/graph-explorer/pkg/server/debug"
"github.com/owncloud/ocis/graph-explorer/pkg/server/http"
2021-03-18 12:10:36 +01:00
"github.com/owncloud/ocis/graph-explorer/pkg/tracing"
2021-03-15 15:17:29 +00:00
"github.com/owncloud/ocis/ocis-pkg/sync"
"github.com/urfave/cli/v2"
2020-01-22 17:49:46 +01:00
)
// Server is the entrypoint for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
2020-01-22 17:49:46 +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)
2020-01-22 17:49:46 +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-explorer").Msg("ignoring config file parsing when running supervised")
2020-01-22 17:49:46 +01:00
return nil
},
Action: func(c *cli.Context) error {
logger := NewLogger(cfg)
2021-08-09 18:03:01 +02:00
tracing.Configure(cfg)
2020-01-22 17:49:46 +01:00
var (
gr = run.Group{}
2021-03-15 15:17:29 +00:00
ctx, cancel = func() (context.Context, context.CancelFunc) {
if cfg.Context == nil {
return context.WithCancel(context.Background())
}
return context.WithCancel(cfg.Context)
}()
mtrcs = metrics.New()
2020-01-22 17:49:46 +01:00
)
defer cancel()
2021-03-15 15:17:29 +00:00
mtrcs.BuildInfo.WithLabelValues(cfg.Server.Version).Set(1)
2020-01-22 17:49:46 +01:00
{
server, err := http.Server(
http.Logger(logger),
http.Context(ctx),
http.Namespace(cfg.HTTP.Namespace),
http.Config(cfg),
2021-03-15 15:17:29 +00:00
http.Metrics(mtrcs),
2020-01-22 17:49:46 +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")
2020-01-22 17:49:46 +01:00
return err
}
gr.Add(func() error {
err := server.Run()
if err != nil {
logger.Error().
Err(err).
Str("transport", "http").
Msg("Failed to start server")
}
return err
2020-01-22 17:49:46 +01:00
}, func(_ error) {
logger.Info().
Str("transport", "http").
Msg("Shutting down server")
cancel()
})
}
{
server, err := debug.Server(
debug.Logger(logger),
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")
2020-01-22 17:49:46 +01:00
return err
}
2021-03-15 15:17:29 +00:00
gr.Add(server.ListenAndServe, func(_ error) {
_ = server.Shutdown(ctx)
cancel()
2020-01-22 17:49:46 +01:00
})
}
2021-03-15 15:17:29 +00:00
if !cfg.Supervised {
sync.Trap(&gr, cancel)
2020-01-22 17:49:46 +01:00
}
return gr.Run()
},
}
}