2019-12-04 17:15:11 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"github.com/oklog/run"
|
2021-02-04 11:46:32 +01:00
|
|
|
"github.com/owncloud/ocis/graph/pkg/config"
|
2022-01-03 15:21:56 +01:00
|
|
|
"github.com/owncloud/ocis/graph/pkg/config/parser"
|
2021-12-16 19:40:23 +01:00
|
|
|
"github.com/owncloud/ocis/graph/pkg/logging"
|
2021-02-04 11:46:32 +01:00
|
|
|
"github.com/owncloud/ocis/graph/pkg/metrics"
|
|
|
|
|
"github.com/owncloud/ocis/graph/pkg/server/debug"
|
|
|
|
|
"github.com/owncloud/ocis/graph/pkg/server/http"
|
2021-03-18 12:09:00 +01:00
|
|
|
"github.com/owncloud/ocis/graph/pkg/tracing"
|
2021-09-14 16:05:03 +02:00
|
|
|
"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",
|
2021-03-15 15:17:29 +00:00
|
|
|
Before: func(ctx *cli.Context) error {
|
2019-12-09 14:47:38 +01:00
|
|
|
|
2022-01-03 15:21:56 +01:00
|
|
|
if err := parser.ParseConfig(cfg); err != nil {
|
2021-11-08 13:50:52 +01:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
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)
|
|
|
|
|
err := tracing.Configure(cfg)
|
|
|
|
|
if err != nil {
|
2021-03-18 12:09:00 +01:00
|
|
|
return err
|
2019-12-04 17:15:11 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 12:09:00 +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-11-24 18:30:20 +01:00
|
|
|
mtrcs.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1)
|
2021-03-15 15:17:29 +00:00
|
|
|
|
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
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return gr.Run()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|