Skip to content

Commit 3c74060

Browse files
authored
feat: refactor handlers to pkg (#44)
1 parent 0a8a32c commit 3c74060

File tree

12 files changed

+28
-29
lines changed

12 files changed

+28
-29
lines changed

cmd/proxy/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
"time"
1616

1717
"github.com/alexflint/go-arg"
18-
"github.com/azure/peerd/internal/handlers"
1918
"github.com/azure/peerd/pkg/containerd"
2019
pcontext "github.com/azure/peerd/pkg/context"
2120
"github.com/azure/peerd/pkg/discovery/content/provider"
2221
"github.com/azure/peerd/pkg/discovery/routing"
2322
"github.com/azure/peerd/pkg/files/store"
23+
"github.com/azure/peerd/pkg/handlers"
2424
"github.com/azure/peerd/pkg/k8s"
2525
"github.com/azure/peerd/pkg/k8s/events"
2626
"github.com/azure/peerd/pkg/metrics"

internal/handlers/v2/registry.go renamed to pkg/containerd/handler.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package v2
3+
package containerd
44

55
import (
66
"fmt"
77
"net/http"
88
"strconv"
99
"time"
1010

11-
"github.com/azure/peerd/pkg/containerd"
1211
pcontext "github.com/azure/peerd/pkg/context"
1312
"github.com/azure/peerd/pkg/oci/distribution"
1413
"github.com/opencontainers/go-digest"
@@ -24,7 +23,7 @@ const (
2423

2524
// Registry is a handler that handles requests to this registry.
2625
type Registry struct {
27-
containerdStore containerd.Store
26+
containerdStore Store
2827
}
2928

3029
// Handle handles a request to this registry.
@@ -138,7 +137,7 @@ func (r *Registry) handleBlob(c pcontext.Context, dgst digest.Digest) {
138137
}
139138

140139
// NewRegistry creates a new registry handler.
141-
func NewRegistry(containerdStore containerd.Store) *Registry {
140+
func NewRegistry(containerdStore Store) *Registry {
142141
return &Registry{
143142
containerdStore: containerdStore,
144143
}

internal/handlers/v2/registry_test.go renamed to pkg/containerd/handler_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package v2
3+
package containerd
44

55
import (
66
"net/http"
77
"net/http/httptest"
88
"testing"
99

10-
"github.com/azure/peerd/pkg/containerd"
1110
pcontext "github.com/azure/peerd/pkg/context"
1211
"github.com/azure/peerd/pkg/oci/distribution"
1312
"github.com/gin-gonic/gin"
1413
)
1514

1615
func TestNewRegistry(t *testing.T) {
1716
// Create a new registry
18-
r := NewRegistry(containerd.NewMockContainerdStore(nil))
17+
r := NewRegistry(NewMockContainerdStore(nil))
1918

2019
if r == nil {
2120
t.Fatal("expected registry")
2221
}
2322
}
2423

2524
func TestHandleManifest(t *testing.T) {
26-
img, err := containerd.ParseReference("library/alpine:3.18.0", "sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30")
25+
img, err := ParseReference("library/alpine:3.18.0", "sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30")
2726
if err != nil {
2827
t.Fatal(err)
2928
}
30-
refs := []containerd.Reference{img}
29+
refs := []Reference{img}
3130

32-
ms := containerd.NewMockContainerdStore(refs)
31+
ms := NewMockContainerdStore(refs)
3332

3433
r := NewRegistry(ms)
3534

@@ -65,13 +64,13 @@ func TestHandleManifest(t *testing.T) {
6564
}
6665

6766
func TestHandleBlob(t *testing.T) {
68-
img, err := containerd.ParseReference("library/alpine:3.18.0", "sha256:blob")
67+
img, err := ParseReference("library/alpine:3.18.0", "sha256:blob")
6968
if err != nil {
7069
t.Fatal(err)
7170
}
72-
refs := []containerd.Reference{img}
71+
refs := []Reference{img}
7372

74-
ms := containerd.NewMockContainerdStore(refs)
73+
ms := NewMockContainerdStore(refs)
7574

7675
r := NewRegistry(ms)
7776

@@ -107,13 +106,13 @@ func TestHandleBlob(t *testing.T) {
107106
}
108107

109108
func TestHandle(t *testing.T) {
110-
img, err := containerd.ParseReference("library/alpine:3.18.0", "sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30")
109+
img, err := ParseReference("library/alpine:3.18.0", "sha256:bb863d6b95453b6b10dfaa1a52cb53f453d9a97ee775808ebaf6533bb4c9bb30")
111110
if err != nil {
112111
t.Fatal(err)
113112
}
114-
refs := []containerd.Reference{img}
113+
refs := []Reference{img}
115114

116-
ms := containerd.NewMockContainerdStore(refs)
115+
ms := NewMockContainerdStore(refs)
117116

118117
r := NewRegistry(ms)
119118

internal/handlers/v2/mirror.go renamed to pkg/discovery/content/registry/mirror.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package v2
3+
package registry
44

55
import (
66
"context"
@@ -24,7 +24,7 @@ var (
2424
ResolveTimeout = 1 * time.Second
2525
)
2626

27-
// Mirror is a handler that handles requests to this registry Mirror.
27+
// Mirror is a handler that handles requests to this registry proxy.
2828
type Mirror struct {
2929
resolveTimeout time.Duration
3030
router routing.Router
@@ -118,8 +118,8 @@ func (m *Mirror) Handle(c pcontext.Context) {
118118
}
119119
}
120120

121-
// NewMirror creates a new mirror handler.
122-
func NewMirror(router routing.Router) *Mirror {
121+
// New creates a new mirror handler.
122+
func New(router routing.Router) *Mirror {
123123
return &Mirror{
124124
resolveTimeout: ResolveTimeout,
125125
router: router,

internal/handlers/v2/mirror_test.go renamed to pkg/discovery/content/registry/mirror_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
package v2
3+
package registry
44

55
import (
66
"fmt"
File renamed without changes.

internal/handlers/root.go renamed to pkg/handlers/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"net/http"
88
"time"
99

10-
"github.com/azure/peerd/internal/handlers/files"
11-
v2 "github.com/azure/peerd/internal/handlers/v2"
1210
"github.com/azure/peerd/pkg/containerd"
1311
pcontext "github.com/azure/peerd/pkg/context"
1412
"github.com/azure/peerd/pkg/discovery/routing"
1513
filesStore "github.com/azure/peerd/pkg/files/store"
14+
"github.com/azure/peerd/pkg/handlers/files"
15+
v2 "github.com/azure/peerd/pkg/handlers/v2"
1616
"github.com/gin-gonic/gin"
1717
"github.com/rs/zerolog"
1818
)
File renamed without changes.

internal/handlers/v2/handler.go renamed to pkg/handlers/v2/handler.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import (
1010

1111
"github.com/azure/peerd/pkg/containerd"
1212
pcontext "github.com/azure/peerd/pkg/context"
13+
"github.com/azure/peerd/pkg/discovery/content/registry"
1314
"github.com/azure/peerd/pkg/discovery/routing"
1415
"github.com/azure/peerd/pkg/metrics"
1516
"github.com/azure/peerd/pkg/oci/distribution"
1617
)
1718

1819
// V2Handler describes a handler for OCI content.
1920
type V2Handler struct {
20-
mirror *Mirror
21-
registry *Registry
21+
proxy *registry.Mirror
22+
registry *containerd.Registry
2223
metricsRecorder metrics.Metrics
2324
}
2425

@@ -55,7 +56,7 @@ func (h *V2Handler) Handle(c pcontext.Context) {
5556
h.registry.Handle(c)
5657
return
5758
} else {
58-
h.mirror.Handle(c)
59+
h.proxy.Handle(c)
5960
return
6061
}
6162
}
@@ -86,8 +87,8 @@ func (h *V2Handler) fill(c pcontext.Context) error {
8687
// New creates a new OCI content handler.
8788
func New(ctx context.Context, router routing.Router, containerdStore containerd.Store) (*V2Handler, error) {
8889
return &V2Handler{
89-
mirror: NewMirror(router),
90-
registry: NewRegistry(containerdStore),
90+
proxy: registry.New(router),
91+
registry: containerd.NewRegistry(containerdStore),
9192
metricsRecorder: metrics.FromContext(ctx),
9293
}, nil
9394
}

0 commit comments

Comments
 (0)