| 1 |
package middleware |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
|
| 8 |
storagedriver "github.com/docker/distribution/registry/storage/driver" |
| 9 |
storagemiddleware "github.com/docker/distribution/registry/storage/driver/middleware" |
| 10 |
) |
| 11 |
|
| 12 |
type redirectStorageMiddleware struct { |
| 13 |
storagedriver.StorageDriver |
| 14 |
scheme string |
| 15 |
host string |
| 16 |
} |
| 17 |
|
| 18 |
var _ storagedriver.StorageDriver = &redirectStorageMiddleware{} |
| 19 |
|
| 20 |
func newRedirectStorageMiddleware(sd storagedriver.StorageDriver, options map[string]interface{}) (storagedriver.StorageDriver, error) { |
| 21 |
o, ok := options["baseurl"] |
| 22 |
if !ok { |
| 23 |
return nil, fmt.Errorf("no baseurl provided") |
| 24 |
} |
| 25 |
b, ok := o.(string) |
| 26 |
if !ok { |
| 27 |
return nil, fmt.Errorf("baseurl must be a string") |
| 28 |
} |
| 29 |
u, err := url.Parse(b) |
| 30 |
if err != nil { |
| 31 |
return nil, fmt.Errorf("unable to parse redirect baseurl: %s", b) |
| 32 |
} |
| 33 |
if u.Scheme == "" { |
| 34 |
return nil, fmt.Errorf("no scheme specified for redirect baseurl") |
| 35 |
} |
| 36 |
if u.Host == "" { |
| 37 |
return nil, fmt.Errorf("no host specified for redirect baseurl") |
| 38 |
} |
| 39 |
|
| 40 |
return &redirectStorageMiddleware{StorageDriver: sd, scheme: u.Scheme, host: u.Host}, nil |
| 41 |
} |
| 42 |
|
| 43 |
func (r *redirectStorageMiddleware) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) { |
| 44 |
u := &url.URL{Scheme: r.scheme, Host: r.host, Path: path} |
| 45 |
return u.String(), nil |
| 46 |
} |
| 47 |
|
| 48 |
func init() { |
| 49 |
storagemiddleware.Register("redirect", storagemiddleware.InitFunc(newRedirectStorageMiddleware)) |
| 50 |
} |