2019-12-04 17:15:11 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2022-01-07 13:19:39 +01:00
|
|
|
"fmt"
|
2019-12-04 17:15:11 +01:00
|
|
|
|
|
|
|
|
"github.com/oklog/run"
|
2022-08-18 08:57:27 +00:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
|
2022-11-03 10:17:08 +01:00
|
|
|
ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc"
|
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/owncloud/ocis/v2/services/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{
|
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",
|
2022-01-03 19:08:11 +01:00
|
|
|
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)
|
|
|
|
|
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
|
|
|
}
|
2022-11-03 10:17:08 +01:00
|
|
|
err = ogrpc.Configure(ogrpc.GetClientOptions(cfg.GRPCClientTLS)...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
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()
|
|
|
|
|
|
2022-06-13 10:41:14 +02:00
|
|
|
mtrcs.BuildInfo.WithLabelValues(version.GetString()).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 {
|
2023-02-21 09:08:01 +01:00
|
|
|
logger.Error().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()
|
2022-07-05 08:41:21 +02:00
|
|
|
}, func(err error) {
|
|
|
|
|
logger.Error().
|
2019-12-06 13:14:21 +01:00
|
|
|
Str("transport", "http").
|
2022-07-05 08:41:21 +02:00
|
|
|
Err(err).
|
2019-12-06 13:14:21 +01:00
|
|
|
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()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|