Skip to content

Commit 8c0dfcf

Browse files
authored
Add Model interfaces for all types in model files and inject repo and dependent services into all services (#56)
* Add Model interfaces for all types in model files and inject repo and dependent services into all services * Update all repositories to convert between local model struct and the Model interface * Remove unused Context slice from Warrant model
1 parent 6bb3b77 commit 8c0dfcf

72 files changed

Lines changed: 1792 additions & 958 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/warrant/main.go

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
user "github.com/warrant-dev/warrant/pkg/authz/user"
1919
warrant "github.com/warrant-dev/warrant/pkg/authz/warrant"
2020
"github.com/warrant-dev/warrant/pkg/config"
21+
wntContext "github.com/warrant-dev/warrant/pkg/context"
2122
"github.com/warrant-dev/warrant/pkg/database"
2223
"github.com/warrant-dev/warrant/pkg/event"
2324
"github.com/warrant-dev/warrant/pkg/service"
@@ -141,23 +142,114 @@ func main() {
141142
log.Fatal().Err(err).Msg("Could not initialize and connect to the configured eventstore. Shutting down.")
142143
}
143144

145+
// Init event repo and service
146+
eventRepository, err := event.NewRepository(svcEnv.EventDB())
147+
if err != nil {
148+
log.Fatal().Err(err).Msg("Could not initialize EventRepository")
149+
}
150+
151+
eventSvc := event.NewService(svcEnv, eventRepository)
152+
153+
// Init object type repo and service
154+
objectTypeRepository, err := objecttype.NewRepository(svcEnv.DB())
155+
if err != nil {
156+
log.Fatal().Err(err).Msg("Could not initialize ObjectTypeRepository")
157+
}
158+
159+
objectTypeSvc := objecttype.NewService(svcEnv, objectTypeRepository, eventSvc)
160+
161+
// Init context repo and service
162+
ctxRepository, err := wntContext.NewRepository(svcEnv.DB())
163+
if err != nil {
164+
log.Fatal().Err(err).Msg("Could not initialize ContextRepository")
165+
}
166+
167+
ctxSvc := wntContext.NewService(svcEnv, ctxRepository)
168+
169+
// Init warrant repo and service
170+
warrantRepository, err := warrant.NewRepository(svcEnv.DB())
171+
if err != nil {
172+
log.Fatal().Err(err).Msg("Could not initialize WarrantRepository")
173+
}
174+
175+
warrantSvc := warrant.NewService(svcEnv, warrantRepository, eventSvc, objectTypeSvc, ctxSvc)
176+
177+
// Init check service
178+
checkSvc := check.NewService(svcEnv, warrantRepository, ctxSvc, eventSvc, objectTypeSvc)
179+
180+
// Init object repo and service
181+
objectRepository, err := object.NewRepository(svcEnv.DB())
182+
if err != nil {
183+
log.Fatal().Err(err).Msg("Could not initialize ObjectRepository")
184+
}
185+
186+
objectSvc := object.NewService(svcEnv, objectRepository, eventSvc, warrantSvc)
187+
188+
// Init feature repo and service
189+
featureRepository, err := feature.NewRepository(svcEnv.DB())
190+
if err != nil {
191+
log.Fatal().Err(err).Msg("Could not initialize FeatureRepository")
192+
}
193+
194+
featureSvc := feature.NewService(&svcEnv, featureRepository, eventSvc, objectSvc)
195+
196+
// Init permission repo and service
197+
permissionRepository, err := permission.NewRepository(svcEnv.DB())
198+
if err != nil {
199+
log.Fatal().Err(err).Msg("Could not initialize RoleRepository")
200+
}
201+
202+
permissionSvc := permission.NewService(&svcEnv, permissionRepository, eventSvc, objectSvc)
203+
204+
// Init pricing tier repo and service
205+
pricingTierRepository, err := pricingtier.NewRepository(svcEnv.DB())
206+
if err != nil {
207+
log.Fatal().Err(err).Msg("Could not initialize PricingTierRepository")
208+
}
209+
210+
pricingTierSvc := pricingtier.NewService(&svcEnv, pricingTierRepository, eventSvc, objectSvc)
211+
212+
// Init role repo and service
213+
roleRepository, err := role.NewRepository(svcEnv.DB())
214+
if err != nil {
215+
log.Fatal().Err(err).Msg("Could not initialize RoleRepository")
216+
}
217+
218+
roleSvc := role.NewService(&svcEnv, roleRepository, eventSvc, objectSvc)
219+
220+
// Init tenant repo and service
221+
tenantRepository, err := tenant.NewRepository(svcEnv.DB())
222+
if err != nil {
223+
log.Fatal().Err(err).Msg("Could not initialize TenantRepository")
224+
}
225+
226+
tenantSvc := tenant.NewService(&svcEnv, tenantRepository, eventSvc, objectSvc)
227+
228+
// Init user repo and service
229+
userRepository, err := user.NewRepository(svcEnv.DB())
230+
if err != nil {
231+
log.Fatal().Err(err).Msg("Could not initialize UserRepository")
232+
}
233+
234+
userSvc := user.NewService(&svcEnv, userRepository, eventSvc, objectSvc)
235+
144236
svcs := []service.Service{
145-
check.NewService(&svcEnv, &service.AuthInfo{}),
146-
event.NewService(&svcEnv),
147-
feature.NewService(&svcEnv),
148-
object.NewService(&svcEnv),
149-
objecttype.NewService(&svcEnv),
150-
permission.NewService(&svcEnv),
151-
pricingtier.NewService(&svcEnv),
152-
role.NewService(&svcEnv),
153-
tenant.NewService(&svcEnv),
154-
user.NewService(&svcEnv),
155-
warrant.NewService(&svcEnv),
237+
checkSvc,
238+
eventSvc,
239+
featureSvc,
240+
objectSvc,
241+
objectTypeSvc,
242+
permissionSvc,
243+
pricingTierSvc,
244+
roleSvc,
245+
tenantSvc,
246+
userSvc,
247+
warrantSvc,
156248
}
157249

158250
routes := make([]service.Route, 0)
159251
for _, svc := range svcs {
160-
routes = append(routes, svc.GetRoutes()...)
252+
routes = append(routes, svc.Routes()...)
161253
}
162254

163255
log.Debug().Msgf("Listening on port %d", config.Port)

pkg/authz/check/handlers.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ import (
99
"github.com/warrant-dev/warrant/pkg/service"
1010
)
1111

12-
func (svc CheckService) GetRoutes() []service.Route {
12+
func (svc CheckService) Routes() []service.Route {
1313
return []service.Route{
1414
// Standard Authorization
1515
{
1616
Pattern: "/v2/authorize",
1717
Method: "POST",
1818
Handler: middleware.ChainMiddleware(
19-
service.NewRouteHandler(svc.Env(), authorize),
19+
service.NewRouteHandler(svc, authorize),
2020
),
2121
EnableSessionAuth: true,
2222
},
2323
}
2424
}
2525

26-
func authorize(env service.Env, w http.ResponseWriter, r *http.Request) error {
26+
func authorize(svc CheckService, w http.ResponseWriter, r *http.Request) error {
2727
authInfo := service.GetAuthInfoFromRequestContext(r.Context())
2828
if authInfo != nil && authInfo.UserId != "" {
2929
var sessionCheckManySpec SessionCheckManySpec
@@ -53,7 +53,7 @@ func authorize(env service.Env, w http.ResponseWriter, r *http.Request) error {
5353
Debug: sessionCheckManySpec.Debug,
5454
}
5555

56-
checkResult, err := NewService(env, authInfo).CheckMany(r.Context(), &checkManySpec)
56+
checkResult, err := svc.CheckMany(r.Context(), authInfo, &checkManySpec)
5757
if err != nil {
5858
return err
5959
}
@@ -68,7 +68,7 @@ func authorize(env service.Env, w http.ResponseWriter, r *http.Request) error {
6868
return err
6969
}
7070

71-
checkResult, err := NewService(env, authInfo).CheckMany(r.Context(), &checkManySpec)
71+
checkResult, err := svc.CheckMany(r.Context(), authInfo, &checkManySpec)
7272
if err != nil {
7373
return err
7474
}

0 commit comments

Comments
 (0)