-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (94 loc) · 2.21 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"runtime"
ecc "github.com/ernestio/ernest-config-client"
"github.com/nats-io/go-nats"
)
// Transition is used to map a transitions arc, along with any extra information
type Transition struct {
From string
To string
Extra string `json:"extra,omitempty"`
}
func getTransitions(path string) []Transition {
var keys []Transition
file, err := os.Open(path)
log.Printf("Reading config from: %s\n", path)
if err != nil {
log.Panic("error:", err)
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&keys)
if err != nil {
log.Println("Config file is invalid")
log.Panic("error:", err)
}
fmt.Printf("%#v\n", keys)
return keys
}
func manage(n *nats.Conn, m *nats.Msg, ts []Transition) {
var current Transition
for _, t := range ts {
if t.From == m.Subject {
current = t
}
}
fmt.Printf("%#v", current)
if ¤t != nil && current.To != "" && current.From != "" {
var x map[string]interface{}
err := json.Unmarshal(m.Data, &x)
if err != nil {
return
}
x["type"] = current.To
if current.Extra != "" {
var y map[string]interface{}
err = json.Unmarshal([]byte(current.Extra), &y)
if err != nil {
panic(err)
}
fmt.Printf("%#v", y)
for k, v := range y {
x[k] = v
}
}
b, err := json.Marshal(x)
if err != nil {
fmt.Println("error:", err)
}
log.Println("In")
err = n.Publish(current.To, b)
if err != nil {
log.Println(err)
}
}
}
func main() {
trnPath := flag.String("transitions", "transitions/default.json", "path to json transitions")
flag.Parse()
ts := getTransitions(*trnPath)
n := ecc.NewConfig(os.Getenv("NATS_URI")).Nats()
h := handler{Nats: n}
subscriptions := []string{"*", "*.*", "*.*.*"}
for _, s := range subscriptions {
_, err := n.Subscribe(s, func(m *nats.Msg) {
manage(n, m, ts)
})
if err != nil {
log.Fatal(err)
}
}
_, err := n.Subscribe("federation-fake.auth", h.federationAuth)
if err != nil {
log.Fatal(err)
}
runtime.Goexit()
}