forked from moov-io/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
78 lines (66 loc) · 2.29 KB
/
webhook.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
// Copyright 2022 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/moov-io/base/log"
"github.com/moov-io/watchman/pkg/ofac"
"github.com/gorilla/mux"
)
type Customer struct {
ID string `json:"id"`
SDN *ofac.SDN `json:"sdn"`
Addresses []*ofac.Address `json:"addresses"`
Alts []*ofac.AlternateIdentity `json:"alts"`
Match float64 `json:"match,omitempty"`
}
type Company struct {
ID string `json:"id,omitempty"`
SDN *ofac.SDN `json:"sdn"`
Addresses []*ofac.Address `json:"addresses"`
Alts []*ofac.AlternateIdentity `json:"alts"`
Match float64 `json:"match,omitempty"`
}
func addWebhookRoute(logger log.Logger, r *mux.Router) {
r.Methods("POST").Path("/ofac").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bs, err := io.ReadAll(io.LimitReader(r.Body, 5*1024*1024))
if err != nil {
logger.Logf("problem reading request: %v", err)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf(`{"error": "%s"}`, err)))
}
if cust := readCustomer(bytes.NewReader(bs)); cust != nil {
logger.Logf("got webhook for Customer %s (%s) match=%.2f", cust.ID, cust.SDN.SDNName, cust.Match)
w.WriteHeader(http.StatusOK)
return
}
if company := readCompany(bytes.NewReader(bs)); company != nil {
logger.Logf("got webhook for Company %s (%s) match=%.2f", company.ID, company.SDN.SDNName, company.Match)
w.WriteHeader(http.StatusOK)
}
logger.Log("malformed webhook request")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error": "malformed JSON"}`))
})
}
func readCustomer(r io.Reader) *Customer {
var cust Customer
if err := json.NewDecoder(r).Decode(&cust); err != nil || cust.ID == "" {
return nil
}
return &cust
}
func readCompany(r io.Reader) *Company {
var company Company
if err := json.NewDecoder(r).Decode(&company); err != nil || company.ID == "" {
return nil
}
return &company
}