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}
0 commit comments