-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathconfig_dev.go
172 lines (142 loc) · 5.17 KB
/
config_dev.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//go:build dev
package terminal
import (
"fmt"
"path/filepath"
"github.com/lightninglabs/lightning-terminal/accounts"
"github.com/lightninglabs/lightning-terminal/db"
"github.com/lightninglabs/lightning-terminal/firewalldb"
"github.com/lightninglabs/lightning-terminal/session"
"github.com/lightningnetwork/lnd/clock"
)
const (
// DatabaseBackendSqlite is the name of the SQLite database backend.
DatabaseBackendSqlite = "sqlite"
// DatabaseBackendPostgres is the name of the Postgres database backend.
DatabaseBackendPostgres = "postgres"
// DatabaseBackendBbolt is the name of the bbolt database backend.
DatabaseBackendBbolt = "bbolt"
// defaultSqliteDatabaseFileName is the default name of the SQLite
// database file.
defaultSqliteDatabaseFileName = "litd.db"
)
// defaultSqliteDatabasePath is the default path under which we store
// the SQLite database file.
var defaultSqliteDatabasePath = filepath.Join(
DefaultLitDir, DefaultNetwork, defaultSqliteDatabaseFileName,
)
// DevConfig is a struct that holds the configuration options for a development
// environment. The purpose of this struct is to hold config options for
// features not yet available in production. Since our itests are built with
// the dev tag, we can test these features in our itests.
//
// nolint:lll
type DevConfig struct {
// DatabaseBackend is the database backend we will use for storing all
// account related data. While this feature is still in development, we
// include the bbolt type here so that our itests can continue to be
// tested against a bbolt backend. Once the full bbolt to SQL migration
// is complete, however, we will remove the bbolt option.
DatabaseBackend string `long:"databasebackend" description:"The database backend to use for storing all account related data." choice:"bbolt" choice:"sqlite" choice:"postgres"`
// Sqlite holds the configuration options for a SQLite database
// backend.
Sqlite *db.SqliteConfig `group:"sqlite" namespace:"sqlite"`
// Postgres holds the configuration options for a Postgres database
Postgres *db.PostgresConfig `group:"postgres" namespace:"postgres"`
}
// Validate checks that all the values set in our DevConfig are valid and uses
// the passed parameters to override any defaults if necessary.
func (c *DevConfig) Validate(dbDir, network string) error {
// We'll update the database file location if it wasn't set.
if c.Sqlite.DatabaseFileName == defaultSqliteDatabasePath {
c.Sqlite.DatabaseFileName = filepath.Join(
dbDir, network, defaultSqliteDatabaseFileName,
)
}
return nil
}
// defaultDevConfig returns a new DevConfig with default values set.
func defaultDevConfig() *DevConfig {
return &DevConfig{
Sqlite: &db.SqliteConfig{
DatabaseFileName: defaultSqliteDatabasePath,
},
Postgres: &db.PostgresConfig{
Host: "localhost",
Port: 5432,
MaxOpenConnections: 10,
},
}
}
// NewStores creates a new stores instance based on the chosen database backend.
func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
var (
networkDir = filepath.Join(cfg.LitDir, cfg.Network)
stores = &stores{
closeFns: make(map[string]func() error),
}
)
switch cfg.DatabaseBackend {
case DatabaseBackendSqlite:
// Before we initialize the SQLite store, we'll make sure that
// the directory where we will store the database file exists.
err := makeDirectories(networkDir)
if err != nil {
return stores, err
}
sqlStore, err := db.NewSqliteStore(cfg.Sqlite)
if err != nil {
return stores, err
}
acctStore := accounts.NewSQLStore(sqlStore.BaseDB, clock)
sessStore := session.NewSQLStore(sqlStore.BaseDB, clock)
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB)
stores.accounts = acctStore
stores.sessions = sessStore
stores.firewall = firewalldb.NewDB(firewallStore)
stores.closeFns["sqlite"] = sqlStore.BaseDB.Close
case DatabaseBackendPostgres:
sqlStore, err := db.NewPostgresStore(cfg.Postgres)
if err != nil {
return stores, err
}
acctStore := accounts.NewSQLStore(sqlStore.BaseDB, clock)
sessStore := session.NewSQLStore(sqlStore.BaseDB, clock)
firewallStore := firewalldb.NewSQLDB(sqlStore.BaseDB)
stores.accounts = acctStore
stores.sessions = sessStore
stores.firewall = firewalldb.NewDB(firewallStore)
stores.closeFns["postgres"] = sqlStore.BaseDB.Close
default:
accountStore, err := accounts.NewBoltStore(
filepath.Dir(cfg.MacaroonPath), accounts.DBFilename,
clock,
)
if err != nil {
return stores, err
}
stores.accounts = accountStore
stores.closeFns["bbolt-accounts"] = accountStore.Close
sessionStore, err := session.NewDB(
networkDir, session.DBFilename, clock, accountStore,
)
if err != nil {
return stores, err
}
stores.sessions = sessionStore
stores.closeFns["bbolt-sessions"] = sessionStore.Close
}
firewallBoltDB, err := firewalldb.NewBoltDB(
networkDir, firewalldb.DBFilename, stores.sessions,
)
if err != nil {
return stores, fmt.Errorf("error creating firewall BoltDB: %v",
err)
}
if stores.firewall == nil {
stores.firewall = firewalldb.NewDB(firewallBoltDB)
}
stores.firewallBolt = firewallBoltDB
stores.closeFns["bbolt-firewalldb"] = firewallBoltDB.Close
return stores, nil
}