...
1 package middleware
2
3 import (
4 "context"
5 "fmt"
6
7 "github.com/docker/distribution"
8 )
9
10
11
12 type InitFunc func(ctx context.Context, repository distribution.Repository, options map[string]interface{}) (distribution.Repository, error)
13
14 var middlewares map[string]InitFunc
15
16
17
18 func Register(name string, initFunc InitFunc) error {
19 if middlewares == nil {
20 middlewares = make(map[string]InitFunc)
21 }
22 if _, exists := middlewares[name]; exists {
23 return fmt.Errorf("name already registered: %s", name)
24 }
25
26 middlewares[name] = initFunc
27
28 return nil
29 }
30
31
32 func Get(ctx context.Context, name string, options map[string]interface{}, repository distribution.Repository) (distribution.Repository, error) {
33 if middlewares != nil {
34 if initFunc, exists := middlewares[name]; exists {
35 return initFunc(ctx, repository, options)
36 }
37 }
38
39 return nil, fmt.Errorf("no repository middleware registered with name: %s", name)
40 }
41
View as plain text