-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathregistry.go
More file actions
92 lines (83 loc) · 2.38 KB
/
registry.go
File metadata and controls
92 lines (83 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package unregistry
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/distribution/distribution/v3/configuration"
"github.com/distribution/distribution/v3/registry/handlers"
// Register filesystem storage driver.
_ "github.com/distribution/distribution/v3/registry/storage/driver/filesystem"
"github.com/psviderski/unregistry/internal/storage/containerd"
"github.com/sirupsen/logrus"
)
// Registry represents a complete instance of the registry.
type Registry struct {
app *handlers.App
server *http.Server
}
// NewRegistry creates a new registry from the given configuration.
func NewRegistry(cfg Config) (*Registry, error) {
// Configure logging.
level, err := logrus.ParseLevel(cfg.LogLevel)
if err != nil {
return nil, fmt.Errorf("invalid log level: %w", err)
}
logrus.SetLevel(level)
switch cfg.LogFormatter {
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
case "text":
logrus.SetFormatter(&logrus.TextFormatter{})
default:
return nil, fmt.Errorf("invalid log formatter: '%s'; expected 'json' or 'text'", cfg.LogFormatter)
}
distConfig := &configuration.Configuration{
Storage: configuration.Storage{
"filesystem": configuration.Parameters{
"rootdirectory": "/tmp/registry", // Dummy storage driver
},
"maintenance": configuration.Parameters{
"uploadpurging": map[interface{}]interface{}{
"enabled": false,
},
},
},
Middleware: map[string][]configuration.Middleware{
"registry": {
{
Name: containerd.MiddlewareName,
Options: configuration.Parameters{
"namespace": cfg.ContainerdNamespace,
"sock": cfg.ContainerdSock,
},
},
},
},
}
app := handlers.NewApp(context.Background(), distConfig)
server := &http.Server{
Addr: cfg.Addr,
Handler: app,
}
return &Registry{
app: app,
server: server,
}, nil
}
// ListenAndServe starts the HTTP server for the registry.
func (r *Registry) ListenAndServe() error {
logrus.WithField("addr", r.server.Addr).Info("Starting registry server.")
if err := r.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}
// Shutdown gracefully shuts down the registry's HTTP server and application object.
func (r *Registry) Shutdown(ctx context.Context) error {
err := r.server.Shutdown(ctx)
if appErr := r.app.Shutdown(); appErr != nil {
err = errors.Join(err, appErr)
}
return err
}