Skip to content

Commit

Permalink
Add Model interfaces for all types in model files and inject repo and…
Browse files Browse the repository at this point in the history
… dependent services into all services (warrant-dev#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
  • Loading branch information
kkajla12 authored Apr 4, 2023
1 parent 6bb3b77 commit 8c0dfcf
Show file tree
Hide file tree
Showing 72 changed files with 1,792 additions and 958 deletions.
116 changes: 104 additions & 12 deletions cmd/warrant/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
user "github.com/warrant-dev/warrant/pkg/authz/user"
warrant "github.com/warrant-dev/warrant/pkg/authz/warrant"
"github.com/warrant-dev/warrant/pkg/config"
wntContext "github.com/warrant-dev/warrant/pkg/context"
"github.com/warrant-dev/warrant/pkg/database"
"github.com/warrant-dev/warrant/pkg/event"
"github.com/warrant-dev/warrant/pkg/service"
Expand Down Expand Up @@ -141,23 +142,114 @@ func main() {
log.Fatal().Err(err).Msg("Could not initialize and connect to the configured eventstore. Shutting down.")
}

// Init event repo and service
eventRepository, err := event.NewRepository(svcEnv.EventDB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize EventRepository")
}

eventSvc := event.NewService(svcEnv, eventRepository)

// Init object type repo and service
objectTypeRepository, err := objecttype.NewRepository(svcEnv.DB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize ObjectTypeRepository")
}

objectTypeSvc := objecttype.NewService(svcEnv, objectTypeRepository, eventSvc)

// Init context repo and service
ctxRepository, err := wntContext.NewRepository(svcEnv.DB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize ContextRepository")
}

ctxSvc := wntContext.NewService(svcEnv, ctxRepository)

// Init warrant repo and service
warrantRepository, err := warrant.NewRepository(svcEnv.DB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize WarrantRepository")
}

warrantSvc := warrant.NewService(svcEnv, warrantRepository, eventSvc, objectTypeSvc, ctxSvc)

// Init check service
checkSvc := check.NewService(svcEnv, warrantRepository, ctxSvc, eventSvc, objectTypeSvc)

// Init object repo and service
objectRepository, err := object.NewRepository(svcEnv.DB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize ObjectRepository")
}

objectSvc := object.NewService(svcEnv, objectRepository, eventSvc, warrantSvc)

// Init feature repo and service
featureRepository, err := feature.NewRepository(svcEnv.DB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize FeatureRepository")
}

featureSvc := feature.NewService(&svcEnv, featureRepository, eventSvc, objectSvc)

// Init permission repo and service
permissionRepository, err := permission.NewRepository(svcEnv.DB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize RoleRepository")
}

permissionSvc := permission.NewService(&svcEnv, permissionRepository, eventSvc, objectSvc)

// Init pricing tier repo and service
pricingTierRepository, err := pricingtier.NewRepository(svcEnv.DB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize PricingTierRepository")
}

pricingTierSvc := pricingtier.NewService(&svcEnv, pricingTierRepository, eventSvc, objectSvc)

// Init role repo and service
roleRepository, err := role.NewRepository(svcEnv.DB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize RoleRepository")
}

roleSvc := role.NewService(&svcEnv, roleRepository, eventSvc, objectSvc)

// Init tenant repo and service
tenantRepository, err := tenant.NewRepository(svcEnv.DB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize TenantRepository")
}

tenantSvc := tenant.NewService(&svcEnv, tenantRepository, eventSvc, objectSvc)

// Init user repo and service
userRepository, err := user.NewRepository(svcEnv.DB())
if err != nil {
log.Fatal().Err(err).Msg("Could not initialize UserRepository")
}

userSvc := user.NewService(&svcEnv, userRepository, eventSvc, objectSvc)

svcs := []service.Service{
check.NewService(&svcEnv, &service.AuthInfo{}),
event.NewService(&svcEnv),
feature.NewService(&svcEnv),
object.NewService(&svcEnv),
objecttype.NewService(&svcEnv),
permission.NewService(&svcEnv),
pricingtier.NewService(&svcEnv),
role.NewService(&svcEnv),
tenant.NewService(&svcEnv),
user.NewService(&svcEnv),
warrant.NewService(&svcEnv),
checkSvc,
eventSvc,
featureSvc,
objectSvc,
objectTypeSvc,
permissionSvc,
pricingTierSvc,
roleSvc,
tenantSvc,
userSvc,
warrantSvc,
}

routes := make([]service.Route, 0)
for _, svc := range svcs {
routes = append(routes, svc.GetRoutes()...)
routes = append(routes, svc.Routes()...)
}

log.Debug().Msgf("Listening on port %d", config.Port)
Expand Down
10 changes: 5 additions & 5 deletions pkg/authz/check/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (
"github.com/warrant-dev/warrant/pkg/service"
)

func (svc CheckService) GetRoutes() []service.Route {
func (svc CheckService) Routes() []service.Route {
return []service.Route{
// Standard Authorization
{
Pattern: "/v2/authorize",
Method: "POST",
Handler: middleware.ChainMiddleware(
service.NewRouteHandler(svc.Env(), authorize),
service.NewRouteHandler(svc, authorize),
),
EnableSessionAuth: true,
},
}
}

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

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

checkResult, err := NewService(env, authInfo).CheckMany(r.Context(), &checkManySpec)
checkResult, err := svc.CheckMany(r.Context(), authInfo, &checkManySpec)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 8c0dfcf

Please sign in to comment.