Skip to content

Commit e49a1c3

Browse files
authored
Merge pull request #996 from ellemouton/sql22Sessions14
[sql-22] session: sessions CRUD
2 parents 0829b4d + 6dbf880 commit e49a1c3

File tree

8 files changed

+1034
-40
lines changed

8 files changed

+1034
-40
lines changed

config_dev.go

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/lightninglabs/lightning-terminal/accounts"
99
"github.com/lightninglabs/lightning-terminal/db"
10+
"github.com/lightninglabs/lightning-terminal/session"
1011
"github.com/lightningnetwork/lnd/clock"
1112
)
1213

@@ -80,14 +81,19 @@ func defaultDevConfig() *DevConfig {
8081
}
8182
}
8283

83-
// NewAccountStore creates a new account store based on the chosen database
84-
// backend.
85-
func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) {
84+
// NewStores creates a new stores instance based on the chosen database backend.
85+
func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
86+
var (
87+
networkDir = filepath.Join(cfg.LitDir, cfg.Network)
88+
acctStore accounts.Store
89+
sessStore session.Store
90+
closeFn func() error
91+
)
92+
8693
switch cfg.DatabaseBackend {
8794
case DatabaseBackendSqlite:
8895
// Before we initialize the SQLite store, we'll make sure that
8996
// the directory where we will store the database file exists.
90-
networkDir := filepath.Join(cfg.LitDir, cfg.Network)
9197
err := makeDirectories(networkDir)
9298
if err != nil {
9399
return nil, err
@@ -98,20 +104,57 @@ func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) {
98104
return nil, err
99105
}
100106

101-
return accounts.NewSQLStore(sqlStore.BaseDB, clock), nil
107+
acctStore = accounts.NewSQLStore(sqlStore.BaseDB, clock)
108+
sessStore = session.NewSQLStore(sqlStore.BaseDB, clock)
109+
closeFn = sqlStore.BaseDB.Close
102110

103111
case DatabaseBackendPostgres:
104112
sqlStore, err := db.NewPostgresStore(cfg.Postgres)
105113
if err != nil {
106114
return nil, err
107115
}
108116

109-
return accounts.NewSQLStore(sqlStore.BaseDB, clock), nil
117+
acctStore = accounts.NewSQLStore(sqlStore.BaseDB, clock)
118+
sessStore = session.NewSQLStore(sqlStore.BaseDB, clock)
119+
closeFn = sqlStore.BaseDB.Close
110120

111121
default:
112-
return accounts.NewBoltStore(
122+
accountStore, err := accounts.NewBoltStore(
113123
filepath.Dir(cfg.MacaroonPath), accounts.DBFilename,
114124
clock,
115125
)
126+
if err != nil {
127+
return nil, err
128+
}
129+
130+
sessionStore, err := session.NewDB(
131+
networkDir, session.DBFilename, clock, accountStore,
132+
)
133+
if err != nil {
134+
return nil, err
135+
}
136+
137+
acctStore = accountStore
138+
sessStore = sessionStore
139+
closeFn = func() error {
140+
var returnErr error
141+
err = accountStore.Close()
142+
if err != nil {
143+
returnErr = err
144+
}
145+
146+
err = sessionStore.Close()
147+
if err != nil {
148+
returnErr = err
149+
}
150+
151+
return returnErr
152+
}
116153
}
154+
155+
return &stores{
156+
accounts: acctStore,
157+
sessions: sessStore,
158+
close: closeFn,
159+
}, nil
117160
}

config_prod.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
package terminal
44

55
import (
6+
"fmt"
67
"path/filepath"
78

89
"github.com/lightninglabs/lightning-terminal/accounts"
10+
"github.com/lightninglabs/lightning-terminal/session"
911
"github.com/lightningnetwork/lnd/clock"
1012
)
1113

@@ -24,10 +26,41 @@ func (c *DevConfig) Validate(_, _ string) error {
2426
return nil
2527
}
2628

27-
// NewAccountStore creates a new account store using the default Bolt backend
28-
// since in production, this is the only backend supported currently.
29-
func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) {
30-
return accounts.NewBoltStore(
29+
// NewStores creates a new instance of the stores struct using the default Bolt
30+
// backend since in production, this is currently the only backend supported.
31+
func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
32+
networkDir := filepath.Join(cfg.LitDir, cfg.Network)
33+
34+
acctStore, err := accounts.NewBoltStore(
3135
filepath.Dir(cfg.MacaroonPath), accounts.DBFilename, clock,
3236
)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
sessStore, err := session.NewDB(
42+
networkDir, session.DBFilename, clock, acctStore,
43+
)
44+
if err != nil {
45+
return nil, fmt.Errorf("error creating session BoltStore: %v",
46+
err)
47+
}
48+
49+
return &stores{
50+
accounts: acctStore,
51+
sessions: sessStore,
52+
close: func() error {
53+
var returnErr error
54+
if err := acctStore.Close(); err != nil {
55+
returnErr = fmt.Errorf("error closing "+
56+
"account store: %v", err)
57+
}
58+
if err := sessStore.Close(); err != nil {
59+
returnErr = fmt.Errorf("error closing "+
60+
"session store: %v", err)
61+
}
62+
63+
return returnErr
64+
},
65+
}, nil
3366
}

0 commit comments

Comments
 (0)