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

99 lines
2.3 KiB
Go
Raw Normal View History

2020-01-22 17:49:46 +01:00
package command
import (
"context"
"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/config/parser"
"github.com/owncloud/ocis/graph-explorer/pkg/logging"
2021-02-04 12:23:11 +01:00
"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"
"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",
2021-03-15 15:17:29 +00:00
Before: func(ctx *cli.Context) error {
return parser.ParseConfig(cfg)
2020-01-22 17:49:46 +01:00
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
err := tracing.Configure(cfg)
if err != nil {
return err
}
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()
mtrcs.BuildInfo.WithLabelValues(cfg.Service.Version).Set(1)
2021-03-15 15:17:29 +00:00
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
})
}
return gr.Run()
},
}
}