-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
75 lines (65 loc) · 1.86 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/kunal768/trustwallet/clients"
"github.com/kunal768/trustwallet/parser"
)
func initEthClient() clients.EthereumClient {
client := http.Client{}
return clients.ClientService(&client)
}
func main() {
ethClient := initEthClient()
parserSvc := parser.NewParserService(ethClient)
ctx := context.Background()
// API endpoints
http.HandleFunc("/currentBlock", func(w http.ResponseWriter, r *http.Request) {
block, err := parserSvc.GetCurrentBlock(ctx)
if err != nil {
http.Error(w, "Failed to get current block", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Current block: %s", block)
})
http.HandleFunc("/subscribe", func(w http.ResponseWriter, r *http.Request) {
address := r.URL.Query().Get("address")
success := parserSvc.Subscribe(ctx, address)
if success {
fmt.Fprintf(w, "Subscribed to address: %s", address)
} else {
http.Error(w, "Address already subscribed", http.StatusConflict)
}
})
http.HandleFunc("/transactions", func(w http.ResponseWriter, r *http.Request) {
address := r.URL.Query().Get("address")
txns, err := parserSvc.GetTransactions(ctx, address)
if err != nil {
http.Error(w, "Failed to get transactions", http.StatusBadRequest)
return
}
json.NewEncoder(w).Encode(txns)
})
// Start a goroutine to periodically update subscriber transactions
exit := make(chan struct{}) // Channel to signal goroutine exit
go func() {
defer close(exit) // Ensure channel is closed upon completion
for {
select {
case <-ctx.Done(): // Exit if context is cancelled
return
case <-exit: // Exit if signal is received
return
default:
parserSvc.UpdateSubscriberTxns(ctx)
time.Sleep(time.Minute)
}
}
}()
http.ListenAndServe(":8080", nil)
// stop the goroutine when the server shuts down
<-exit
}