forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.go
66 lines (58 loc) · 1.32 KB
/
index.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
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type HasuraEvent struct {
ID string `json:"id"`
Event `json:"event"`
Table `json:"table"`
Trigger `json:"trigger"`
}
type Event struct {
Op string `json:"op"`
Data `json:"data"`
}
type Data struct {
Old map[string]interface{} `json:"old"`
New map[string]interface{} `json:"new"`
}
type Table struct {
Name string `json:"name"`
Schema string `json:"schema"`
}
type Trigger struct {
ID string `json:"id"`
Name string `json:"name"`
}
type TriggerResponse struct {
Message string `json:"message"`
OldData map[string]interface{} `json:"oldData"`
NewData map[string]interface{} `json:"newData"`
}
func Handler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var event HasuraEvent
err := decoder.Decode(&event)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
response := TriggerResponse{
Message: fmt.Sprintf(
"got '%s' for '%s' operation on '%s' table in '%s' schema from '%s' trigger",
event.ID,
event.Event.Op,
event.Table.Name,
event.Table.Schema,
event.Trigger.Name,
),
OldData: event.Data.Old,
NewData: event.Data.New,
}
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}