-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathplugin.go
More file actions
86 lines (74 loc) · 3.33 KB
/
Copy pathplugin.go
File metadata and controls
86 lines (74 loc) · 3.33 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
package credentials
import (
"net/http"
pkgrbac "github.com/ambient-code/platform/components/ambient-api-server/plugins/rbac"
"github.com/gorilla/mux"
"github.com/openshift-online/rh-trex-ai/pkg/api"
"github.com/openshift-online/rh-trex-ai/pkg/api/presenters"
"github.com/openshift-online/rh-trex-ai/pkg/auth"
"github.com/openshift-online/rh-trex-ai/pkg/controllers"
"github.com/openshift-online/rh-trex-ai/pkg/db"
"github.com/openshift-online/rh-trex-ai/pkg/environments"
"github.com/openshift-online/rh-trex-ai/pkg/registry"
pkgserver "github.com/openshift-online/rh-trex-ai/pkg/server"
"github.com/openshift-online/rh-trex-ai/plugins/events"
"github.com/openshift-online/rh-trex-ai/plugins/generic"
)
type ServiceLocator func() CredentialService
func NewServiceLocator(env *environments.Env) ServiceLocator {
return func() CredentialService {
return NewCredentialService(
db.NewAdvisoryLockFactory(env.Database.SessionFactory),
NewCredentialDao(&env.Database.SessionFactory),
events.Service(&env.Services),
)
}
}
func Service(s *environments.Services) CredentialService {
if s == nil {
return nil
}
if obj := s.GetService("Credentials"); obj != nil {
locator := obj.(ServiceLocator)
return locator()
}
return nil
}
func init() {
registry.RegisterService("Credentials", func(env interface{}) interface{} {
return NewServiceLocator(env.(*environments.Env))
})
pkgserver.RegisterRoutes("credentials", func(apiV1Router *mux.Router, services pkgserver.ServicesInterface, authMiddleware environments.JWTMiddleware, authzMiddleware auth.AuthorizationMiddleware) {
envServices := services.(*environments.Services)
if dbAuthz := pkgrbac.Middleware(envServices); dbAuthz != nil {
authzMiddleware = dbAuthz
}
credentialHandler := NewCredentialHandler(Service(envServices), generic.Service(envServices))
credentialsRouter := apiV1Router.PathPrefix("/credentials").Subrouter()
credentialsRouter.HandleFunc("", credentialHandler.List).Methods(http.MethodGet)
credentialsRouter.HandleFunc("/{id}", credentialHandler.Get).Methods(http.MethodGet)
credentialsRouter.HandleFunc("/{id}/token", credentialHandler.GetToken).Methods(http.MethodGet)
credentialsRouter.HandleFunc("", credentialHandler.Create).Methods(http.MethodPost)
credentialsRouter.HandleFunc("/{id}", credentialHandler.Patch).Methods(http.MethodPatch)
credentialsRouter.HandleFunc("/{id}", credentialHandler.Delete).Methods(http.MethodDelete)
credentialsRouter.Use(authMiddleware.AuthenticateAccountJWT)
credentialsRouter.Use(authzMiddleware.AuthorizeApi)
})
pkgserver.RegisterController("Credentials", func(manager *controllers.KindControllerManager, services pkgserver.ServicesInterface) {
credentialServices := Service(services.(*environments.Services))
manager.Add(&controllers.ControllerConfig{
Source: "Credentials",
Handlers: map[api.EventType][]controllers.ControllerHandlerFunc{
api.CreateEventType: {credentialServices.OnUpsert},
api.UpdateEventType: {credentialServices.OnUpsert},
api.DeleteEventType: {credentialServices.OnDelete},
},
})
})
presenters.RegisterPath(Credential{}, "credentials")
presenters.RegisterPath(&Credential{}, "credentials")
presenters.RegisterKind(Credential{}, "Credential")
presenters.RegisterKind(&Credential{}, "Credential")
db.RegisterMigration(migration())
db.RegisterMigration(rolesMigration())
}