Skip to content

Commit 0289e29

Browse files
committed
subservers+terminal: let subserver mgr handle REST registrations
1 parent 7d832ee commit 0289e29

File tree

6 files changed

+89
-24
lines changed

6 files changed

+89
-24
lines changed

subservers/faraday.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package subservers
22

33
import (
4+
"context"
5+
6+
restProxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
47
"github.com/lightninglabs/faraday"
58
"github.com/lightninglabs/faraday/frdrpc"
69
"github.com/lightninglabs/faraday/frdrpcserver"
@@ -76,6 +79,19 @@ func (f *faradaySubServer) RegisterGrpcService(service grpc.ServiceRegistrar) {
7679
frdrpc.RegisterFaradayServerServer(service, f)
7780
}
7881

82+
// RegisterRestService registers the sub-server's REST handlers with the given
83+
// endpoint.
84+
//
85+
// NOTE: this is part of the SubServer interface.
86+
func (f *faradaySubServer) RegisterRestService(ctx context.Context,
87+
mux *restProxy.ServeMux, endpoint string,
88+
dialOpts []grpc.DialOption) error {
89+
90+
return frdrpc.RegisterFaradayServerHandlerFromEndpoint(
91+
ctx, mux, endpoint, dialOpts,
92+
)
93+
}
94+
7995
// ServerErrChan returns an error channel that should be listened on after
8096
// starting the sub-server to listen for any runtime errors. It is optional and
8197
// may be set to nil. This only applies in integrated mode.

subservers/interface.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package subservers
22

33
import (
4+
"context"
5+
6+
restProxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
47
"github.com/lightninglabs/lndclient"
58
"github.com/lightningnetwork/lnd/lnrpc"
69
"github.com/lightningnetwork/lnd/macaroons"
@@ -36,6 +39,11 @@ type SubServer interface {
3639
// the given registrar.
3740
RegisterGrpcService(grpc.ServiceRegistrar)
3841

42+
// RegisterRestService registers the sub-server's REST handlers with the
43+
// given endpoint.
44+
RegisterRestService(context.Context, *restProxy.ServeMux, string,
45+
[]grpc.DialOption) error
46+
3947
// ServerErrChan returns an error channel that should be listened on
4048
// after starting the sub-server to listen for any runtime errors. It
4149
// is optional and may be set to nil. This only applies in integrated

subservers/loop.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package subservers
22

33
import (
4+
"context"
5+
6+
restProxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
47
"github.com/lightninglabs/lndclient"
58
"github.com/lightninglabs/loop/loopd"
69
"github.com/lightninglabs/loop/looprpc"
@@ -81,6 +84,19 @@ func (l *loopSubServer) RegisterGrpcService(registrar grpc.ServiceRegistrar) {
8184
looprpc.RegisterSwapClientServer(registrar, l)
8285
}
8386

87+
// RegisterRestService registers the sub-server's REST handlers with the given
88+
// endpoint.
89+
//
90+
// NOTE: this is part of the SubServer interface.
91+
func (l *loopSubServer) RegisterRestService(ctx context.Context,
92+
mux *restProxy.ServeMux, endpoint string,
93+
dialOpts []grpc.DialOption) error {
94+
95+
return looprpc.RegisterSwapClientHandlerFromEndpoint(
96+
ctx, mux, endpoint, dialOpts,
97+
)
98+
}
99+
84100
// ServerErrChan returns an error channel that should be listened on after
85101
// starting the sub-server to listen for any runtime errors. It is optional and
86102
// may be set to nil. This only applies in integrated mode.

subservers/manager.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88
"time"
99

10+
restProxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1011
"github.com/lightninglabs/lightning-terminal/perms"
1112
"github.com/lightninglabs/lndclient"
1213
"github.com/lightningnetwork/lnd/lncfg"
@@ -119,6 +120,33 @@ func (s *Manager) RegisterRPCServices(server grpc.ServiceRegistrar) {
119120
}
120121
}
121122

123+
// RegisterRestServices registers all the manager's sub-servers REST handlers
124+
// with the given endpoint.
125+
func (s *Manager) RegisterRestServices(ctx context.Context,
126+
mux *restProxy.ServeMux, endpoint string,
127+
dialOpts []grpc.DialOption) error {
128+
129+
s.mu.RLock()
130+
defer s.mu.RUnlock()
131+
132+
for _, ss := range s.servers {
133+
// In remote mode the "director" of the RPC proxy will act as
134+
// a catch-all for any gRPC request that isn't known because we
135+
// didn't register any server for it. The director will then
136+
// forward the request to the remote service.
137+
if ss.Remote() {
138+
continue
139+
}
140+
141+
err := ss.RegisterRestService(ctx, mux, endpoint, dialOpts)
142+
if err != nil {
143+
return err
144+
}
145+
}
146+
147+
return nil
148+
}
149+
122150
// GetRemoteConn checks if any of the manager's sub-servers owns the given uri
123151
// and if so, the remote connection to that sub-server is returned. The bool
124152
// return value indicates if the uri is managed by one of the sub-servers

subservers/pool.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package subservers
22

33
import (
4+
"context"
5+
6+
restProxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
47
"github.com/lightninglabs/lndclient"
58
"github.com/lightninglabs/pool"
69
"github.com/lightninglabs/pool/poolrpc"
@@ -72,6 +75,19 @@ func (p *poolSubServer) RegisterGrpcService(registrar grpc.ServiceRegistrar) {
7275
poolrpc.RegisterTraderServer(registrar, p)
7376
}
7477

78+
// RegisterRestService registers the sub-server's REST handlers with the given
79+
// endpoint.
80+
//
81+
// NOTE: this is part of the SubServer interface.
82+
func (p *poolSubServer) RegisterRestService(ctx context.Context,
83+
mux *restProxy.ServeMux, endpoint string,
84+
dialOpts []grpc.DialOption) error {
85+
86+
return poolrpc.RegisterTraderHandlerFromEndpoint(
87+
ctx, mux, endpoint, dialOpts,
88+
)
89+
}
90+
7591
// ServerErrChan returns an error channel that should be listened on after
7692
// starting the sub-server to listen for any runtime errors. It is optional and
7793
// may be set to nil. This only applies in integrated mode.

terminal.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
restProxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
2121
"github.com/jessevdk/go-flags"
22-
"github.com/lightninglabs/faraday/frdrpc"
2322
"github.com/lightninglabs/lightning-terminal/accounts"
2423
"github.com/lightninglabs/lightning-terminal/autopilotserver"
2524
"github.com/lightninglabs/lightning-terminal/firewall"
@@ -33,9 +32,7 @@ import (
3332
"github.com/lightninglabs/lightning-terminal/subservers"
3433
"github.com/lightninglabs/lndclient"
3534
"github.com/lightninglabs/loop"
36-
"github.com/lightninglabs/loop/looprpc"
3735
"github.com/lightninglabs/pool"
38-
"github.com/lightninglabs/pool/poolrpc"
3936
"github.com/lightningnetwork/lnd"
4037
"github.com/lightningnetwork/lnd/build"
4138
"github.com/lightningnetwork/lnd/chainreg"
@@ -234,6 +231,10 @@ func (g *LightningTerminal) Run() error {
234231
// lnd once it's fully started.
235232
g.subServerMgr = subservers.NewManager(g.permsMgr)
236233

234+
// Register our sub-servers. This must be done before the Rest proxy is
235+
// set up so that the correct REST handlers are registered.
236+
g.initSubServers()
237+
237238
// Construct the rpcProxy. It must be initialised before the main web
238239
// server is started.
239240
g.rpcProxy = newRpcProxy(
@@ -293,10 +294,6 @@ func (g *LightningTerminal) Run() error {
293294
// up, these are considered non-fatal and will not result in an error being
294295
// returned.
295296
func (g *LightningTerminal) start() error {
296-
// Create the instances of our subservers now so we can hook them up to
297-
// lnd once it's fully started.
298-
g.initSubServers()
299-
300297
var err error
301298

302299
g.accountService, err = accounts.NewService(
@@ -926,23 +923,7 @@ func (g *LightningTerminal) RegisterRestSubserver(ctx context.Context,
926923
return err
927924
}
928925

929-
err = frdrpc.RegisterFaradayServerHandlerFromEndpoint(
930-
ctx, mux, endpoint, dialOpts,
931-
)
932-
if err != nil {
933-
return err
934-
}
935-
936-
err = looprpc.RegisterSwapClientHandlerFromEndpoint(
937-
ctx, mux, endpoint, dialOpts,
938-
)
939-
if err != nil {
940-
return err
941-
}
942-
943-
return poolrpc.RegisterTraderHandlerFromEndpoint(
944-
ctx, mux, endpoint, dialOpts,
945-
)
926+
return g.subServerMgr.RegisterRestServices(ctx, mux, endpoint, dialOpts)
946927
}
947928

948929
// ValidateMacaroon extracts the macaroon from the context's gRPC metadata,

0 commit comments

Comments
 (0)