Files

98 lines
2.9 KiB
Go

package cmd
import (
"github.com/hanzo-fi/go-libs/v2/aws/iam"
"github.com/hanzo-fi/go-libs/v2/otlp"
"github.com/hanzo-fi/go-libs/v2/otlp/otlpmetrics"
"github.com/hanzo-fi/go-libs/v2/otlp/otlptraces"
"github.com/hanzo-fi/go-libs/v2/publish"
"github.com/hanzo-fi/go-libs/v2/auth"
"github.com/hanzo-fi/go-libs/v2/bun/bunconnect"
"github.com/hanzo-fi/go-libs/v2/licence"
"github.com/hanzo-fi/go-libs/v2/service"
"github.com/hanzo-fi/webhooks/cmd/flag"
"github.com/hanzo-fi/webhooks/pkg/backoff"
innerotlp "github.com/hanzo-fi/webhooks/pkg/otlp"
"github.com/hanzo-fi/webhooks/pkg/server"
"github.com/hanzo-fi/webhooks/pkg/storage/postgres"
"github.com/hanzo-fi/webhooks/pkg/worker"
"github.com/spf13/cobra"
"go.uber.org/fx"
)
func newServeCommand() *cobra.Command {
ret := &cobra.Command{
Use: "serve",
Aliases: []string{"server"},
Short: "Run webhooks server",
RunE: serve,
PreRunE: handleAutoMigrate,
}
otlp.AddFlags(ret.Flags())
otlptraces.AddFlags(ret.Flags())
otlpmetrics.AddFlags(ret.Flags())
publish.AddFlags(ServiceName, ret.Flags())
auth.AddFlags(ret.Flags())
flag.Init(ret.Flags())
bunconnect.AddFlags(ret.Flags())
iam.AddFlags(ret.Flags())
service.AddFlags(ret.Flags())
licence.AddFlags(ret.Flags())
return ret
}
func serve(cmd *cobra.Command, _ []string) error {
connectionOptions, err := bunconnect.ConnectionOptionsFromFlags(cmd)
if err != nil {
return err
}
listen, _ := cmd.Flags().GetString(flag.Listen)
auditEnabled, _ := cmd.Flags().GetBool(flag.AuditEnabled)
options := []fx.Option{
fx.Provide(func() server.ServiceInfo {
return server.ServiceInfo{
Version: Version,
}
}),
auth.FXModuleFromFlags(cmd),
publish.FXModuleFromFlags(cmd, service.IsDebug(cmd)),
postgres.NewModule(*connectionOptions, service.IsDebug(cmd)),
// Registered after postgres so metrics stop (and flush DB-backed
// gauges) before the database connection is closed.
otlpmetrics.FXModuleFromFlags(cmd),
innerotlp.HttpClientModule(),
server.FXModuleFromFlags(cmd, listen, service.IsDebug(cmd), auditEnabled),
licence.FXModuleFromFlags(cmd, ServiceName),
}
isWorker, _ := cmd.Flags().GetBool(flag.Worker)
if isWorker {
retryPeriod, _ := cmd.Flags().GetDuration(flag.RetryPeriod)
retryBatchSize, _ := cmd.Flags().GetInt(flag.RetryBatchSize)
minBackOffDelay, _ := cmd.Flags().GetDuration(flag.MinBackoffDelay)
maxBackOffDelay, _ := cmd.Flags().GetDuration(flag.MaxBackoffDelay)
abortAfter, _ := cmd.Flags().GetDuration(flag.AbortAfter)
maxAttempts, _ := cmd.Flags().GetInt(flag.MaxAttempts)
topics, _ := cmd.Flags().GetStringSlice(flag.KafkaTopics)
options = append(options, worker.StartModule(
cmd,
retryPeriod,
backoff.NewExponential(
minBackOffDelay,
maxBackOffDelay,
abortAfter,
maxAttempts,
),
retryBatchSize,
service.IsDebug(cmd),
topics,
retentionConfigFromFlags(cmd),
))
}
return service.New(cmd.OutOrStdout(), options...).Run(cmd)
}