-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
126 lines (107 loc) · 2.78 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"encoding/json"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/nats-io/nats.go"
"go.uber.org/zap"
"os"
"phaidra-connect/domain"
"text/template"
)
var conn *nats.Conn
var fns = template.FuncMap{
"minus": func(a, b int) int {
return a - b
},
}
type config struct {
natsHost string
museumHost string
phaidraHost string
phaidraUser string
phaidraPassword string
templateFile string
template *template.Template
log *zap.Logger
}
func main() {
var log *zap.Logger
var err error
if os.Getenv("RUNTIME_ENV") == "production" {
log, err = zap.NewProduction()
} else {
log, err = zap.NewDevelopment()
}
if err != nil {
panic(err)
}
defer func(log *zap.Logger) {
err := log.Sync()
if err != nil {
panic(err)
}
}(log)
conf := config{
natsHost: os.Getenv("NATS_HOST"),
museumHost: os.Getenv("MUSEUM_HOST"),
phaidraHost: os.Getenv("PHAIDRA_HOST"),
phaidraUser: os.Getenv("PHAIDRA_USERNAME"),
phaidraPassword: os.Getenv("PHAIDRA_PASSWORD"),
templateFile: os.Getenv("TEMPLATE_FILE"),
log: log,
}
tmpl, err := template.New(conf.templateFile).
Funcs(fns).
ParseFiles(conf.templateFile)
if err != nil {
log.Fatal("error parsing template file", zap.Error(err))
}
conf.template = tmpl
conn, err = nats.Connect(conf.natsHost)
if err != nil {
panic(err)
}
_, err = conn.Subscribe("museum.exhibit.created", func(m *nats.Msg) {
log.Info("received museum.exhibit.created event")
cloudEvent := new(cloudevents.Event)
err = json.Unmarshal(m.Data, &cloudEvent)
if err != nil {
log.Error("error unmarshalling cloud event", zap.Error(err))
return
}
data := new(map[string]interface{})
err = json.Unmarshal(cloudEvent.Data(), data)
if err != nil {
log.Error("error unmarshalling cloud event data", zap.Error(err))
return
}
log.Debug("creating phaidra object", zap.String("exhibitId", (*data)["exhibitId"].(string)))
exhibit, err := getExhibitById(conf, (*data)["exhibitId"].(string))
if err != nil {
log.Error("error getting exhibit by id", zap.Error(err))
return
}
meta := domain.PhaidraMetadata{
Title: exhibit.Meta.Title,
Description: exhibit.Meta.Description,
ResourceLink: conf.museumHost + "/exhibit/" + (*data)["exhibitId"].(string),
Author: domain.PhaidraMetadataAuthor{
FirstName: exhibit.Meta.AuthorFirstName,
LastName: exhibit.Meta.AuthorLastName,
},
Keywords: exhibit.Meta.Keywords,
OefosId: exhibit.Meta.OefosId,
OrgUnitId: exhibit.Meta.OrgUnitId,
}
err = createPhaidraObject(conf, meta)
if err != nil {
log.Error("error creating phaidra object", zap.Error(err))
return
}
log.Info("created phaidra object")
})
if err != nil {
panic(err)
}
select {}
}