Created
November 13, 2019 16:47
-
-
Save shal/7b54b26193196cba5dd0440f1367bb94 to your computer and use it in GitHub Desktop.
Simple example of EEX webhook consumer in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
) | |
// Webhook entity. | |
type Webhook struct { | |
SubscriptionID int `json:"subscription_id"` | |
Event string `json:"event"` | |
Data json.RawMessage `json:"data"` | |
} | |
// TransferWithFee contains informantion about the transfer. | |
type TransferWithFee struct { | |
Hash string `json:"hash"` | |
Sender string `json:"sender"` | |
Recipient string `json:"recipient"` | |
Amount string `json:"amount"` | |
Fee string `json:"fee"` | |
Timestamp time.Time `json:"timestamp"` | |
BlockNumber int `json:"block_number"` | |
Index int `json:"index"` | |
} | |
// Echo is simple example of webhook consumer. | |
func Echo(w http.ResponseWriter, r *http.Request) { | |
var webhook Webhook | |
if err := json.NewDecoder(r.Body).Decode(&webhook); err != nil { | |
fmt.Println(err) | |
} | |
if webhook.Event != "TransferWithFee" { | |
return | |
} | |
var transfer TransferWithFee | |
if err := json.Unmarshal(webhook.Data, &transfer); err != nil { | |
fmt.Println(err) | |
} | |
log.Printf("%s EEX received on %s address\n", transfer.Amount, transfer.Recipient) | |
} | |
func main() { | |
var port string | |
flag.StringVar(&port, "port", "8080", "Port of the server") | |
flag.Parse() | |
log.Printf("Listening on port %s...\n", port) | |
http.ListenAndServe(":"+port, http.HandlerFunc(Echo)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment