Files
ocis/services/sharing/pkg/command/server.go
T

117 lines
3.5 KiB
Go
Raw Normal View History

2022-05-03 10:59:52 +02:00
package command
import (
"context"
"fmt"
"os"
"path"
"path/filepath"
2022-05-03 10:59:52 +02:00
"github.com/cs3org/reva/v2/cmd/revad/runtime"
"github.com/gofrs/uuid"
"github.com/oklog/run"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
2022-05-04 14:49:59 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/sync"
2023-08-21 12:27:37 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
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/sharing/pkg/config"
"github.com/owncloud/ocis/v2/services/sharing/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/sharing/pkg/logging"
"github.com/owncloud/ocis/v2/services/sharing/pkg/revaconfig"
"github.com/owncloud/ocis/v2/services/sharing/pkg/server/debug"
2022-05-03 10:59:52 +02:00
"github.com/urfave/cli/v2"
)
// Server is the entry point for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
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-05-03 10:59:52 +02:00
Category: "server",
Before: func(c *cli.Context) error {
2022-08-22 08:08:18 +02:00
return configlog.ReturnFatal(parser.ParseConfig(cfg))
2022-05-03 10:59:52 +02:00
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
2023-08-21 12:27:37 +02:00
tracingProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name)
2022-05-03 10:59:52 +02:00
if err != nil {
return err
}
gr := run.Group{}
ctx, cancel := defineContext(cfg)
defer cancel()
// precreate folders
if cfg.UserSharingDriver == "json" && cfg.UserSharingDrivers.JSON.File != "" {
if err := os.MkdirAll(filepath.Dir(cfg.UserSharingDrivers.JSON.File), os.FileMode(0700)); err != nil {
return err
}
}
if cfg.PublicSharingDriver == "json" && cfg.PublicSharingDrivers.JSON.File != "" {
if err := os.MkdirAll(filepath.Dir(cfg.PublicSharingDrivers.JSON.File), os.FileMode(0700)); err != nil {
return err
}
}
gr.Add(func() error {
pidFile := path.Join(os.TempDir(), "revad-"+cfg.Service.Name+"-"+uuid.Must(uuid.NewV4()).String()+".pid")
rCfg := revaconfig.SharingConfigFromStruct(cfg)
reg := registry.GetRegistry()
2022-05-03 10:59:52 +02:00
runtime.RunWithOptions(rCfg, pidFile,
runtime.WithLogger(&logger.Logger),
runtime.WithRegistry(reg),
2023-08-21 12:27:37 +02:00
runtime.WithTraceProvider(tracingProvider),
)
2022-05-03 10:59:52 +02:00
return nil
2022-07-05 08:41:21 +02:00
}, func(err error) {
logger.Error().
Err(err).
2022-05-03 10:59:52 +02:00
Str("server", cfg.Service.Name).
Msg("Shutting down server")
cancel()
})
debugServer, err := debug.Server(
debug.Logger(logger),
debug.Context(ctx),
debug.Config(cfg),
)
if err != nil {
logger.Info().Err(err).Str("server", "debug").Msg("Failed to initialize server")
return err
}
gr.Add(debugServer.ListenAndServe, func(_ error) {
cancel()
})
if !cfg.Supervised {
sync.Trap(&gr, cancel)
}
grpcSvc := registry.BuildGRPCService(cfg.GRPC.Namespace+"."+cfg.Service.Name, uuid.Must(uuid.NewV4()).String(), cfg.GRPC.Addr, version.GetString())
if err := registry.RegisterService(ctx, grpcSvc, logger); err != nil {
logger.Fatal().Err(err).Msg("failed to register the grpc service")
2022-05-03 10:59:52 +02:00
}
return gr.Run()
},
}
}
// defineContext sets the context for the service. If there is a context configured it will create a new child from it,
2022-05-03 10:59:52 +02:00
// if not, it will create a root context that can be cancelled.
func defineContext(cfg *config.Config) (context.Context, context.CancelFunc) {
return func() (context.Context, context.CancelFunc) {
if cfg.Context == nil {
return context.WithCancel(context.Background())
}
return context.WithCancel(cfg.Context)
}()
}