2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-11 13:11:12 +00:00

cmd/loopd: add REST proxy

This commit is contained in:
Wilmer Paulino 2019-03-12 15:35:53 -07:00
parent 9edb910f53
commit eb8277b9f9
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F
3 changed files with 38 additions and 9 deletions

View File

@ -12,7 +12,8 @@ type config struct {
Insecure bool `long:"insecure" description:"disable tls"` Insecure bool `long:"insecure" description:"disable tls"`
Network string `long:"network" description:"network to run on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"` Network string `long:"network" description:"network to run on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"`
SwapServer string `long:"swapserver" description:"swap server address host:port"` SwapServer string `long:"swapserver" description:"swap server address host:port"`
Listen string `long:"listen" description:"address to listen on for rpc lcients"` RPCListen string `long:"rpclisten" description:"Address to listen on for gRPC clients"`
RESTListen string `long:"restlisten" description:"Address to listen on for REST clients"`
Lnd *lndConfig `group:"lnd" namespace:"lnd"` Lnd *lndConfig `group:"lnd" namespace:"lnd"`
@ -22,7 +23,8 @@ type config struct {
var defaultConfig = config{ var defaultConfig = config{
Network: "mainnet", Network: "mainnet",
SwapServer: "swap.lightning.today:11009", SwapServer: "swap.lightning.today:11009",
Listen: "localhost:11010", RPCListen: "localhost:11010",
RESTListen: "localhost:8081",
Insecure: false, Insecure: false,
Lnd: &lndConfig{ Lnd: &lndConfig{
Host: "localhost:10009", Host: "localhost:10009",

View File

@ -4,12 +4,14 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"net/http"
"os" "os"
"os/signal" "os/signal"
"runtime/pprof" "runtime/pprof"
"sync" "sync"
"time" "time"
proxy "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/lightninglabs/loop" "github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/looprpc" "github.com/lightninglabs/loop/looprpc"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -59,15 +61,38 @@ func daemon(config *config) error {
grpcServer := grpc.NewServer(serverOpts...) grpcServer := grpc.NewServer(serverOpts...)
looprpc.RegisterSwapClientServer(grpcServer, &server) looprpc.RegisterSwapClientServer(grpcServer, &server)
// Next, Start the gRPC server listening for HTTP/2 connections. // Next, start the gRPC server listening for HTTP/2 connections.
logger.Infof("Starting RPC listener") logger.Infof("Starting gRPC listener")
lis, err := net.Listen("tcp", config.Listen) grpcListener, err := net.Listen("tcp", config.RPCListen)
if err != nil { if err != nil {
return fmt.Errorf("RPC server unable to listen on %s", return fmt.Errorf("RPC server unable to listen on %s",
config.Listen) config.RPCListen)
} }
defer lis.Close() defer grpcListener.Close()
// We'll also create and start an accompanying proxy to serve clients
// through REST.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mux := proxy.NewServeMux()
proxyOpts := []grpc.DialOption{grpc.WithInsecure()}
err = looprpc.RegisterSwapClientHandlerFromEndpoint(
ctx, mux, config.RPCListen, proxyOpts,
)
if err != nil {
return err
}
logger.Infof("Starting REST proxy listener")
restListener, err := net.Listen("tcp", config.RESTListen)
if err != nil {
return fmt.Errorf("REST proxy unable to listen on %s",
config.RESTListen)
}
defer restListener.Close()
proxy := &http.Server{Handler: mux}
go proxy.Serve(restListener)
statusChan := make(chan loop.SwapInfo) statusChan := make(chan loop.SwapInfo)
@ -124,9 +149,10 @@ func daemon(config *config) error {
go func() { go func() {
defer wg.Done() defer wg.Done()
logger.Infof("RPC server listening on %s", lis.Addr()) logger.Infof("RPC server listening on %s", grpcListener.Addr())
logger.Infof("REST proxy listening on %s", restListener.Addr())
err = grpcServer.Serve(lis) err = grpcServer.Serve(grpcListener)
if err != nil { if err != nil {
logger.Error(err) logger.Error(err)
} }

1
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/coreos/bbolt v0.0.0-20180223184059-7ee3ded59d4835e10f3e7d0f7603c42aa5e83820 github.com/coreos/bbolt v0.0.0-20180223184059-7ee3ded59d4835e10f3e7d0f7603c42aa5e83820
github.com/fortytw2/leaktest v1.3.0 github.com/fortytw2/leaktest v1.3.0
github.com/golang/protobuf v1.2.0 github.com/golang/protobuf v1.2.0
github.com/grpc-ecosystem/grpc-gateway v0.0.0-20170724004829-f2862b476edc
github.com/jessevdk/go-flags v0.0.0-20170926144705-f88afde2fa19 github.com/jessevdk/go-flags v0.0.0-20170926144705-f88afde2fa19
github.com/lightningnetwork/lnd v0.0.0 github.com/lightningnetwork/lnd v0.0.0
github.com/urfave/cli v1.20.0 github.com/urfave/cli v1.20.0