/
orchestrator.go
67 lines (53 loc) · 1.27 KB
/
orchestrator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package gsm
import (
"encoding/json"
"regexp"
)
import (
"github.com/streadway/amqp"
)
var ApplicationJsonRegex = regexp.MustCompile(`^application/json`)
type Instructions struct {
Rev string `json:"rev"`
RepoName string `json:"repo_name"`
RepoOwner string `json:"repo_owner"`
AuthToken string
}
type Orchestrator struct {
Configuration
}
func NewOrchestrator(config Configuration) *Orchestrator {
return &Orchestrator{
Configuration: config,
}
}
func (me *Orchestrator) Orchestrate(delivery amqp.Delivery) (*Instructions, error) {
defer func() {
err := delivery.Ack(false)
if err != nil {
// If this fails, and I don't exit, will I leak memory?
me.Logger.Printf("amqp - Error acking delivery %+v: %+v\n", delivery, err)
}
}()
var instructions *Instructions
var err error
switch {
case ApplicationJsonRegex.MatchString(delivery.ContentType):
instructions, err = me.parseJson(delivery.Body)
default:
instructions, err = me.parseJson(delivery.Body)
}
if err != nil {
return nil, err
}
instructions.AuthToken = me.AuthToken
return instructions, nil
}
func (me *Orchestrator) parseJson(rawBody []byte) (*Instructions, error) {
var ret *Instructions
err := json.Unmarshal(rawBody, ret)
if err != nil {
return nil, err
}
return ret, nil
}