-
Notifications
You must be signed in to change notification settings - Fork 9
/
handler.go
130 lines (115 loc) · 4.51 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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 repositories
import (
"path"
"reflect"
"github.com/labstack/echo/v4"
"github.com/go-sigma/sigma/pkg/auth"
"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 repository handlers
type Handler interface {
// CreateRepository handles the post repository request
CreateRepository(c echo.Context) error
// UpdateRepository handles the put repository request
UpdateRepository(c echo.Context) error
// GetRepository handles the get repository request
GetRepository(c echo.Context) error
// ListRepositories handles the list repository request
ListRepositories(c echo.Context) error
// DeleteRepository handles the delete repository request
DeleteRepository(c echo.Context) error
}
var _ Handler = &handler{}
type handler struct {
authServiceFactory auth.AuthServiceFactory
auditServiceFactory dao.AuditServiceFactory
namespaceServiceFactory dao.NamespaceServiceFactory
repositoryServiceFactory dao.RepositoryServiceFactory
tagServiceFactory dao.TagServiceFactory
artifactServiceFactory dao.ArtifactServiceFactory
builderServiceFactory dao.BuilderServiceFactory
}
type inject struct {
authServiceFactory auth.AuthServiceFactory
auditServiceFactory dao.AuditServiceFactory
namespaceServiceFactory dao.NamespaceServiceFactory
repositoryServiceFactory dao.RepositoryServiceFactory
tagServiceFactory dao.TagServiceFactory
artifactServiceFactory dao.ArtifactServiceFactory
builderServiceFactory dao.BuilderServiceFactory
}
// handlerNew creates a new instance of the distribution handlers
func handlerNew(injects ...inject) Handler {
authServiceFactory := auth.NewAuthServiceFactory()
auditServiceFactory := dao.NewAuditServiceFactory()
namespaceServiceFactory := dao.NewNamespaceServiceFactory()
repositoryServiceFactory := dao.NewRepositoryServiceFactory()
tagServiceFactory := dao.NewTagServiceFactory()
artifactServiceFactory := dao.NewArtifactServiceFactory()
builderServiceFactory := dao.NewBuilderServiceFactory()
if len(injects) > 0 {
ij := injects[0]
if ij.authServiceFactory != nil {
authServiceFactory = ij.authServiceFactory
}
if ij.auditServiceFactory != nil {
auditServiceFactory = ij.auditServiceFactory
}
if ij.namespaceServiceFactory != nil {
namespaceServiceFactory = ij.namespaceServiceFactory
}
if ij.repositoryServiceFactory != nil {
repositoryServiceFactory = ij.repositoryServiceFactory
}
if ij.tagServiceFactory != nil {
tagServiceFactory = ij.tagServiceFactory
}
if ij.artifactServiceFactory != nil {
artifactServiceFactory = ij.artifactServiceFactory
}
if ij.builderServiceFactory != nil {
builderServiceFactory = ij.builderServiceFactory
}
}
return &handler{
authServiceFactory: authServiceFactory,
auditServiceFactory: auditServiceFactory,
namespaceServiceFactory: namespaceServiceFactory,
repositoryServiceFactory: repositoryServiceFactory,
tagServiceFactory: tagServiceFactory,
artifactServiceFactory: artifactServiceFactory,
builderServiceFactory: builderServiceFactory,
}
}
type factory struct{}
// Initialize initializes the namespace handlers
func (f factory) Initialize(e *echo.Echo) error {
repositoryGroup := e.Group(consts.APIV1+"/namespaces/:namespace_id/repositories", middlewares.AuthWithConfig(middlewares.AuthConfig{}))
repositoryHandler := handlerNew()
repositoryGroup.GET("/", repositoryHandler.ListRepositories)
repositoryGroup.POST("/", repositoryHandler.CreateRepository)
repositoryGroup.GET("/:id", repositoryHandler.GetRepository)
repositoryGroup.PUT("/:id", repositoryHandler.UpdateRepository)
repositoryGroup.DELETE("/:id", repositoryHandler.DeleteRepository)
return nil
}
func init() {
utils.PanicIf(handlers.RegisterRouterFactory(path.Base(reflect.TypeOf(factory{}).PkgPath()), &factory{}))
}