From 903fa68f144fb24bb5447011e27f21da662f2aaf Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 5 Feb 2026 18:09:05 +1100 Subject: [PATCH] refactor: similar to the cache refactor This allows for strategy-specific parameters to passed in during construction. --- cmd/cachewd/main.go | 21 ++++++++++--- internal/cache/remote_test.go | 5 ++-- internal/config/config.go | 16 ++++++---- internal/strategy/api.go | 36 +++++++++++++---------- internal/strategy/apiv1.go | 7 ++--- internal/strategy/artifactory.go | 7 ++--- internal/strategy/artifactory_test.go | 7 ++--- internal/strategy/git/git.go | 6 ++-- internal/strategy/github_releases.go | 7 ++--- internal/strategy/github_releases_test.go | 7 ++--- internal/strategy/gomod/gomod.go | 7 ++--- internal/strategy/gomod/gomod_test.go | 3 +- internal/strategy/hermit.go | 11 +++---- internal/strategy/hermit_test.go | 5 ++-- internal/strategy/host.go | 7 ++--- internal/strategy/host_test.go | 9 +++--- 16 files changed, 89 insertions(+), 72 deletions(-) diff --git a/cmd/cachewd/main.go b/cmd/cachewd/main.go index 332edf2..076bcfa 100644 --- a/cmd/cachewd/main.go +++ b/cmd/cachewd/main.go @@ -23,6 +23,9 @@ import ( "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/metrics" + "github.com/block/cachew/internal/strategy" + "github.com/block/cachew/internal/strategy/git" + "github.com/block/cachew/internal/strategy/gomod" ) var cli struct { @@ -30,6 +33,7 @@ var cli struct { Config *os.File `hcl:"-" help:"Configuration file path." placeholder:"PATH" required:"" default:"cachew.hcl"` Bind string `hcl:"bind" default:"127.0.0.1:8080" help:"Bind address for the server."` + URL string `hcl:"url" default:"http://127.0.0.1:8080/" help:"Base URL for cachewd."` SchedulerConfig jobscheduler.Config `embed:"" prefix:"scheduler-"` LoggingConfig logging.Config `embed:"" prefix:"log-"` MetricsConfig metrics.Config `embed:"" prefix:"metrics-"` @@ -41,15 +45,26 @@ func main() { ctx := context.Background() logger, ctx := logging.Configure(ctx, cli.LoggingConfig) + scheduler := jobscheduler.New(ctx, cli.SchedulerConfig) + cr := cache.NewRegistry() cache.RegisterMemory(cr) cache.RegisterDisk(cr) cache.RegisterS3(cr) + sr := strategy.NewRegistry() + strategy.RegisterAPIV1(sr) + strategy.RegisterArtifactory(sr) + strategy.RegisterGitHubReleases(sr) + strategy.RegisterHermit(sr, cli.URL) + strategy.RegisterHost(sr) + git.Register(sr, scheduler) + gomod.Register(sr) + // Commands switch { //nolint:gocritic case cli.Schema: - schema := config.Schema(cr) + schema := config.Schema(cr, sr) slices.SortStableFunc(schema.Entries, func(a, b hcl.Entry) int { return strings.Compare(a.EntryKey(), b.EntryKey()) }) @@ -78,9 +93,7 @@ func main() { _, _ = w.Write([]byte("OK")) //nolint:errcheck }) - scheduler := jobscheduler.New(ctx, cli.SchedulerConfig) - - err := config.Load(ctx, cr, cli.Config, scheduler, mux, parseEnvars()) + err := config.Load(ctx, cr, sr, cli.Config, mux, parseEnvars()) kctx.FatalIfErrorf(err) metricsClient, err := metrics.New(ctx, cli.MetricsConfig) diff --git a/internal/cache/remote_test.go b/internal/cache/remote_test.go index dd54fed..4b27fa2 100644 --- a/internal/cache/remote_test.go +++ b/internal/cache/remote_test.go @@ -12,7 +12,6 @@ import ( "github.com/block/cachew/internal/cache" "github.com/block/cachew/internal/cache/cachetest" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy" ) @@ -28,7 +27,7 @@ func TestRemoteCache(t *testing.T) { t.Cleanup(func() { memCache.Close() }) mux := http.NewServeMux() - _, err = strategy.NewAPIV1(ctx, struct{}{}, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + _, err = strategy.NewAPIV1(ctx, struct{}{}, memCache, mux) assert.NoError(t, err) ts := httptest.NewServer(mux) t.Cleanup(ts.Close) @@ -53,7 +52,7 @@ func TestRemoteCacheSoak(t *testing.T) { defer memCache.Close() mux := http.NewServeMux() - _, err = strategy.NewAPIV1(ctx, struct{}{}, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + _, err = strategy.NewAPIV1(ctx, struct{}{}, memCache, mux) assert.NoError(t, err) ts := httptest.NewServer(mux) defer ts.Close() diff --git a/internal/config/config.go b/internal/config/config.go index 22fa650..68893af 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -12,7 +12,6 @@ import ( "github.com/alecthomas/hcl/v2" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy" _ "github.com/block/cachew/internal/strategy/git" // Register git strategy @@ -37,14 +36,21 @@ func (l *loggingMux) HandleFunc(pattern string, handler func(http.ResponseWriter var _ strategy.Mux = (*loggingMux)(nil) // Schema returns the configuration file schema. -func Schema(cr *cache.Registry) *hcl.AST { +func Schema(cr *cache.Registry, sr *strategy.Registry) *hcl.AST { return &hcl.AST{ - Entries: append(strategy.Schema().Entries, cr.Schema().Entries...), + Entries: append(sr.Schema().Entries, cr.Schema().Entries...), } } // Load HCL configuration and uses that to construct the cache backend, and proxy strategies. -func Load(ctx context.Context, cr *cache.Registry, r io.Reader, scheduler jobscheduler.Scheduler, mux *http.ServeMux, vars map[string]string) error { +func Load( + ctx context.Context, + cr *cache.Registry, + sr *strategy.Registry, + r io.Reader, + mux *http.ServeMux, + vars map[string]string, +) error { logger := logging.FromContext(ctx) ast, err := hcl.Parse(r) if err != nil { @@ -88,7 +94,7 @@ func Load(ctx context.Context, cr *cache.Registry, r io.Reader, scheduler jobsch for _, block := range strategyCandidates { logger := logger.With("strategy", block.Name) mlog := &loggingMux{logger: logger, mux: mux} - _, err := strategy.Create(ctx, block.Name, block, scheduler.WithQueuePrefix(block.Name), cache, mlog, vars) + _, err := sr.Create(ctx, block.Name, block, cache, mlog, vars) if err != nil { return errors.Errorf("%s: %w", block.Pos, err) } diff --git a/internal/strategy/api.go b/internal/strategy/api.go index b7979da..90ecb31 100644 --- a/internal/strategy/api.go +++ b/internal/strategy/api.go @@ -10,7 +10,6 @@ import ( "github.com/alecthomas/hcl/v2" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" ) // ErrNotFound is returned when a strategy is not found. @@ -21,17 +20,25 @@ type Mux interface { HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) } +type Registry struct { + registry map[string]registryEntry +} + +func NewRegistry() *Registry { + return &Registry{ + registry: make(map[string]registryEntry), + } +} + type registryEntry struct { schema *hcl.Block - factory func(ctx context.Context, config *hcl.Block, scheduler jobscheduler.Scheduler, cache cache.Cache, mux Mux, vars map[string]string) (Strategy, error) + factory func(ctx context.Context, config *hcl.Block, cache cache.Cache, mux Mux, vars map[string]string) (Strategy, error) } -var registry = map[string]registryEntry{} - -type Factory[Config any, S Strategy] func(ctx context.Context, config Config, scheduler jobscheduler.Scheduler, cache cache.Cache, mux Mux) (S, error) +type Factory[Config any, S Strategy] func(ctx context.Context, config Config, cache cache.Cache, mux Mux) (S, error) // Register a new proxy strategy. -func Register[Config any, S Strategy](id, description string, factory Factory[Config, S]) { +func Register[Config any, S Strategy](r *Registry, id, description string, factory Factory[Config, S]) { var c Config schema, err := hcl.BlockSchema(id, &c) if err != nil { @@ -39,9 +46,9 @@ func Register[Config any, S Strategy](id, description string, factory Factory[Co } block := schema.Entries[0].(*hcl.Block) //nolint:errcheck // This seems spurious block.Comments = hcl.CommentList{description} - registry[id] = registryEntry{ + r.registry[id] = registryEntry{ schema: block, - factory: func(ctx context.Context, config *hcl.Block, scheduler jobscheduler.Scheduler, cache cache.Cache, mux Mux, vars map[string]string) (Strategy, error) { + factory: func(ctx context.Context, config *hcl.Block, cache cache.Cache, mux Mux, vars map[string]string) (Strategy, error) { var cfg Config transformer := func(defaultValue string) string { return os.Expand(defaultValue, func(key string) string { return vars[key] }) @@ -49,15 +56,15 @@ func Register[Config any, S Strategy](id, description string, factory Factory[Co if err := hcl.UnmarshalBlock(config, &cfg, hcl.AllowExtra(false), hcl.WithDefaultTransformer(transformer)); err != nil { return nil, errors.WithStack(err) } - return factory(ctx, cfg, scheduler, cache, mux) + return factory(ctx, cfg, cache, mux) }, } } // Schema returns the schema for all registered strategies. -func Schema() *hcl.AST { +func (r *Registry) Schema() *hcl.AST { ast := &hcl.AST{} - for _, entry := range registry { + for _, entry := range r.registry { ast.Entries = append(ast.Entries, entry.schema) } return ast @@ -66,17 +73,16 @@ func Schema() *hcl.AST { // Create a new proxy strategy. // // Will return "ErrNotFound" if the strategy is not found. -func Create( +func (r *Registry) Create( ctx context.Context, name string, config *hcl.Block, - scheduler jobscheduler.Scheduler, cache cache.Cache, mux Mux, vars map[string]string, ) (Strategy, error) { - if entry, ok := registry[name]; ok { - return errors.WithStack2(entry.factory(ctx, config, scheduler.WithQueuePrefix(name), cache, mux, vars)) + if entry, ok := r.registry[name]; ok { + return errors.WithStack2(entry.factory(ctx, config, cache, mux, vars)) } return nil, errors.Errorf("%s: %w", name, ErrNotFound) } diff --git a/internal/strategy/apiv1.go b/internal/strategy/apiv1.go index 2251976..d92b0b9 100644 --- a/internal/strategy/apiv1.go +++ b/internal/strategy/apiv1.go @@ -12,12 +12,11 @@ import ( "time" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" ) -func init() { - Register("apiv1", "The stable API of the cache server.", NewAPIV1) +func RegisterAPIV1(r *Registry) { + Register(r, "apiv1", "The stable API of the cache server.", NewAPIV1) } var _ Strategy = (*APIV1)(nil) @@ -28,7 +27,7 @@ type APIV1 struct { logger *slog.Logger } -func NewAPIV1(ctx context.Context, _ struct{}, _ jobscheduler.Scheduler, cache cache.Cache, mux Mux) (*APIV1, error) { +func NewAPIV1(ctx context.Context, _ struct{}, cache cache.Cache, mux Mux) (*APIV1, error) { s := &APIV1{ logger: logging.FromContext(ctx), cache: cache, diff --git a/internal/strategy/artifactory.go b/internal/strategy/artifactory.go index a764774..08c105c 100644 --- a/internal/strategy/artifactory.go +++ b/internal/strategy/artifactory.go @@ -10,13 +10,12 @@ import ( "strings" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy/handler" ) -func init() { - Register("artifactory", "Caches artifacts from an Artifactory server.", NewArtifactory) +func RegisterArtifactory(r *Registry) { + Register(r, "artifactory", "Caches artifacts from an Artifactory server.", NewArtifactory) } // ArtifactoryConfig represents the configuration for the Artifactory strategy. @@ -53,7 +52,7 @@ type Artifactory struct { var _ Strategy = (*Artifactory)(nil) -func NewArtifactory(ctx context.Context, config ArtifactoryConfig, _ jobscheduler.Scheduler, cache cache.Cache, mux Mux) (*Artifactory, error) { +func NewArtifactory(ctx context.Context, config ArtifactoryConfig, cache cache.Cache, mux Mux) (*Artifactory, error) { u, err := url.Parse(config.Target) if err != nil { return nil, fmt.Errorf("invalid target URL: %w", err) diff --git a/internal/strategy/artifactory_test.go b/internal/strategy/artifactory_test.go index 059f1a3..363c941 100644 --- a/internal/strategy/artifactory_test.go +++ b/internal/strategy/artifactory_test.go @@ -11,7 +11,6 @@ import ( "github.com/alecthomas/assert/v2" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy" ) @@ -64,7 +63,7 @@ func setupArtifactoryTest(t *testing.T, config strategy.ArtifactoryConfig) (*moc t.Cleanup(func() { memCache.Close() }) mux := http.NewServeMux() - _, err = strategy.NewArtifactory(ctx, config, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + _, err = strategy.NewArtifactory(ctx, config, memCache, mux) assert.NoError(t, err) return mock, mux, ctx @@ -213,7 +212,7 @@ func TestArtifactoryString(t *testing.T) { mux := http.NewServeMux() artifactory, err := strategy.NewArtifactory(ctx, strategy.ArtifactoryConfig{ Target: "https://ec2.example.jfrog.io", - }, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + }, memCache, mux) assert.NoError(t, err) assert.Equal(t, "artifactory:ec2.example.jfrog.io", artifactory.String()) @@ -228,7 +227,7 @@ func TestArtifactoryInvalidTargetURL(t *testing.T) { mux := http.NewServeMux() _, err = strategy.NewArtifactory(ctx, strategy.ArtifactoryConfig{ Target: "://invalid-url", - }, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + }, memCache, mux) assert.Error(t, err) } diff --git a/internal/strategy/git/git.go b/internal/strategy/git/git.go index c1605d1..b3a35b7 100644 --- a/internal/strategy/git/git.go +++ b/internal/strategy/git/git.go @@ -20,8 +20,10 @@ import ( "github.com/block/cachew/internal/strategy" ) -func init() { - strategy.Register("git", "Caches Git repositories, including bundle and tarball snapshots.", New) +func Register(r *strategy.Registry, scheduler jobscheduler.Scheduler) { + strategy.Register(r, "git", "Caches Git repositories, including bundle and tarball snapshots.", func(ctx context.Context, config Config, cache cache.Cache, mux strategy.Mux) (*Strategy, error) { + return New(ctx, config, scheduler, cache, mux) + }) } type Config struct { diff --git a/internal/strategy/github_releases.go b/internal/strategy/github_releases.go index 5a4866f..fa2da49 100644 --- a/internal/strategy/github_releases.go +++ b/internal/strategy/github_releases.go @@ -12,13 +12,12 @@ import ( "github.com/block/cachew/internal/cache" "github.com/block/cachew/internal/httputil" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy/handler" ) -func init() { - Register("github-releases", "Caches public and authenticated GitHub releases.", NewGitHubReleases) +func RegisterGitHubReleases(r *Registry) { + Register(r, "github-releases", "Caches public and authenticated GitHub releases.", NewGitHubReleases) } type GitHubReleasesConfig struct { @@ -34,7 +33,7 @@ type GitHubReleases struct { } // NewGitHubReleases creates a [Strategy] that fetches private (and public) release binaries from GitHub. -func NewGitHubReleases(ctx context.Context, config GitHubReleasesConfig, _ jobscheduler.Scheduler, cache cache.Cache, mux Mux) (*GitHubReleases, error) { +func NewGitHubReleases(ctx context.Context, config GitHubReleasesConfig, cache cache.Cache, mux Mux) (*GitHubReleases, error) { s := &GitHubReleases{ config: config, cache: cache, diff --git a/internal/strategy/github_releases_test.go b/internal/strategy/github_releases_test.go index dd3619a..7518a04 100644 --- a/internal/strategy/github_releases_test.go +++ b/internal/strategy/github_releases_test.go @@ -14,7 +14,6 @@ import ( "github.com/alecthomas/assert/v2" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy" ) @@ -127,7 +126,7 @@ func setupTest(t *testing.T, config strategy.GitHubReleasesConfig) (*mockGitHubS t.Cleanup(func() { memCache.Close() }) mux := http.NewServeMux() - _, err = strategy.NewGitHubReleases(ctx, config, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + _, err = strategy.NewGitHubReleases(ctx, config, memCache, mux) assert.NoError(t, err) return mock, mux, ctx @@ -242,7 +241,7 @@ func TestGitHubReleasesNoToken(t *testing.T) { defer memCache.Close() mux := http.NewServeMux() - gh, err := strategy.NewGitHubReleases(ctx, strategy.GitHubReleasesConfig{}, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + gh, err := strategy.NewGitHubReleases(ctx, strategy.GitHubReleasesConfig{}, memCache, mux) assert.NoError(t, err) assert.Equal(t, "github-releases", gh.String()) } @@ -256,7 +255,7 @@ func TestGitHubReleasesString(t *testing.T) { mux := http.NewServeMux() gh, err := strategy.NewGitHubReleases(ctx, strategy.GitHubReleasesConfig{ Token: "test-token", - }, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + }, memCache, mux) assert.NoError(t, err) assert.Equal(t, "github-releases", gh.String()) diff --git a/internal/strategy/gomod/gomod.go b/internal/strategy/gomod/gomod.go index 3fc91b3..a7e3fdf 100644 --- a/internal/strategy/gomod/gomod.go +++ b/internal/strategy/gomod/gomod.go @@ -10,13 +10,12 @@ import ( "github.com/goproxy/goproxy" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy" ) -func init() { - strategy.Register("gomod", "Caches Go module proxy requests.", New) +func Register(r *strategy.Registry) { + strategy.Register(r, "gomod", "Caches Go module proxy requests.", New) } type Config struct { @@ -33,7 +32,7 @@ type Strategy struct { var _ strategy.Strategy = (*Strategy)(nil) -func New(ctx context.Context, config Config, _ jobscheduler.Scheduler, cache cache.Cache, mux strategy.Mux) (*Strategy, error) { +func New(ctx context.Context, config Config, cache cache.Cache, mux strategy.Mux) (*Strategy, error) { parsedURL, err := url.Parse(config.Proxy) if err != nil { return nil, fmt.Errorf("invalid proxy URL: %w", err) diff --git a/internal/strategy/gomod/gomod_test.go b/internal/strategy/gomod/gomod_test.go index 2e403be..cfa23ca 100644 --- a/internal/strategy/gomod/gomod_test.go +++ b/internal/strategy/gomod/gomod_test.go @@ -15,7 +15,6 @@ import ( "github.com/alecthomas/assert/v2" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy/gomod" ) @@ -178,7 +177,7 @@ func setupGoModTest(t *testing.T) (*mockGoModServer, *http.ServeMux, context.Con mux := http.NewServeMux() _, err = gomod.New(ctx, gomod.Config{ Proxy: mock.server.URL, - }, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + }, memCache, mux) assert.NoError(t, err) return mock, mux, ctx diff --git a/internal/strategy/hermit.go b/internal/strategy/hermit.go index ad5517e..a70e03e 100644 --- a/internal/strategy/hermit.go +++ b/internal/strategy/hermit.go @@ -5,7 +5,6 @@ import ( "log/slog" "net/http" "net/url" - "os" "strings" "github.com/alecthomas/errors" @@ -16,8 +15,10 @@ import ( "github.com/block/cachew/internal/strategy/handler" ) -func init() { - Register("hermit", "Caches Hermit package downloads.", NewHermit) +func RegisterHermit(r *Registry, cachewURL string) { + Register(r, "hermit", "Caches Hermit package downloads.", func(ctx context.Context, config HermitConfig, c cache.Cache, mux Mux) (*Hermit, error) { + return NewHermit(ctx, cachewURL, config, nil, c, mux) + }) } type HermitConfig struct { @@ -39,7 +40,7 @@ type Hermit struct { var _ Strategy = (*Hermit)(nil) -func NewHermit(ctx context.Context, config HermitConfig, _ jobscheduler.Scheduler, c cache.Cache, mux Mux) (*Hermit, error) { +func NewHermit(ctx context.Context, cachewURL string, config HermitConfig, _ jobscheduler.Scheduler, c cache.Cache, mux Mux) (*Hermit, error) { logger := logging.FromContext(ctx) s := &Hermit{ @@ -54,7 +55,7 @@ func NewHermit(ctx context.Context, config HermitConfig, _ jobscheduler.Schedule mux.Handle("GET /hermit/{host}/{path...}", s.directHandler) if config.GitHubBaseURL != "" { - isInternalRedirect := strings.Contains(config.GitHubBaseURL, os.Getenv("CACHEW_URL")) + isInternalRedirect := strings.HasPrefix(config.GitHubBaseURL, cachewURL) s.redirectHandler = s.createRedirectHandler(isInternalRedirect, c) mux.Handle("GET /hermit/github.com/{path...}", s.redirectHandler) logger.InfoContext(ctx, "Hermit strategy initialized", diff --git a/internal/strategy/hermit_test.go b/internal/strategy/hermit_test.go index 02494cb..3d9b0ae 100644 --- a/internal/strategy/hermit_test.go +++ b/internal/strategy/hermit_test.go @@ -12,7 +12,6 @@ import ( "github.com/alecthomas/assert/v2" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy" ) @@ -30,7 +29,7 @@ func setupHermitTest(t *testing.T) (*http.ServeMux, context.Context, cache.Cache t.Cleanup(func() { memCache.Close() }) mux := http.NewServeMux() - _, err = strategy.NewHermit(ctx, strategy.HermitConfig{GitHubBaseURL: "http://localhost:8080"}, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + _, err = strategy.NewHermit(ctx, "http://github.com", strategy.HermitConfig{GitHubBaseURL: "http://localhost:8080"}, nil, memCache, mux) assert.NoError(t, err) return mux, ctx, memCache @@ -95,7 +94,7 @@ func TestHermitGitHubRelease(t *testing.T) { mux, ctx, memCache := setupHermitTest(t) - _, err := strategy.NewGitHubReleases(ctx, strategy.GitHubReleasesConfig{}, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + _, err := strategy.NewGitHubReleases(ctx, strategy.GitHubReleasesConfig{}, memCache, mux) assert.NoError(t, err) req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/hermit/github.com/alecthomas/chroma/releases/download/v2.14.0/chroma-2.14.0-linux-amd64.tar.gz", nil) diff --git a/internal/strategy/host.go b/internal/strategy/host.go index 2d55cae..afa4f8a 100644 --- a/internal/strategy/host.go +++ b/internal/strategy/host.go @@ -8,13 +8,12 @@ import ( "net/url" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy/handler" ) -func init() { - Register("host", "A generic host-based proxying strategy.", NewHost) +func RegisterHost(r *Registry) { + Register(r, "host", "A generic host-based proxying strategy.", NewHost) } // HostConfig represents the configuration for the Host strategy. @@ -41,7 +40,7 @@ type Host struct { var _ Strategy = (*Host)(nil) -func NewHost(ctx context.Context, config HostConfig, _ jobscheduler.Scheduler, cache cache.Cache, mux Mux) (*Host, error) { +func NewHost(ctx context.Context, config HostConfig, cache cache.Cache, mux Mux) (*Host, error) { u, err := url.Parse(config.Target) if err != nil { return nil, fmt.Errorf("invalid target URL: %w", err) diff --git a/internal/strategy/host_test.go b/internal/strategy/host_test.go index 749a119..1f8442b 100644 --- a/internal/strategy/host_test.go +++ b/internal/strategy/host_test.go @@ -12,7 +12,6 @@ import ( "github.com/alecthomas/assert/v2" "github.com/block/cachew/internal/cache" - "github.com/block/cachew/internal/jobscheduler" "github.com/block/cachew/internal/logging" "github.com/block/cachew/internal/strategy" ) @@ -32,7 +31,7 @@ func TestHostCaching(t *testing.T) { defer memCache.Close() mux := http.NewServeMux() - _, err = strategy.NewHost(ctx, strategy.HostConfig{Target: backend.URL}, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + _, err = strategy.NewHost(ctx, strategy.HostConfig{Target: backend.URL}, memCache, mux) assert.NoError(t, err) // Request path must include the host prefix from the target URL @@ -69,7 +68,7 @@ func TestHostNonOKStatus(t *testing.T) { defer memCache.Close() mux := http.NewServeMux() - _, err = strategy.NewHost(ctx, strategy.HostConfig{Target: backend.URL}, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + _, err = strategy.NewHost(ctx, strategy.HostConfig{Target: backend.URL}, memCache, mux) assert.NoError(t, err) // Request path must include the host prefix from the target URL @@ -95,7 +94,7 @@ func TestHostInvalidTargetURL(t *testing.T) { defer memCache.Close() mux := http.NewServeMux() - _, err = strategy.NewHost(ctx, strategy.HostConfig{Target: "://invalid"}, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + _, err = strategy.NewHost(ctx, strategy.HostConfig{Target: "://invalid"}, memCache, mux) assert.Error(t, err) } @@ -106,7 +105,7 @@ func TestHostString(t *testing.T) { defer memCache.Close() mux := http.NewServeMux() - host, err := strategy.NewHost(ctx, strategy.HostConfig{Target: "https://example.com/prefix"}, jobscheduler.New(ctx, jobscheduler.Config{}), memCache, mux) + host, err := strategy.NewHost(ctx, strategy.HostConfig{Target: "https://example.com/prefix"}, memCache, mux) assert.NoError(t, err) assert.Equal(t, "host:example.com/prefix", host.String())