-
Notifications
You must be signed in to change notification settings - Fork 9
/
handler.go
99 lines (87 loc) · 3.24 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
// 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 artifact
import (
"path"
"reflect"
"github.com/labstack/echo/v4"
"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/dal/dao"
"github.com/go-sigma/sigma/pkg/handlers"
"github.com/go-sigma/sigma/pkg/middlewares"
"github.com/go-sigma/sigma/pkg/utils"
)
// Handler is the interface for the artifact handlers
type Handler interface {
// ListArtifact handles the list artifact request
ListArtifact(c echo.Context) error
// GetArtifact handles the get artifact request
GetArtifact(c echo.Context) error
// DeleteArtifact handles the delete artifact request
DeleteArtifact(c echo.Context) error
}
var _ Handler = &handler{}
type handler struct {
namespaceServiceFactory dao.NamespaceServiceFactory
artifactServiceFactory dao.ArtifactServiceFactory
tagServiceFactory dao.TagServiceFactory
repositoryServiceFactory dao.RepositoryServiceFactory
}
type inject struct {
namespaceServiceFactory dao.NamespaceServiceFactory
artifactServiceFactory dao.ArtifactServiceFactory
tagServiceFactory dao.TagServiceFactory
repositoryServiceFactory dao.RepositoryServiceFactory
}
// handlerNew creates a new instance of the distribution handlers
func handlerNew(injects ...inject) Handler {
namespaceServiceFactory := dao.NewNamespaceServiceFactory()
tagServiceFactory := dao.NewTagServiceFactory()
artifactServiceFactory := dao.NewArtifactServiceFactory()
repositoryServiceFactory := dao.NewRepositoryServiceFactory()
if len(injects) > 0 {
ij := injects[0]
if ij.namespaceServiceFactory != nil {
namespaceServiceFactory = ij.namespaceServiceFactory
}
if ij.tagServiceFactory != nil {
tagServiceFactory = ij.tagServiceFactory
}
if ij.artifactServiceFactory != nil {
artifactServiceFactory = ij.artifactServiceFactory
}
if ij.repositoryServiceFactory != nil {
repositoryServiceFactory = ij.repositoryServiceFactory
}
}
return &handler{
namespaceServiceFactory: namespaceServiceFactory,
tagServiceFactory: tagServiceFactory,
artifactServiceFactory: artifactServiceFactory,
repositoryServiceFactory: repositoryServiceFactory,
}
}
type factory struct{}
// Initialize initializes the namespace handlers
func (f factory) Initialize(e *echo.Echo) error {
artifactGroup := e.Group(consts.APIV1+"/namespaces/:namespace/artifacts", middlewares.AuthWithConfig(middlewares.AuthConfig{}))
artifactHandler := handlerNew()
artifactGroup.GET("/", artifactHandler.ListArtifact)
artifactGroup.GET("/:digest", artifactHandler.GetArtifact)
artifactGroup.DELETE("/:digest", artifactHandler.DeleteArtifact)
return nil
}
func init() {
utils.PanicIf(handlers.RegisterRouterFactory(path.Base(reflect.TypeOf(factory{}).PkgPath()), &factory{}))
}