-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.go
201 lines (176 loc) · 5.8 KB
/
router.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package http
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"github.com/breez/notify/config"
"github.com/breez/notify/notify"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
type MobilePushWebHookQuery struct {
Platform string `form:"platform" binding:"required,oneof=ios android"`
Token string `form:"token" binding:"required"`
AppData *string `form:"app_data"`
}
type NotificationConvertible interface {
ToNotification(query *MobilePushWebHookQuery) *notify.Notification
}
type LnurlPayInfoPayload struct {
Template string `json:"template" binding:"required,eq=lnurlpay_info"`
Data struct {
CallbackURL string `json:"callback_url" binding:"required"`
ReplyURL string `json:"reply_url" binding:"required"`
} `json:"data"`
}
func (p *LnurlPayInfoPayload) ToNotification(query *MobilePushWebHookQuery) *notify.Notification {
return ¬ify.Notification{
Template: p.Template,
DisplayMessage: "Receiving payment",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]interface{}{
"callback_url": p.Data.CallbackURL,
"reply_url": p.Data.ReplyURL,
},
}
}
type LnurlPayInvoicePayload struct {
Template string `json:"template" binding:"required,eq=lnurlpay_invoice"`
Data struct {
Amount uint64 `json:"amount" binding:"required,min=1"`
ReplyURL string `json:"reply_url" binding:"required"`
} `json:"data"`
}
func (p *LnurlPayInvoicePayload) ToNotification(query *MobilePushWebHookQuery) *notify.Notification {
return ¬ify.Notification{
Template: p.Template,
DisplayMessage: "Invoice requested",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]interface{}{
"amount": p.Data.Amount,
"reply_url": p.Data.ReplyURL,
},
}
}
type PaymentReceivedPayload struct {
Template string `json:"template" binding:"required,eq=payment_received"`
Data struct {
PaymentHash string `json:"payment_hash" binding:"required"`
} `json:"data"`
}
func (p *PaymentReceivedPayload) ToNotification(query *MobilePushWebHookQuery) *notify.Notification {
return ¬ify.Notification{
Template: p.Template,
DisplayMessage: "Incoming payment",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]interface{}{"payment_hash": p.Data.PaymentHash},
}
}
type TxConfirmedPayload struct {
Template string `json:"template" binding:"required,eq=tx_confirmed"`
Data struct {
TxID string `json:"tx_id" binding:"required"`
} `json:"data"`
}
func (p *TxConfirmedPayload) ToNotification(query *MobilePushWebHookQuery) *notify.Notification {
return ¬ify.Notification{
Template: p.Template,
DisplayMessage: "Transaction confirmed",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]interface{}{"tx_id": p.Data.TxID},
}
}
type AddressTxsConfirmedPayload struct {
Template string `json:"template" binding:"required,eq=address_txs_confirmed"`
Data struct {
Address string `json:"address" binding:"required"`
} `json:"data"`
}
func (p *AddressTxsConfirmedPayload) ToNotification(query *MobilePushWebHookQuery) *notify.Notification {
return ¬ify.Notification{
Template: p.Template,
DisplayMessage: "Address transactions confirmed",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]interface{}{"address": p.Data.Address},
}
}
type SwapUpdatedPayload struct {
Event string `json:"event" binding:"required,eq=swap.update"`
Data struct {
Id string `json:"id" binding:"required"`
Status string `json:"status" binding:"required"`
} `json:"data"`
}
func (p *SwapUpdatedPayload) ToNotification(query *MobilePushWebHookQuery) *notify.Notification {
return ¬ify.Notification{
Template: notify.NOTIFICATION_SWAP_UPDATED,
DisplayMessage: "Swap updated",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]interface{}{"id": p.Data.Id, "status": p.Data.Status},
}
}
func Run(notifier *notify.Notifier, config *config.HTTPConfig) error {
r := setupRouter(notifier)
r.SetTrustedProxies(nil)
return r.Run(config.Address)
}
func setupRouter(notifier *notify.Notifier) *gin.Engine {
r := gin.Default()
router := r.Group("api/v1")
addWebHookRouter(router, notifier)
return r
}
func addWebHookRouter(r *gin.RouterGroup, notifier *notify.Notifier) {
r.POST("/notify", func(c *gin.Context) {
body, _ := io.ReadAll(c.Request.Body)
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
// Make sure the query string fits the mobile push structure
var query MobilePushWebHookQuery
if err := c.ShouldBindQuery(&query); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
// Find a matching notification payload
payloads := []NotificationConvertible{
&PaymentReceivedPayload{},
&TxConfirmedPayload{},
&AddressTxsConfirmedPayload{},
&LnurlPayInfoPayload{},
&LnurlPayInvoicePayload{},
&SwapUpdatedPayload{},
}
var validPayload NotificationConvertible
for _, p := range payloads {
if err := c.ShouldBindBodyWith(p, binding.JSON); err != nil {
continue
}
validPayload = p
break
}
if validPayload == nil {
log.Printf("invalid payload, body: %s", body)
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("unsupported payload, body: %s", body))
return
}
if err := notifier.Notify(c, validPayload.ToNotification(&query)); err != nil {
log.Printf("failed to notify, query: %v, error: %v", query, err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Status(http.StatusOK)
})
}