Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow user to specify a custom endpoint #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/gorilla/websocket"
)

const (
API_URL = "https://mempool.space/api/v1/ws"
)

type MempoolInfo struct {
Size int `json:"size"`
Bytes int `json:"bytes"`
Expand Down Expand Up @@ -69,12 +66,22 @@ type Response struct {
}

type Client struct {
conn *websocket.Conn
conn *websocket.Conn
endpoint string
}

func New() (*Client, error) {
return NewWithEndpoint("mempool.space")
}

func NewWithEndpoint(endpoint string) (*Client, error) {
if !strings.HasSuffix(endpoint, "/") {
endpoint = endpoint + "/"
}
endpoint = endpoint + "api/v1/ws"

dialer := websocket.Dialer{}
conn, _, err := dialer.Dial("wss://mempool.space/ws", nil)
conn, _, err := dialer.Dial("wss://"+endpoint, nil)
if err != nil {
return nil, err
}
Expand All @@ -83,7 +90,10 @@ func New() (*Client, error) {
`{"action": "init"}`,
))

return &Client{conn: conn}, nil
return &Client{
conn: conn,
endpoint: endpoint,
}, nil
}

func (c *Client) Read() (*Response, error) {
Expand Down Expand Up @@ -114,8 +124,8 @@ func (f Fees) Len() int { return len(f) }
func (f Fees) Less(i, j int) bool { return f[i].FPV < f[j].FPV }
func (f Fees) Swap(i, j int) { f[i], f[j] = f[j], f[i] }

func Get(ctx context.Context, path string, v interface{}) error {
req, err := http.NewRequest("GET", API_URL+path, nil)
func (c *Client) Get(ctx context.Context, path string, v interface{}) error {
req, err := http.NewRequest("GET", "https://"+c.endpoint+path, nil)
if err != nil {
return err
}
Expand All @@ -135,17 +145,17 @@ func Get(ctx context.Context, path string, v interface{}) error {
return json.NewDecoder(r.Body).Decode(v)
}

func GetMempoolFee(ctx context.Context, n int) (Fees, error) {
func (c *Client) GetMempoolFee(ctx context.Context, n int) (Fees, error) {
var fees Fees
if err := Get(ctx, fmt.Sprintf("transactions/mempool/%d", n), &fees); err != nil {
if err := c.Get(ctx, fmt.Sprintf("/transactions/mempool/%d", n), &fees); err != nil {
return nil, err
}
return fees, nil
}

func GetBlockFee(ctx context.Context, n int) (Fees, error) {
func (c *Client) GetBlockFee(ctx context.Context, n int) (Fees, error) {
var fees Fees
if err := Get(ctx, fmt.Sprintf("transactions/height/%d", n), &fees); err != nil {
if err := c.Get(ctx, fmt.Sprintf("/transactions/height/%d", n), &fees); err != nil {
return nil, err
}
return fees, nil
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package main

import (
"flag"
"log"

"github.com/mempool/mempool-cli/ui"
)

func main() {
gui, err := ui.New()
endpoint := flag.String("endpoint", "mempool.space", "The API endpoint")
flag.Parse()

gui, err := ui.New(*endpoint)
if err != nil {
log.Fatal(err)
}
Expand Down
9 changes: 5 additions & 4 deletions ui/fee_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ type FeeDistribution struct {
cancelFn context.CancelFunc
fees client.Fees
title string
client *client.Client
}

func NewFeeDistribution(g *gocui.Gui) *FeeDistribution {
return &FeeDistribution{gui: g}
func NewFeeDistribution(g *gocui.Gui, c *client.Client) *FeeDistribution {
return &FeeDistribution{gui: g, client: c}
}

func (fd *FeeDistribution) newCtx() context.Context {
Expand All @@ -35,14 +36,14 @@ func (fd *FeeDistribution) newCtx() context.Context {

func (fd *FeeDistribution) FetchProjection(n int) error {
fn := func(ctx context.Context) (client.Fees, error) {
return client.GetMempoolFee(ctx, n)
return fd.client.GetMempoolFee(ctx, n)
}
return fd.fetch(fn)
}

func (fd *FeeDistribution) FetchBlock(n int) error {
fn := func(ctx context.Context) (client.Fees, error) {
return client.GetBlockFee(ctx, n)
return fd.client.GetBlockFee(ctx, n)
}
return fd.fetch(fn)
}
Expand Down
13 changes: 7 additions & 6 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ type UI struct {
state state
}

func New() (*UI, error) {
func New(endpoint string) (*UI, error) {
c, err := client.NewWithEndpoint(endpoint)
if err != nil {
log.Fatal(err)
}

gui, err := gocui.NewGui(gocui.Output256)
if err != nil {
return nil, err
}

ui := &UI{gui: gui}
ui.fd = NewFeeDistribution(gui)
ui.fd = NewFeeDistribution(gui, c)
ui.ts = NewTXSearch(gui)
gui.SetManager(ui, ui.fd, ui.ts)

Expand All @@ -54,10 +59,6 @@ func New() (*UI, error) {
gui.SelFgColor = gocui.ColorWhite

go func() {
c, err := client.New()
if err != nil {
log.Fatal(err)
}
if err := c.Want(); err != nil {
log.Fatal(err)
}
Expand Down