From a9fb60004475e1c253c88754371e98a79dfd29ed Mon Sep 17 00:00:00 2001 From: positiveblue Date: Sun, 28 May 2023 08:22:08 -0700 Subject: [PATCH] config: support params for different database backends --- config.go | 41 ++++++++++++++++++++++++++++++++++++----- hashmail_server_test.go | 3 ++- sample-conf.yaml | 28 ++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index 86dcfb2..eaab3f5 100644 --- a/config.go +++ b/config.go @@ -3,9 +3,11 @@ package aperture import ( "errors" "fmt" + "path/filepath" "time" "github.com/btcsuite/btcd/btcutil" + "github.com/lightninglabs/aperture/aperturedb" "github.com/lightninglabs/aperture/proxy" ) @@ -18,6 +20,14 @@ var ( defaultLogFilename = "aperture.log" defaultMaxLogFiles = 3 defaultMaxLogFileSize = 10 + + defaultSqliteDatabaseFileName = "aperture.db" + + // defaultSqliteDatabasePath is the default path under which we store + // the SQLite database file. + defaultSqliteDatabasePath = filepath.Join( + apertureDataDir, defaultSqliteDatabaseFileName, + ) ) type EtcdConfig struct { @@ -98,6 +108,16 @@ type Config struct { // directory defined by StaticRoot. ServeStatic bool `long:"servestatic" description:"Flag to enable or disable static content serving."` + // DatabaseBackend is the database backend to be used by the server. + DatabaseBackend string `long:"dbbackend" description:"The database backend to use for storing all asset related data." choice:"sqlite" choice:"postgres"` + + // Sqlite is the configuration section for the SQLite database backend. + Sqlite *aperturedb.SqliteConfig `group:"sqlite" namespace:"sqlite"` + + // Postgres is the configuration section for the Postgres database backend. + Postgres *aperturedb.PostgresConfig `group:"postgres" namespace:"postgres"` + + // Etcd is the configuration section for the Etcd database backend. Etcd *EtcdConfig `group:"etcd" namespace:"etcd"` Authenticator *AuthConfig `group:"authenticator" namespace:"authenticator"` @@ -142,13 +162,24 @@ func (c *Config) validate() error { return nil } +// DefaultConfig returns the default configuration for a sqlite backend. +func DefaultSqliteConfig() *aperturedb.SqliteConfig { + return &aperturedb.SqliteConfig{ + SkipMigrations: false, + DatabaseFileName: defaultSqliteDatabasePath, + } +} + // NewConfig initializes a new Config variable. func NewConfig() *Config { return &Config{ - Etcd: &EtcdConfig{}, - Authenticator: &AuthConfig{}, - Tor: &TorConfig{}, - HashMail: &HashMailConfig{}, - Prometheus: &PrometheusConfig{}, + DatabaseBackend: "etcd", + Etcd: &EtcdConfig{}, + Sqlite: DefaultSqliteConfig(), + Postgres: &aperturedb.PostgresConfig{}, + Authenticator: &AuthConfig{}, + Tor: &TorConfig{}, + HashMail: &HashMailConfig{}, + Prometheus: &PrometheusConfig{}, } } diff --git a/hashmail_server_test.go b/hashmail_server_test.go index f75d7b7..29dbbf6 100644 --- a/hashmail_server_test.go +++ b/hashmail_server_test.go @@ -160,7 +160,8 @@ func setupAperture(t *testing.T) { Authenticator: &AuthConfig{ Disable: true, }, - Etcd: &EtcdConfig{}, + DatabaseBackend: "etcd", + Etcd: &EtcdConfig{}, HashMail: &HashMailConfig{ Enabled: true, MessageRate: time.Millisecond, diff --git a/sample-conf.yaml b/sample-conf.yaml index 224a7fa..48411d4 100644 --- a/sample-conf.yaml +++ b/sample-conf.yaml @@ -38,6 +38,34 @@ authenticator: # The chain network the lnd is active on. network: "simnet" +# The selected database backend. The current default backend is "sqlite". +# Aperture also has support for postgres and etcd. +dbbackend: "sqlite" + +# Settings for the sqlite process which the proxy will use to reliably store and +# retrieve token information. +sqlite: + # The full path to the database. + dbfile: "/path/to/.aperture/aperture.db" + +# Settings for the postgres instance which the proxy will use to reliably store +# and retrieve token information. +postgres: + # Connection parameters. + host: "localhost" + port: 5432 + user: "user" + password: "password" + dbname: "aperture" + + # Max open connections to keep alive to the database server. + maxconnections: 25 + + # Whether to require using SSL (mode: require) when connecting to the + # server. + requireSSL: true + + # Settings for the etcd instance which the proxy will use to reliably store and # retrieve token information. etcd: