mirror of
https://github.com/status-im/status-go-monitor.git
synced 2025-01-12 20:34:16 +00:00
move rpc code to client.go
Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
3565768230
commit
dfb8d41a35
26
client.go
Normal file
26
client.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
type client struct {
|
||||||
|
rpcClient *rpc.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func newClient(url string) (*client, error) {
|
||||||
|
rpcClient, err := rpc.Dial(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &client{rpcClient}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *client) getPeers() (interface{}, error) {
|
||||||
|
var rval interface{}
|
||||||
|
err := c.rpcClient.Call(&rval, "admin_peers")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return rval, nil
|
||||||
|
}
|
18
main.go
18
main.go
@ -3,27 +3,27 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type rcpResp map[string]interface{}
|
type rcpResp map[string]interface{}
|
||||||
|
|
||||||
const serverAddr = "127.0.0.1"
|
const host = "127.0.0.1"
|
||||||
const serverPort = 8545
|
const port = 8545
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
addr := fmt.Sprintf("http://%s:%d", serverAddr, serverPort)
|
url := fmt.Sprintf("http://%s:%d", host, port)
|
||||||
c, err := rpc.DialHTTP(addr)
|
fmt.Println("Type :%t", url)
|
||||||
|
c, err := newClient(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to connec to RPC:", err)
|
log.Fatal("Failed to connec to RPC:", err)
|
||||||
}
|
}
|
||||||
fmt.Println("Connected: ", addr)
|
fmt.Println("Connected: ", url)
|
||||||
var rval interface{}
|
|
||||||
err = c.Call(&rval, "admin_peers")
|
peers, err := c.getPeers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to make RPC call:", err)
|
log.Fatal("Failed to make RPC call:", err)
|
||||||
}
|
}
|
||||||
pretty, err := json.MarshalIndent(rval, "", " ")
|
pretty, err := json.MarshalIndent(peers, "", " ")
|
||||||
fmt.Println("Response: ", string(pretty))
|
fmt.Println("Response: ", string(pretty))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user