github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/plugin/organization/setup.go (about) 1 package organization 2 3 import ( 4 "errors" 5 "github.com/hellofresh/janus/pkg/plugin" 6 "github.com/hellofresh/janus/pkg/plugin/basic" 7 "github.com/hellofresh/janus/pkg/proxy" 8 "github.com/hellofresh/janus/pkg/router" 9 log "github.com/sirupsen/logrus" 10 ) 11 12 var ( 13 repo Repository 14 basicRepo basic.Repository 15 adminRouter router.Router 16 ) 17 18 // Organization represents the configuration to save the user and organization pair 19 type Organization struct { 20 Username string `json:"username"` 21 Organization string `json:"organization"` 22 Password string `json:"password"` 23 } 24 25 // OrganizationConfig represents the configuration to save the user and organization pair 26 type OrganizationConfig struct { 27 Organization string `json:"organization"` 28 Priority int `json:"priority"` 29 ContentPerDay int `json:"contentPerDay"` 30 Config map[string]interface{} `json:"config"` 31 } 32 33 type OrganizationUserAndConfig struct { 34 Username string `json:"username"` 35 Organization string `json:"organization"` 36 Password string `json:"password"` 37 Priority int `json:"priority"` 38 ContentPerDay int `json:"contentPerDay"` 39 Config map[string]interface{} `json:"config"` 40 } 41 42 func init() { 43 plugin.RegisterEventHook(plugin.StartupEvent, onStartup) 44 plugin.RegisterEventHook(plugin.AdminAPIStartupEvent, onAdminAPIStartup) 45 46 plugin.RegisterPlugin("organization_auth", plugin.Plugin{ 47 Action: setupOrganization, 48 }) 49 } 50 51 func setupOrganization(def *proxy.RouterDefinition, rawConfig plugin.Config) error { 52 if repo == nil { 53 return errors.New("the repository was not set by onStartup event") 54 } 55 56 var organization Organization 57 err := plugin.Decode(rawConfig, &organization) 58 if err != nil { 59 return err 60 } 61 62 def.AddMiddleware(NewOrganization(organization, repo)) 63 return nil 64 } 65 66 func onAdminAPIStartup(event interface{}) error { 67 e, ok := event.(plugin.OnAdminAPIStartup) 68 if !ok { 69 return errors.New("could not convert event to admin startup type") 70 } 71 72 adminRouter = e.Router 73 return nil 74 } 75 76 func onStartup(event interface{}) error { 77 var err error 78 79 e, ok := event.(plugin.OnStartup) 80 if !ok { 81 return errors.New("could not convert event to startup type") 82 } 83 84 if e.MongoDB != nil { 85 log.Debug("Mongo DB is set, using mongo repository for organization plugin") 86 log.Debug("unimplemented") 87 //repo, err = NewMongoRepository(e.MongoDB) 88 if err != nil { 89 return err 90 } 91 } else if e.Cassandra != nil { 92 log.Debugf("Cassandra is set, using cassandra repository for organization plugin") 93 94 repo, err = NewCassandraRepository(e.Cassandra) 95 if err != nil { 96 log.Errorf("error getting cassandra repo: %v", err) 97 return err 98 } 99 } else { 100 log.Debug("No DB set, using memory repository for organization plugin") 101 log.Debug("unimplemented") 102 //repo = NewInMemoryRepository() 103 } 104 105 if adminRouter == nil { 106 return ErrInvalidAdminRouter 107 } 108 109 handlers := NewHandler(repo) 110 group := adminRouter.Group("/credentials/organization_auth") 111 { 112 group.GET("/", handlers.Index()) 113 group.POST("/", handlers.Create()) 114 group.POST("/organization", handlers.CreateOrganization()) 115 group.GET("/{username}", handlers.Show()) 116 group.GET("/organization/{organization}", handlers.ShowOrganization()) 117 group.PUT("/{username}", handlers.Update()) 118 group.PUT("/organization/{organization}", handlers.UpdateOrganization()) 119 group.DELETE("/{username}", handlers.Delete()) 120 } 121 122 return nil 123 }