diff --git a/README.md b/README.md index bdf739f7..1506b836 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,36 @@ Update [values.yaml](https://github.com/openclarity/apiclarity/blob/master/chart 4. Open APIClarity UI in the browser: +## Database + +SQLite, for local testing, and PostgreSQL databases are supported. + +To choose your database type, use the `DATABASE_DRIVER` environment variable: + +* LOCAL: SQLite driver is used. By default, the the `db.db` SQlite file will be used. +* POSTGRES: PostgreSQL driver is used. In that case you can specifiy other self-explanatory environment variables: + * DB_NAME + * DB_USER + * DB_PASS + * DB_HOST + * DB_PORT_NUMBER + +In any case, you can also specify the full DSN yourself, depending on your +database type, by setting the `DB_DSN` environment variable. + +https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING +https://www.sqlite.org/c3ref/open.html#urifilenameexamples + +For example: + + DATABASE_DRIVER="POSTGRES" DB_DSN="postgresql:///apiclarity?host=/tmp/apiclaritydb" + + DATABASE_DRIVER="POSTGRES" DB_DSN="postgresql://user@localhost:5433/mydb?options=-c%20synchronous_commit%3Doff" + + DATABASE_DRIVER="LOCAL" DB_DSN="file::memory:?cache=shared" + + DATABASE_DRIVER="LOCAL" DB_DSN="file:/home/fred/data.db?vfs=unix-dotfile" + ## Contributing Pull requests and bug reports are welcome. diff --git a/backend/pkg/backend/backend.go b/backend/pkg/backend/backend.go index 82f7dc14..8ab6698c 100644 --- a/backend/pkg/backend/backend.go +++ b/backend/pkg/backend/backend.go @@ -80,6 +80,7 @@ func CreateBackend(config *_config.Config, monitor *k8smonitor.Monitor, speculat func createDatabaseConfig(config *_config.Config) *_database.DBConfig { return &_database.DBConfig{ DriverType: config.DatabaseDriver, + DSN: config.DBDSN, EnableInfoLogs: config.EnableDBInfoLogs, DBPassword: config.DBPassword, DBUser: config.DBUser, diff --git a/backend/pkg/config/config.go b/backend/pkg/config/config.go index 3c13cbe4..948e0867 100644 --- a/backend/pkg/config/config.go +++ b/backend/pkg/config/config.go @@ -55,6 +55,7 @@ const ( DBHostEnvVar = "DB_HOST" DBPortEnvVar = "DB_PORT_NUMBER" DatabaseDriver = "DATABASE_DRIVER" + DBDSN = "DB_DSN" EnableDBInfoLogs = "ENABLE_DB_INFO_LOGS" ResponseHeadersToIgnore = "RESPONSE_HEADERS_TO_IGNORE" @@ -95,6 +96,7 @@ type Config struct { // database config DatabaseDriver string + DBDSN string DBName string DBUser string DBPassword string @@ -130,6 +132,7 @@ func LoadConfig() (*Config, error) { config.EnableK8s = viper.GetBool(EnableK8s) config.EnableTLS = viper.GetBool(EnableTLS) config.DatabaseDriver = viper.GetString(DatabaseDriver) + config.DBDSN = viper.GetString(DBDSN) config.DBPassword = viper.GetString(DBPasswordEnvVar) config.DBUser = viper.GetString(DBUserEnvVar) config.DBHost = viper.GetString(DBHostEnvVar) diff --git a/backend/pkg/database/database.go b/backend/pkg/database/database.go index 1083d9e3..df18d735 100644 --- a/backend/pkg/database/database.go +++ b/backend/pkg/database/database.go @@ -54,6 +54,7 @@ type Handler struct { type DBConfig struct { EnableInfoLogs bool DriverType string + DSN string DBPassword string DBUser string DBHost string @@ -126,7 +127,7 @@ func initDataBase(config *DBConfig) *gorm.DB { case DBDriverTypePostgres: db = initPostgres(config, dbLogger) case DBDriverTypeLocal: - db = initSqlite(dbLogger) + db = initSqlite(config, dbLogger) default: log.Fatalf("DB driver is not supported: %v", dbDriver) } @@ -145,8 +146,13 @@ func initDataBase(config *DBConfig) *gorm.DB { } func initPostgres(config *DBConfig, dbLogger logger.Interface) *gorm.DB { - dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=UTC", - config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.DBPort) + var dsn string + if config.DSN != "" { + dsn = config.DSN + } else { + dsn = fmt.Sprintf("host='%s' user='%s' password='%s' dbname='%s' port='%s' sslmode='disable' TimeZone=UTC", + config.DBHost, config.DBUser, config.DBPassword, config.DBName, config.DBPort) + } db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ Logger: dbLogger, @@ -158,10 +164,16 @@ func initPostgres(config *DBConfig, dbLogger logger.Interface) *gorm.DB { return db } -func initSqlite(dbLogger logger.Interface) *gorm.DB { - cleanLocalDataBase(localDBPath) +func initSqlite(config *DBConfig, dbLogger logger.Interface) *gorm.DB { + var dsn string + if config.DSN != "" { + dsn = config.DSN + } else { + dsn = localDBPath + cleanLocalDataBase(localDBPath) + } - db, err := gorm.Open(sqlite.Open(localDBPath), &gorm.Config{ + db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{ Logger: dbLogger, }) if err != nil {