Browse Source

Create services with JSON by default

kegan/create-with-json
Kegan Dougal 8 years ago
parent
commit
5026b90b52
  1. 10
      src/github.com/matrix-org/go-neb/api.go
  2. 9
      src/github.com/matrix-org/go-neb/database/schema.go
  3. 12
      src/github.com/matrix-org/go-neb/types/types.go

10
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} 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} return nil, &errors.HTTPError{err, "Error parsing config JSON", 400}
} }
err := service.Register()
err = service.Register()
if err != nil { if err != nil {
return nil, &errors.HTTPError{err, "Failed to register service: " + err.Error(), 500} return nil, &errors.HTTPError{err, "Failed to register service: " + err.Error(), 500}
} }

9
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 { if err := row.Scan(&serviceType, &serviceJSON); err != nil {
return nil, err 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 = ` const updateServiceSQL = `

12
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. // 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] f := servicesByType[serviceType]
if f == nil { if f == nil {
return nil
return nil, errors.New("Unknown service type: " + serviceType)
} }
webhookEndpointURL := baseURL + "services/hooks/" + serviceID 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. // AuthRealm represents a place where a user can authenticate themselves.

Loading…
Cancel
Save