Skip to content

Commit 7b769d5

Browse files
authored
Merge pull request #14 from wpaulino/looprpc-rest
looprpc+cmd/loopd: add client REST definitions and REST proxy
2 parents d4e98ee + eb8277b commit 7b769d5

File tree

11 files changed

+691
-153
lines changed

11 files changed

+691
-153
lines changed

cmd/loop/loopout.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ var loopOutCommand = cli.Command{
1414
ArgsUsage: "amt [addr]",
1515
Description: `
1616
Attempts loop out the target amount into either the backing lnd's
17-
wallet, or a targeted address.
18-
19-
The amount is to be specified in satoshis.
17+
wallet, or a targeted address.
2018
21-
Optionally a BASE58/bech32 encoded bitcoin destination address may be
19+
The amount is to be specified in satoshis.
20+
21+
Optionally a BASE58/bech32 encoded bitcoin destination address may be
2222
specified. If not specified, a new wallet address will be generated.`,
2323
Flags: []cli.Flag{
2424
cli.Uint64Flag{
@@ -55,12 +55,10 @@ func loopOut(ctx *cli.Context) error {
5555
}
5656
defer cleanup()
5757

58-
quote, err := client.GetLoopOutQuote(
59-
context.Background(),
60-
&looprpc.QuoteRequest{
61-
Amt: int64(amt),
62-
},
63-
)
58+
quoteReq := &looprpc.QuoteRequest{
59+
Amt: int64(amt),
60+
}
61+
quote, err := client.LoopOutQuote(context.Background(), quoteReq)
6462
if err != nil {
6563
return err
6664
}

cmd/loop/terms.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
var termsCommand = cli.Command{
1515
Name: "terms",
16-
Usage: "show current server swap terms",
16+
Usage: "Display the current swap terms imposed by the server.",
1717
Action: terms,
1818
}
1919

@@ -24,9 +24,8 @@ func terms(ctx *cli.Context) error {
2424
}
2525
defer cleanup()
2626

27-
terms, err := client.GetLoopOutTerms(
28-
context.Background(), &looprpc.TermsRequest{},
29-
)
27+
req := &looprpc.TermsRequest{}
28+
terms, err := client.LoopOutTerms(context.Background(), req)
3029
if err != nil {
3130
return err
3231
}

cmd/loopd/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ type config struct {
1212
Insecure bool `long:"insecure" description:"disable tls"`
1313
Network string `long:"network" description:"network to run on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"`
1414
SwapServer string `long:"swapserver" description:"swap server address host:port"`
15-
Listen string `long:"listen" description:"address to listen on for rpc lcients"`
15+
RPCListen string `long:"rpclisten" description:"Address to listen on for gRPC clients"`
16+
RESTListen string `long:"restlisten" description:"Address to listen on for REST clients"`
1617

1718
Lnd *lndConfig `group:"lnd" namespace:"lnd"`
1819

@@ -22,7 +23,8 @@ type config struct {
2223
var defaultConfig = config{
2324
Network: "mainnet",
2425
SwapServer: "swap.lightning.today:11009",
25-
Listen: "localhost:11010",
26+
RPCListen: "localhost:11010",
27+
RESTListen: "localhost:8081",
2628
Insecure: false,
2729
Lnd: &lndConfig{
2830
Host: "localhost:10009",

cmd/loopd/daemon.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"net/http"
78
"os"
89
"os/signal"
910
"runtime/pprof"
1011
"sync"
1112
"time"
1213

14+
proxy "github.com/grpc-ecosystem/grpc-gateway/runtime"
1315
"github.com/lightninglabs/loop"
1416
"github.com/lightninglabs/loop/looprpc"
1517
"google.golang.org/grpc"
@@ -59,15 +61,38 @@ func daemon(config *config) error {
5961
grpcServer := grpc.NewServer(serverOpts...)
6062
looprpc.RegisterSwapClientServer(grpcServer, &server)
6163

62-
// Next, Start the gRPC server listening for HTTP/2 connections.
63-
logger.Infof("Starting RPC listener")
64-
lis, err := net.Listen("tcp", config.Listen)
64+
// Next, start the gRPC server listening for HTTP/2 connections.
65+
logger.Infof("Starting gRPC listener")
66+
grpcListener, err := net.Listen("tcp", config.RPCListen)
6567
if err != nil {
6668
return fmt.Errorf("RPC server unable to listen on %s",
67-
config.Listen)
69+
config.RPCListen)
6870

6971
}
70-
defer lis.Close()
72+
defer grpcListener.Close()
73+
74+
// We'll also create and start an accompanying proxy to serve clients
75+
// through REST.
76+
ctx, cancel := context.WithCancel(context.Background())
77+
defer cancel()
78+
mux := proxy.NewServeMux()
79+
proxyOpts := []grpc.DialOption{grpc.WithInsecure()}
80+
err = looprpc.RegisterSwapClientHandlerFromEndpoint(
81+
ctx, mux, config.RPCListen, proxyOpts,
82+
)
83+
if err != nil {
84+
return err
85+
}
86+
87+
logger.Infof("Starting REST proxy listener")
88+
restListener, err := net.Listen("tcp", config.RESTListen)
89+
if err != nil {
90+
return fmt.Errorf("REST proxy unable to listen on %s",
91+
config.RESTListen)
92+
}
93+
defer restListener.Close()
94+
proxy := &http.Server{Handler: mux}
95+
go proxy.Serve(restListener)
7196

7297
statusChan := make(chan loop.SwapInfo)
7398

@@ -124,9 +149,10 @@ func daemon(config *config) error {
124149
go func() {
125150
defer wg.Done()
126151

127-
logger.Infof("RPC server listening on %s", lis.Addr())
152+
logger.Infof("RPC server listening on %s", grpcListener.Addr())
153+
logger.Infof("REST proxy listening on %s", restListener.Addr())
128154

129-
err = grpcServer.Serve(lis)
155+
err = grpcServer.Serve(grpcListener)
130156
if err != nil {
131157
logger.Error(err)
132158
}

cmd/loopd/swapclient_server.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ func (s *swapClientServer) Monitor(in *looprpc.MonitorRequest,
208208
}
209209
}
210210

211-
// GetTerms returns the terms that the server enforces for swaps.
212-
func (s *swapClientServer) GetLoopOutTerms(ctx context.Context,
211+
// LoopOutTerms returns the terms that the server enforces for loop out swaps.
212+
func (s *swapClientServer) LoopOutTerms(ctx context.Context,
213213
req *looprpc.TermsRequest) (*looprpc.TermsResponse, error) {
214214

215215
logger.Infof("Terms request received")
@@ -230,8 +230,9 @@ func (s *swapClientServer) GetLoopOutTerms(ctx context.Context,
230230
}, nil
231231
}
232232

233-
// GetQuote returns a quote for a swap with the provided parameters.
234-
func (s *swapClientServer) GetLoopOutQuote(ctx context.Context,
233+
// LoopOutQuote returns a quote for a loop out swap with the provided
234+
// parameters.
235+
func (s *swapClientServer) LoopOutQuote(ctx context.Context,
235236
req *looprpc.QuoteRequest) (*looprpc.QuoteResponse, error) {
236237

237238
quote, err := s.impl.LoopOutQuote(ctx, &loop.LoopOutQuoteRequest{

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/coreos/bbolt v0.0.0-20180223184059-7ee3ded59d4835e10f3e7d0f7603c42aa5e83820
88
github.com/fortytw2/leaktest v1.3.0
99
github.com/golang/protobuf v1.2.0
10+
github.com/grpc-ecosystem/grpc-gateway v0.0.0-20170724004829-f2862b476edc
1011
github.com/jessevdk/go-flags v0.0.0-20170926144705-f88afde2fa19
1112
github.com/lightningnetwork/lnd v0.0.0
1213
github.com/urfave/cli v1.20.0

0 commit comments

Comments
 (0)