generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
545095a
commit 51f105c
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Package main - send_op_return example | ||
*/ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
walletclient "github.com/bitcoin-sv/spv-wallet-go-client" | ||
"github.com/bitcoin-sv/spv-wallet-go-client/examples" | ||
) | ||
|
||
func main() { | ||
defer examples.HandlePanic() | ||
|
||
client := walletclient.NewWithAdminKey("http://localhost:3003/v1", examples.ExampleAdminKey) | ||
wh := walletclient.NewWebhook(client, "http://localhost:5005/notification", "", "") | ||
err := wh.Subscribe(context.Background()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
http.Handle("/notification", wh.HTTPHandler()) | ||
|
||
go func() { | ||
for { | ||
select { | ||
case event := <-wh.Channel: | ||
time.Sleep(100 * time.Millisecond) // simulate processing time | ||
fmt.Println(event) | ||
case <-context.Background().Done(): | ||
return | ||
} | ||
} | ||
}() | ||
|
||
http.ListenAndServe(":5005", nil) | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package walletclient | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const ( | ||
eventBufferLength = 100 | ||
) | ||
|
||
type Event any | ||
|
||
type Webhook struct { | ||
URL string | ||
TokenHeader string | ||
TokenValue string | ||
Channel chan Event | ||
|
||
client *WalletClient | ||
} | ||
|
||
func NewWebhook(client *WalletClient, url, tokenHeader, tokenValue string) *Webhook { | ||
return &Webhook{ | ||
URL: url, | ||
TokenHeader: tokenHeader, | ||
TokenValue: tokenValue, | ||
Channel: make(chan Event, eventBufferLength), | ||
client: client, | ||
} | ||
} | ||
|
||
func (w *Webhook) Subscribe(ctx context.Context) ResponseError { | ||
return w.client.AdminSubscribeWebhook(ctx, w.URL, w.TokenHeader, w.TokenValue) | ||
} | ||
|
||
func (w *Webhook) HTTPHandler() http.Handler { | ||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
var events []Event | ||
if err := json.NewDecoder(r.Body).Decode(&events); err != nil { | ||
http.Error(rw, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
fmt.Printf("Received: %v\n", events) | ||
for _, event := range events { | ||
select { | ||
case w.Channel <- event: | ||
// event sent | ||
case <-r.Context().Done(): | ||
// context cancelled | ||
return | ||
case <-time.After(1 * time.Second): | ||
// timeout, most probably the channel is full | ||
// TODO: log this | ||
} | ||
} | ||
rw.WriteHeader(http.StatusOK) | ||
}) | ||
} |