From 5026b90b520f05f18dd1b8cf3c2b765ea5c1a274 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 12 Aug 2016 09:46:14 +0100 Subject: [PATCH] Create services with JSON by default --- src/github.com/matrix-org/go-neb/api.go | 10 +++------- src/github.com/matrix-org/go-neb/database/schema.go | 9 +-------- src/github.com/matrix-org/go-neb/types/types.go | 12 ++++++++---- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/api.go b/src/github.com/matrix-org/go-neb/api.go index baa6875..146a820 100644 --- a/src/github.com/matrix-org/go-neb/api.go +++ b/src/github.com/matrix-org/go-neb/api.go @@ -196,16 +196,12 @@ func (s *configureServiceHandler) OnIncomingRequest(req *http.Request) (interfac return nil, &errors.HTTPError{nil, `Must supply a "ID", a "Type" and a "Config"`, 400} } - service := types.CreateService(body.ID, body.Type) - if service == nil { - return nil, &errors.HTTPError{nil, "Unknown service type", 400} - } - - if err := json.Unmarshal(body.Config, service); err != nil { + service, err := types.CreateService(body.ID, body.Type, body.Config) + if err != nil { return nil, &errors.HTTPError{err, "Error parsing config JSON", 400} } - err := service.Register() + err = service.Register() if err != nil { return nil, &errors.HTTPError{err, "Failed to register service: " + err.Error(), 500} } diff --git a/src/github.com/matrix-org/go-neb/database/schema.go b/src/github.com/matrix-org/go-neb/database/schema.go index 8cd58c7..0eff3f1 100644 --- a/src/github.com/matrix-org/go-neb/database/schema.go +++ b/src/github.com/matrix-org/go-neb/database/schema.go @@ -135,14 +135,7 @@ func selectServiceTxn(txn *sql.Tx, serviceID string) (types.Service, error) { if err := row.Scan(&serviceType, &serviceJSON); err != nil { return nil, err } - service := types.CreateService(serviceID, serviceType) - if service == nil { - return nil, fmt.Errorf("Cannot create services of type %s", serviceType) - } - if err := json.Unmarshal(serviceJSON, service); err != nil { - return nil, err - } - return service, nil + return types.CreateService(serviceID, serviceType, serviceJSON) } const updateServiceSQL = ` diff --git a/src/github.com/matrix-org/go-neb/types/types.go b/src/github.com/matrix-org/go-neb/types/types.go index 49ca0ea..360cf8b 100644 --- a/src/github.com/matrix-org/go-neb/types/types.go +++ b/src/github.com/matrix-org/go-neb/types/types.go @@ -66,14 +66,18 @@ func RegisterService(factory func(string, string) Service) { } // CreateService creates a Service of the given type and serviceID. -// Returns nil if the Service couldn't be created. -func CreateService(serviceID, serviceType string) Service { +// Returns an error if the Service couldn't be created. +func CreateService(serviceID, serviceType string, serviceJSON []byte) (Service, error) { f := servicesByType[serviceType] if f == nil { - return nil + return nil, errors.New("Unknown service type: " + serviceType) } webhookEndpointURL := baseURL + "services/hooks/" + serviceID - return f(serviceID, webhookEndpointURL) + service := f(serviceID, webhookEndpointURL) + if err := json.Unmarshal(serviceJSON, service); err != nil { + return nil, err + } + return service, nil } // AuthRealm represents a place where a user can authenticate themselves.