Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds database DB_DSN param #312

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,36 @@ Update [values.yaml](https://github.com/openclarity/apiclarity/blob/master/chart

4. Open APIClarity UI in the browser: <http://localhost:8080/>

## 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.
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions backend/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -95,6 +96,7 @@ type Config struct {

// database config
DatabaseDriver string
DBDSN string
DBName string
DBUser string
DBPassword string
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 18 additions & 6 deletions backend/pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Handler struct {
type DBConfig struct {
EnableInfoLogs bool
DriverType string
DSN string
DBPassword string
DBUser string
DBHost string
Expand Down Expand Up @@ -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)
}
Expand All @@ -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,
Expand All @@ -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 {
Expand Down