/
handler.go
107 lines (95 loc) · 3.15 KB
/
handler.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2023 sigma
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package distribution
import (
"net/http"
"strings"
"github.com/labstack/echo/v4"
"github.com/go-sigma/sigma/pkg/auth"
"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/dal/dao"
"github.com/go-sigma/sigma/pkg/handlers/distribution"
"github.com/go-sigma/sigma/pkg/utils"
)
// Handler is the interface for the distribution handlers
type Handler interface {
// GetHealthy handles the get healthy request
GetHealthy(ctx echo.Context) error
// ListTags handles the list tags request
ListTags(ctx echo.Context) error
// ListRepositories handles the list repositories request
ListRepositories(ctx echo.Context) error
}
var _ Handler = &handler{}
type handler struct {
config *configs.Configuration
authServiceFactory auth.AuthServiceFactory
tagServiceFactory dao.TagServiceFactory
repositoryServiceFactory dao.RepositoryServiceFactory
}
type inject struct {
config *configs.Configuration
authServiceFactory auth.AuthServiceFactory
tagServiceFactory dao.TagServiceFactory
repositoryServiceFactory dao.RepositoryServiceFactory
}
// New creates a new instance of the distribution handlers
func handlerNew(injects ...inject) Handler {
config := configs.GetConfiguration()
authServiceFactory := auth.NewAuthServiceFactory()
tagServiceFactory := dao.NewTagServiceFactory()
repositoryServiceFactory := dao.NewRepositoryServiceFactory()
if len(injects) > 0 {
ij := injects[0]
if ij.config != nil {
config = ij.config
}
if ij.authServiceFactory != nil {
authServiceFactory = ij.authServiceFactory
}
if ij.repositoryServiceFactory != nil {
repositoryServiceFactory = ij.repositoryServiceFactory
}
if ij.tagServiceFactory != nil {
tagServiceFactory = ij.tagServiceFactory
}
}
return &handler{
config: config,
authServiceFactory: authServiceFactory,
repositoryServiceFactory: repositoryServiceFactory,
tagServiceFactory: tagServiceFactory,
}
}
type factory struct{}
// Initialize initializes the distribution manifest handlers
func (f factory) Initialize(c echo.Context) error {
method := c.Request().Method
uri := c.Request().RequestURI
baseHandler := handlerNew()
if method == http.MethodGet {
switch {
case uri == "/v2/":
return baseHandler.GetHealthy(c)
case uri == "/v2/_catalog":
return baseHandler.ListRepositories(c)
case strings.HasSuffix(uri, "/tags/list") && strings.HasPrefix(uri, "/v2/"):
return baseHandler.ListTags(c)
}
}
return distribution.ErrNext
}
func init() {
utils.PanicIf(distribution.RegisterRouterFactory(&factory{}, 1))
}