func (s *Service) RegisterResource(res kit.Resource) { if res.Backend() == nil { if s.defaultBackend == nil { s.registry.Logger().Panic("Registering resource without backend, but no default backend set on resources.Service") } s.defaultBackend.RegisterModel(res.Model()) res.SetBackend(s.defaultBackend) } if res.Collection() == "" { s.registry.Logger().Panic("Registering resource without a model type") } s.resources[res.Collection()] = res }
func (a *App) RegisterResource(res kit.Resource) { if res.Backend() == nil { if a.registry.DefaultBackend() == nil { a.Logger().Panic("Registering resource without backend, but no default backend set.") } // Set backend. res.SetBackend(a.registry.DefaultBackend()) } if res.Registry() == nil { res.SetRegistry(a.registry) } res.SetDebug(a.Debug()) // Allow a resource to register custom http routes and methods. if res.Hooks() != nil { // Handle http routes. if resRoutes, ok := res.Hooks().(resources.ApiHttpRoutes); ok { for _, route := range resRoutes.HttpRoutes(res) { a.RegisterHttpHandler(route.Method(), route.Route(), route.Handler()) } } // Handle methods. if resMethods, ok := res.Hooks().(resources.MethodsHook); ok { for _, method := range resMethods.Methods(res) { a.RegisterMethod(method) } } } a.registry.AddResource(res) }