@@ -26,21 +26,23 @@ package registry
26
26
import (
27
27
"log"
28
28
"net/http"
29
+ "os"
29
30
)
30
31
31
- type v struct {
32
+ type registry struct {
33
+ log * log.Logger
32
34
blobs blobs
33
35
manifests manifests
34
36
}
35
37
36
38
// https://docs.docker.com/registry/spec/api/#api-version-check
37
39
// https://github.com/opencontainers/distribution-spec/blob/master/spec.md#api-version-check
38
- func (v * v ) v2 (resp http.ResponseWriter , req * http.Request ) * regError {
40
+ func (r * registry ) v2 (resp http.ResponseWriter , req * http.Request ) * regError {
39
41
if isBlob (req ) {
40
- return v .blobs .handle (resp , req )
42
+ return r .blobs .handle (resp , req )
41
43
}
42
44
if isManifest (req ) {
43
- return v .manifests .handle (resp , req )
45
+ return r .manifests .handle (resp , req )
44
46
}
45
47
resp .Header ().Set ("Docker-Distribution-API-Version" , "registry/2.0" )
46
48
if req .URL .Path != "/v2/" && req .URL .Path != "/v2" {
@@ -54,18 +56,20 @@ func (v *v) v2(resp http.ResponseWriter, req *http.Request) *regError {
54
56
return nil
55
57
}
56
58
57
- func (v * v ) root (resp http.ResponseWriter , req * http.Request ) {
58
- if rerr := v .v2 (resp , req ); rerr != nil {
59
- log .Printf ("%s %s %d %s %s" , req .Method , req .URL , rerr .Status , rerr .Code , rerr .Message )
59
+ func (r * registry ) root (resp http.ResponseWriter , req * http.Request ) {
60
+ if rerr := r .v2 (resp , req ); rerr != nil {
61
+ r . log .Printf ("%s %s %d %s %s" , req .Method , req .URL , rerr .Status , rerr .Code , rerr .Message )
60
62
rerr .Write (resp )
61
63
return
62
64
}
63
- log .Printf ("%s %s" , req .Method , req .URL )
65
+ r . log .Printf ("%s %s" , req .Method , req .URL )
64
66
}
65
67
66
- // New returns a handler which implements the docker registry protocol. It should be registered at the site root.
67
- func New () http.Handler {
68
- v := v {
68
+ // New returns a handler which implements the docker registry protocol.
69
+ // It should be registered at the site root.
70
+ func New (opts ... Option ) http.Handler {
71
+ r := & registry {
72
+ log : log .New (os .Stderr , "" , log .LstdFlags ),
69
73
blobs : blobs {
70
74
contents : map [string ][]byte {},
71
75
uploads : map [string ][]byte {},
@@ -74,5 +78,18 @@ func New() http.Handler {
74
78
manifests : map [string ]map [string ]manifest {},
75
79
},
76
80
}
77
- return http .HandlerFunc (v .root )
81
+ for _ , o := range opts {
82
+ o (r )
83
+ }
84
+ return http .HandlerFunc (r .root )
85
+ }
86
+
87
+ // Options describes the available options
88
+ // for creating the registry.
89
+ type Option func (r * registry )
90
+
91
+ func Logger (l * log.Logger ) Option {
92
+ return func (r * registry ) {
93
+ r .log = l
94
+ }
78
95
}
0 commit comments