Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cmd/webhix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
}
10 changes: 10 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand Down
19 changes: 12 additions & 7 deletions internal/app/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"net/http"

"github.com/GaIsBAX/Webhix/internal/config"
Expand All @@ -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"
)

Expand Down Expand Up @@ -47,26 +49,29 @@ 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 {
hook := core.NewHook(repos.hook, func() string {
return pkg.GeneratePrefixedString("ho")
})
serve := core.NewServe(repos.serve)
version := core.NewVersion()

return &services{
hook: hook,
serve: serve,
version: version,
hook: hook,
serve: serve,
}
}

Expand Down
10 changes: 2 additions & 8 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@ import (
"context"

"github.com/GaIsBAX/Webhix/internal/cli/serve"
"github.com/GaIsBAX/Webhix/internal/cli/version"
"github.com/GaIsBAX/Webhix/internal/config"
)

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()
}
6 changes: 3 additions & 3 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ 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")

addGroup(cmd, serve.ServeGroup, serve.ServeTitle)

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
}
Expand Down
18 changes: 5 additions & 13 deletions internal/cli/serve/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()
Expand All @@ -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)
},
}

Expand Down
18 changes: 0 additions & 18 deletions internal/cli/serve/runner.go

This file was deleted.

8 changes: 3 additions & 5 deletions internal/cli/version/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ 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"`
Built string `json:"built" yaml:"built"`
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{
Expand Down
7 changes: 0 additions & 7 deletions internal/core/serve_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down