-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
55 lines (47 loc) · 1.21 KB
/
app.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
package main
import (
"encoding/json"
"errors"
"github.com/just1689/entity-sync/es/esq"
"github.com/just1689/entity-sync/es/shared"
"github.com/sirupsen/logrus"
"net/http"
"os"
"sig-worker/bus"
"sig-worker/domain"
)
var queueHandlers = map[string]func(m map[string]string){
domain.QueueOEMsV1: bus.ProcessAllOEMs,
domain.QueueOEMPagesV1: bus.ProcessOEMPage,
domain.QueueOEMPageResultsV1: bus.ProcessOEMPageResultUrl,
domain.QueueCarPageV1: bus.ProcessCarPage,
}
var okBytes, _ = json.Marshal(struct {
OK bool `json:"ok"`
}{
OK: true,
})
func main() {
queue := os.Getenv("queue")
if queue == "" {
panic(errors.New("could not find queue env var - empty"))
}
handler, ok := queueHandlers[queue]
if !ok || handler == nil {
panic(errors.New("could not find function to handle queue for " + queue))
}
builder := esq.BuildSubscriber(os.Getenv("nsqAddr"))
builder(shared.EntityType(queue), func(b []byte) {
m := map[string]string{}
err := json.Unmarshal(b, &m)
if err != nil {
logrus.Errorln(err)
return
}
handler(m)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(okBytes)
})
panic(http.ListenAndServe(os.Getenv("listenAddr"), nil))
}