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

114 lines
3.1 KiB
Go
Raw Normal View History

2022-05-03 10:59:52 +02:00
package command
import (
"context"
"fmt"
"os"
"path"
"github.com/gofrs/uuid"
"github.com/oklog/run"
2025-03-04 16:02:36 +01:00
"github.com/owncloud/reva/v2/cmd/revad/runtime"
2024-11-07 10:34:58 +01:00
"github.com/urfave/cli/v2"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"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/frontend/pkg/config"
"github.com/owncloud/ocis/v2/services/frontend/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/frontend/pkg/logging"
"github.com/owncloud/ocis/v2/services/frontend/pkg/revaconfig"
"github.com/owncloud/ocis/v2/services/frontend/pkg/server/debug"
2022-05-03 10:59:52 +02:00
)
// 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)
2024-01-24 16:34:58 +01:00
traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name)
if err != nil {
return err
}
2022-05-03 10:59:52 +02:00
gr := run.Group{}
2024-07-24 13:33:57 +02:00
ctx, cancel := context.WithCancel(c.Context)
2022-05-03 10:59:52 +02:00
defer cancel()
rCfg, err := revaconfig.FrontendConfigFromStruct(cfg, logger)
2022-08-12 16:05:15 +02:00
if err != nil {
return err
}
2022-05-03 10:59:52 +02:00
// make sure the run group executes all interrupt handlers when the context is canceled
gr.Add(func() error {
<-ctx.Done()
return nil
}, func(_ error) {
})
2022-05-03 10:59:52 +02:00
gr.Add(func() error {
pidFile := path.Join(os.TempDir(), "revad-"+cfg.Service.Name+"-"+uuid.Must(uuid.NewV4()).String()+".pid")
reg := registry.GetRegistry()
runtime.RunWithOptions(rCfg, pidFile,
runtime.WithLogger(&logger.Logger),
runtime.WithRegistry(reg),
2024-01-24 16:34:58 +01:00
runtime.WithTraceProvider(traceProvider),
)
2022-05-03 10:59:52 +02:00
return nil
2022-07-05 08:41:21 +02:00
}, func(err error) {
2024-10-25 01:07:04 +02:00
if err == nil {
logger.Info().
Str("transport", "reva").
Str("server", cfg.Service.Name).
Msg("Shutting down server")
} else {
logger.Error().Err(err).
Str("transport", "reva").
Str("server", cfg.Service.Name).
Msg("Shutting down server")
}
2022-05-03 10:59:52 +02:00
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) {
2024-10-10 11:57:59 +02:00
_ = debugServer.Shutdown(ctx)
2022-05-03 10:59:52 +02:00
cancel()
})
httpSvc := registry.BuildHTTPService(cfg.HTTP.Namespace+"."+cfg.Service.Name, cfg.HTTP.Addr, version.GetString())
2024-11-07 10:34:58 +01:00
if err := registry.RegisterService(ctx, logger, httpSvc, cfg.Debug.Addr); err != nil {
logger.Fatal().Err(err).Msg("failed to register the http service")
2022-05-03 10:59:52 +02:00
}
2023-09-06 16:46:43 +02:00
// add event handler
gr.Add(func() error {
return ListenForEvents(ctx, cfg, logger)
}, func(_ error) {
cancel()
})
2023-08-22 11:54:45 +02:00
2022-05-03 10:59:52 +02:00
return gr.Run()
},
}
}