Skip to content

Commit bf96592

Browse files
committed
aperture: initialize server with the configured db backend
1 parent a9fb600 commit bf96592

File tree

1 file changed

+102
-21
lines changed

1 file changed

+102
-21
lines changed

aperture.go

Lines changed: 102 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aperture
33
import (
44
"context"
55
"crypto/tls"
6+
"database/sql"
67
"errors"
78
"fmt"
89
"io"
@@ -17,6 +18,7 @@ import (
1718
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
1819
gateway "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1920
flags "github.com/jessevdk/go-flags"
21+
"github.com/lightninglabs/aperture/aperturedb"
2022
"github.com/lightninglabs/aperture/auth"
2123
"github.com/lightninglabs/aperture/mint"
2224
"github.com/lightninglabs/aperture/proxy"
@@ -160,6 +162,7 @@ type Aperture struct {
160162
cfg *Config
161163

162164
etcdClient *clientv3.Client
165+
db *sql.DB
163166
challenger *LndChallenger
164167
httpsServer *http.Server
165168
torHTTPServer *http.Server
@@ -208,17 +211,79 @@ func (a *Aperture) Start(errChan chan error) error {
208211
}()
209212
}
210213

211-
// Initialize our etcd client.
212-
a.etcdClient, err = clientv3.New(clientv3.Config{
213-
Endpoints: []string{a.cfg.Etcd.Host},
214-
DialTimeout: 5 * time.Second,
215-
Username: a.cfg.Etcd.User,
216-
Password: a.cfg.Etcd.Password,
217-
})
218-
if err != nil {
219-
return fmt.Errorf("unable to connect to etcd: %v", err)
214+
var (
215+
secretStore mint.SecretStore
216+
onionStore tor.OnionStore
217+
)
218+
219+
// Connect to the chosen database backend.
220+
switch a.cfg.DatabaseBackend {
221+
case "etcd":
222+
// Initialize our etcd client.
223+
a.etcdClient, err = clientv3.New(clientv3.Config{
224+
Endpoints: []string{a.cfg.Etcd.Host},
225+
DialTimeout: 5 * time.Second,
226+
Username: a.cfg.Etcd.User,
227+
Password: a.cfg.Etcd.Password,
228+
})
229+
if err != nil {
230+
return fmt.Errorf("unable to connect to etcd: %v", err)
231+
}
232+
233+
secretStore = newSecretStore(a.etcdClient)
234+
onionStore = newOnionStore(a.etcdClient)
235+
236+
case "postgres":
237+
db, err := aperturedb.NewPostgresStore(a.cfg.Postgres)
238+
if err != nil {
239+
return fmt.Errorf("unable to connect to postgres: %v",
240+
err)
241+
}
242+
a.db = db.DB
243+
244+
dbSecretTxer := aperturedb.NewTransactionExecutor(db,
245+
func(tx *sql.Tx) aperturedb.SecretsDB {
246+
return db.WithTx(tx)
247+
},
248+
)
249+
secretStore = aperturedb.NewSecretsStore(dbSecretTxer)
250+
251+
dbOnionTxer := aperturedb.NewTransactionExecutor(db,
252+
func(tx *sql.Tx) aperturedb.OnionDB {
253+
return db.WithTx(tx)
254+
},
255+
)
256+
onionStore = aperturedb.NewOnionStore(dbOnionTxer)
257+
258+
case "sqlite":
259+
db, err := aperturedb.NewSqliteStore(a.cfg.Sqlite)
260+
if err != nil {
261+
return fmt.Errorf("unable to connect to sqlite: %v",
262+
err)
263+
}
264+
a.db = db.DB
265+
266+
dbSecretTxer := aperturedb.NewTransactionExecutor(db,
267+
func(tx *sql.Tx) aperturedb.SecretsDB {
268+
return db.WithTx(tx)
269+
},
270+
)
271+
secretStore = aperturedb.NewSecretsStore(dbSecretTxer)
272+
273+
dbOnionTxer := aperturedb.NewTransactionExecutor(db,
274+
func(tx *sql.Tx) aperturedb.OnionDB {
275+
return db.WithTx(tx)
276+
},
277+
)
278+
onionStore = aperturedb.NewOnionStore(dbOnionTxer)
279+
280+
default:
281+
return fmt.Errorf("unknown database backend: %s",
282+
a.cfg.DatabaseBackend)
220283
}
221284

285+
log.Infof("Using %v as database backend", a.cfg.DatabaseBackend)
286+
222287
// Create our challenger that uses our backing lnd node to create
223288
// invoices and check their settlement status.
224289
genInvoiceReq := func(price int64) (*lnrpc.Invoice, error) {
@@ -243,7 +308,7 @@ func (a *Aperture) Start(errChan chan error) error {
243308

244309
// Create the proxy and connect it to lnd.
245310
a.proxy, a.proxyCleanup, err = createProxy(
246-
a.cfg, a.challenger, a.etcdClient,
311+
a.cfg, a.challenger, secretStore,
247312
)
248313
if err != nil {
249314
return err
@@ -304,7 +369,7 @@ func (a *Aperture) Start(errChan chan error) error {
304369
// provide encryption, so running this additional HTTP server should be
305370
// relatively safe.
306371
if a.cfg.Tor.V3 {
307-
torController, err := initTorListener(a.cfg, a.etcdClient)
372+
torController, err := initTorListener(a.cfg, onionStore)
308373
if err != nil {
309374
return err
310375
}
@@ -351,14 +416,31 @@ func (a *Aperture) Stop() error {
351416
a.proxyCleanup()
352417
}
353418

419+
if a.etcdClient != nil {
420+
if err := a.etcdClient.Close(); err != nil {
421+
log.Errorf("Error terminating etcd client: %v", err)
422+
returnErr = err
423+
}
424+
}
425+
426+
if a.db != nil {
427+
if err := a.db.Close(); err != nil {
428+
log.Errorf("Error closing database: %v", err)
429+
returnErr = err
430+
}
431+
}
432+
354433
// Shut down our client and server connections now. This should cause
355434
// the first goroutine to quit.
356-
cleanup(a.etcdClient, a.httpsServer, a.proxy)
435+
cleanup(a.httpsServer, a.proxy)
357436

358437
// If we started a tor server as well, shut it down now too to cause the
359438
// second goroutine to quit.
360439
if a.torHTTPServer != nil {
361-
returnErr = a.torHTTPServer.Close()
440+
if err := a.torHTTPServer.Close(); err != nil {
441+
log.Errorf("Error stopping tor server: %v", err)
442+
returnErr = err
443+
}
362444
}
363445

364446
// Now we wait for the goroutines to exit before we return. The defers
@@ -628,13 +710,15 @@ func getTLSConfig(serverName, baseDir string, autoCert bool) (
628710
// initTorListener initiates a Tor controller instance with the Tor server
629711
// specified in the config. Onion services will be created over which the proxy
630712
// can be reached at.
631-
func initTorListener(cfg *Config, etcd *clientv3.Client) (*tor.Controller, error) {
713+
func initTorListener(cfg *Config, store tor.OnionStore) (*tor.Controller,
714+
error) {
715+
632716
// Establish a controller connection with the backing Tor server and
633717
// proceed to create the requested onion services.
634718
onionCfg := tor.AddOnionConfig{
635719
VirtualPort: int(cfg.Tor.VirtualPort),
636720
TargetPorts: []int{int(cfg.Tor.ListenPort)},
637-
Store: newOnionStore(etcd),
721+
Store: store,
638722
}
639723
torController := tor.NewController(cfg.Tor.Control, "", "")
640724
if err := torController.Start(); err != nil {
@@ -656,11 +740,11 @@ func initTorListener(cfg *Config, etcd *clientv3.Client) (*tor.Controller, error
656740

657741
// createProxy creates the proxy with all the services it needs.
658742
func createProxy(cfg *Config, challenger *LndChallenger,
659-
etcdClient *clientv3.Client) (*proxy.Proxy, func(), error) {
743+
store mint.SecretStore) (*proxy.Proxy, func(), error) {
660744

661745
minter := mint.New(&mint.Config{
662746
Challenger: challenger,
663-
Secrets: newSecretStore(etcdClient),
747+
Secrets: store,
664748
ServiceLimiter: newStaticServiceLimiter(cfg.Services),
665749
Now: time.Now,
666750
})
@@ -817,13 +901,10 @@ func createHashMailServer(cfg *Config) ([]proxy.LocalService, func(), error) {
817901
}
818902

819903
// cleanup closes the given server and shuts down the log rotator.
820-
func cleanup(etcdClient io.Closer, server io.Closer, proxy io.Closer) {
904+
func cleanup(server io.Closer, proxy io.Closer) {
821905
if err := proxy.Close(); err != nil {
822906
log.Errorf("Error terminating proxy: %v", err)
823907
}
824-
if err := etcdClient.Close(); err != nil {
825-
log.Errorf("Error terminating etcd client: %v", err)
826-
}
827908
err := server.Close()
828909
if err != nil {
829910
log.Errorf("Error closing server: %v", err)

0 commit comments

Comments
 (0)