diff --git a/cmd/webhix/main.go b/cmd/webhix/main.go index 3e7ea30..9f0fead 100644 --- a/cmd/webhix/main.go +++ b/cmd/webhix/main.go @@ -8,6 +8,8 @@ import ( "syscall" "github.com/GaIsBAX/Webhix/internal/app" + "github.com/GaIsBAX/Webhix/internal/cli" + "github.com/GaIsBAX/Webhix/internal/cli/serve" "github.com/GaIsBAX/Webhix/internal/config" _ "github.com/GaIsBAX/Webhix/pkg" ) @@ -26,14 +28,12 @@ func main() { os.Exit(1) } - application, err := app.New(ctx, cfg) - if err != nil { - slog.Error("up app", "err", err) - os.Exit(1) - } + serveFactory := serve.ServiceFactory(func() (serve.Service, error) { + return app.New(ctx, cfg) + }) - if err := application.Start(ctx); err != nil { - slog.Error("start app", "err", err) + if err := cli.Run(ctx, cfg, os.Args[1:], serveFactory); err != nil { + slog.Error("run", "err", err) os.Exit(1) } } diff --git a/internal/app/app.go b/internal/app/app.go index 2672e38..e6779c2 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -8,6 +8,7 @@ import ( "time" "github.com/GaIsBAX/Webhix/internal/config" + "github.com/GaIsBAX/Webhix/internal/core" "github.com/GaIsBAX/Webhix/internal/server" "github.com/GaIsBAX/Webhix/internal/server/middleware" ) @@ -88,6 +89,15 @@ func (a *App) Start(ctx context.Context) error { } } +func (a *App) RunServe(ctx context.Context, retention time.Duration) error { + a.deps.services.serve.StartRetentionCleaner( + ctx, + core.ServeRunOptions{Retention: retention, ReadOnly: a.config.ReadOnly}, + func(err error) { slog.Error("retention cleaner", "err", err) }, + ) + return a.Start(ctx) +} + func (a *App) Shutdown(ctx context.Context) error { slog.Info("shutting down") a.deps.infra.hub.Close() diff --git a/internal/app/deps.go b/internal/app/deps.go index 67e2b82..817e481 100644 --- a/internal/app/deps.go +++ b/internal/app/deps.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io/fs" "net/http" "github.com/GaIsBAX/Webhix/internal/config" @@ -12,6 +13,7 @@ import ( "github.com/GaIsBAX/Webhix/internal/repos" "github.com/GaIsBAX/Webhix/internal/server" "github.com/GaIsBAX/Webhix/internal/store" + "github.com/GaIsBAX/Webhix/internal/web" "github.com/GaIsBAX/Webhix/pkg" ) @@ -47,13 +49,18 @@ func newDependencies(ctx context.Context, cfg *config.Config) (*dependencies, er deps.handlers = newHandlers(&deps) deps.handlers.registerRoutes() + staticFS, err := fs.Sub(web.Static, "static") + if err != nil { + return nil, err + } + mux.Handle("/", http.FileServer(http.FS(staticFS))) + return &deps, nil } type services struct { - hook *core.Hook - serve *core.Serve - version *core.Version + hook *core.Hook + serve *core.Serve } func newServices(repos *repositories) *services { @@ -61,12 +68,10 @@ func newServices(repos *repositories) *services { return pkg.GeneratePrefixedString("ho") }) serve := core.NewServe(repos.serve) - version := core.NewVersion() return &services{ - hook: hook, - serve: serve, - version: version, + hook: hook, + serve: serve, } } diff --git a/internal/cli/cli.go b/internal/cli/cli.go index afe7cf6..c59c97e 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -4,7 +4,6 @@ import ( "context" "github.com/GaIsBAX/Webhix/internal/cli/serve" - "github.com/GaIsBAX/Webhix/internal/cli/version" "github.com/GaIsBAX/Webhix/internal/config" ) @@ -12,15 +11,10 @@ func Run( ctx context.Context, cfg *config.Config, args []string, - versionService version.Service, serveFactory serve.ServiceFactory, ) error { - root := NewRootCommand(ctx, cfg, versionService, serveFactory) + root := NewRootCommand(ctx, cfg, serveFactory) root.SetArgs(args) - if err := root.Execute(); err != nil { - return err - } - - return nil + return root.Execute() } diff --git a/internal/cli/root.go b/internal/cli/root.go index 90732ee..f6f6024 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -7,20 +7,20 @@ import ( "github.com/GaIsBAX/Webhix/internal/cli/serve" "github.com/GaIsBAX/Webhix/internal/cli/version" "github.com/GaIsBAX/Webhix/internal/config" + "github.com/GaIsBAX/Webhix/internal/core" "github.com/spf13/cobra" ) func NewRootCommand( ctx context.Context, cfg *config.Config, - versionService version.Service, serveFactory serve.ServiceFactory, ) *cobra.Command { cmd := &cobra.Command{ Use: "webhix", SilenceUsage: true, SilenceErrors: true, - Version: versionService.Info().Version, + Version: core.WebhixVersion, } cmd.SetVersionTemplate("webhix {{.Version}}\n") @@ -28,7 +28,7 @@ func NewRootCommand( cmd.AddCommand(serve.NewCommand(ctx, cfg, serveFactory)) cmd.AddCommand(forward.NewCommand(ctx, cfg)) - cmd.AddCommand(version.NewCommand(ctx, versionService)) + cmd.AddCommand(version.NewCommand(ctx)) return cmd } diff --git a/internal/cli/serve/command.go b/internal/cli/serve/command.go index d3b04ea..6934851 100644 --- a/internal/cli/serve/command.go +++ b/internal/cli/serve/command.go @@ -3,9 +3,9 @@ package serve import ( "context" "log/slog" + "time" "github.com/GaIsBAX/Webhix/internal/config" - "github.com/GaIsBAX/Webhix/internal/core" "github.com/spf13/cobra" ) @@ -15,18 +15,10 @@ const ( ) type Service interface { - Run(ctx context.Context, opts core.ServeRunOptions, start core.ServeStartFunc, onRetentionError func(error)) error + RunServe(ctx context.Context, retention time.Duration) error } -type ServiceFactory interface { - New(ctx context.Context, cfg *config.Config) (Service, core.ServeStartFunc, error) -} - -type ServiceFactoryFunc func(ctx context.Context, cfg *config.Config) (Service, core.ServeStartFunc, error) - -func (f ServiceFactoryFunc) New(ctx context.Context, cfg *config.Config) (Service, core.ServeStartFunc, error) { - return f(ctx, cfg) -} +type ServiceFactory func() (Service, error) func NewCommand(ctx context.Context, cfg *config.Config, factory ServiceFactory) *cobra.Command { opts := DefaultOptions() @@ -40,13 +32,13 @@ func NewCommand(ctx context.Context, cfg *config.Config, factory ServiceFactory) return err } - service, start, err := factory.New(ctx, cfg) + service, err := factory() if err != nil { slog.Error("init app", "err", err) return err } - return run(ctx, service, start, cfg, opts) + return service.RunServe(ctx, opts.Retention) }, } diff --git a/internal/cli/serve/runner.go b/internal/cli/serve/runner.go deleted file mode 100644 index da04e0d..0000000 --- a/internal/cli/serve/runner.go +++ /dev/null @@ -1,18 +0,0 @@ -package serve - -import ( - "context" - "log/slog" - - "github.com/GaIsBAX/Webhix/internal/config" - "github.com/GaIsBAX/Webhix/internal/core" -) - -func run(ctx context.Context, service Service, start core.ServeStartFunc, cfg *config.Config, opts Options) error { - return service.Run(ctx, core.ServeRunOptions{ - Retention: opts.Retention, - ReadOnly: cfg.ReadOnly, - }, start, func(err error) { - slog.Error("retention cleaner", "err", err) - }) -} diff --git a/internal/cli/version/command.go b/internal/cli/version/command.go index 76fcc30..55c31fb 100644 --- a/internal/cli/version/command.go +++ b/internal/cli/version/command.go @@ -6,15 +6,12 @@ import ( "fmt" "io" + "github.com/GaIsBAX/Webhix/internal/core" "github.com/GaIsBAX/Webhix/internal/domain" "github.com/spf13/cobra" "gopkg.in/yaml.v3" ) -type Service interface { - Info() domain.VersionInfo -} - type versionInfoContract struct { Version string `json:"version" yaml:"version"` Commit string `json:"commit" yaml:"commit"` @@ -22,7 +19,8 @@ type versionInfoContract struct { Go string `json:"go" yaml:"go"` } -func NewCommand(ctx context.Context, service Service) *cobra.Command { +func NewCommand(ctx context.Context) *cobra.Command { + service := core.NewVersion() opts := NewOptions() cmd := &cobra.Command{ diff --git a/internal/core/serve_core.go b/internal/core/serve_core.go index a4f397b..ddc990e 100644 --- a/internal/core/serve_core.go +++ b/internal/core/serve_core.go @@ -11,8 +11,6 @@ type ServeRunOptions struct { ReadOnly bool } -type ServeStartFunc func(context.Context) error - type ServeRepository interface { DeleteWebhookRequestsOlderThan(ctx context.Context, retention time.Duration) (int64, error) GetCountRequests(ctx context.Context) (int64, error) @@ -54,11 +52,6 @@ func (s *Serve) RetentionCleaner(ctx context.Context, retention time.Duration) ( } } -func (s *Serve) Run(ctx context.Context, opts ServeRunOptions, start ServeStartFunc, onRetentionError func(error)) error { - s.StartRetentionCleaner(ctx, opts, onRetentionError) - return start(ctx) -} - func (s *Serve) StartRetentionCleaner(ctx context.Context, opts ServeRunOptions, onError func(error)) { if opts.Retention <= 0 || opts.ReadOnly { return