Files

89 lines
2.4 KiB
Go
Raw Permalink Normal View History

2023-03-15 15:21:45 +01:00
package command
import (
"context"
"fmt"
2024-05-02 14:10:32 +02:00
"os/signal"
2023-03-15 15:21:45 +01:00
"github.com/urfave/cli/v2"
2023-03-15 15:21:45 +01:00
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
2024-05-02 14:10:32 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/runner"
2023-08-21 12:04:54 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
2023-03-15 15:21:45 +01:00
"github.com/owncloud/ocis/v2/services/antivirus/pkg/config"
"github.com/owncloud/ocis/v2/services/antivirus/pkg/config/parser"
2024-10-09 12:27:54 +02:00
"github.com/owncloud/ocis/v2/services/antivirus/pkg/server/debug"
2023-03-15 15:21:45 +01:00
"github.com/owncloud/ocis/v2/services/antivirus/pkg/service"
)
// Server is the entrypoint for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
2023-03-22 13:45:53 +01:00
Usage: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name),
2023-03-15 15:21:45 +01:00
Category: "server",
Before: func(c *cli.Context) error {
return configlog.ReturnFatal(parser.ParseConfig(cfg))
},
Action: func(c *cli.Context) error {
2024-05-02 14:10:32 +02:00
var cancel context.CancelFunc
if cfg.Context == nil {
cfg.Context, cancel = signal.NotifyContext(context.Background(), runner.StopSignals...)
2024-05-02 14:10:32 +02:00
defer cancel()
}
ctx := cfg.Context
2024-05-02 14:10:32 +02:00
logger := log.NewLogger(
log.Name(cfg.Service.Name),
log.Level(cfg.Log.Level),
log.Pretty(cfg.Log.Pretty),
log.Color(cfg.Log.Color),
log.File(cfg.Log.File),
2023-03-15 15:21:45 +01:00
)
2024-05-02 14:10:32 +02:00
2023-08-21 12:04:54 +02:00
traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name)
if err != nil {
return err
}
2024-05-02 14:10:32 +02:00
2025-05-27 18:36:30 +02:00
gr := runner.NewGroup()
2023-03-15 15:21:45 +01:00
{
2023-08-21 12:04:54 +02:00
svc, err := service.NewAntivirus(cfg, logger, traceProvider)
2023-03-15 15:21:45 +01:00
if err != nil {
return err
}
2025-05-27 13:02:34 +02:00
gr.Add(runner.New(cfg.Service.Name+".svc", func() error {
2024-05-02 14:10:32 +02:00
return svc.Run()
}, func() {
svc.Close()
}))
2023-03-15 15:21:45 +01:00
}
{
2024-10-09 12:27:54 +02:00
debugServer, err := debug.Server(
2023-03-15 15:21:45 +01:00
debug.Logger(logger),
2024-10-09 12:27:54 +02:00
debug.Context(ctx),
debug.Config(cfg),
2023-03-15 15:21:45 +01:00
)
2024-10-09 12:27:54 +02:00
if err != nil {
logger.Info().Err(err).Str("server", "debug").Msg("Failed to initialize server")
return err
}
2023-03-15 15:21:45 +01:00
2025-05-27 13:02:34 +02:00
gr.Add(runner.NewGolangHttpServerRunner(cfg.Service.Name+".debug", debugServer))
2023-03-15 15:21:45 +01:00
}
2025-06-10 18:21:49 +02:00
logger.Warn().Msgf("starting service %s", cfg.Service.Name)
2024-05-02 14:10:32 +02:00
grResults := gr.Run(ctx)
2025-06-10 18:21:49 +02:00
if err := runner.ProcessResults(grResults); err != nil {
logger.Error().Err(err).Msgf("service %s stopped with error", cfg.Service.Name)
return err
2024-05-02 14:10:32 +02:00
}
2025-06-10 18:21:49 +02:00
logger.Warn().Msgf("service %s stopped without error", cfg.Service.Name)
2024-05-02 14:10:32 +02:00
return nil
2023-03-15 15:21:45 +01:00
},
}
}