chore: support for cockroachdb (resolve #90)

This commit is contained in:
Ferdinand Mütsch 2021-01-18 21:34:08 +01:00
parent 1aecfc4ca3
commit acda62488d
5 changed files with 32 additions and 6 deletions

View File

@ -85,8 +85,17 @@ You can specify configuration options either via a config file (default: `config
| `db.user` | `WAKAPI_DB_USER` | - | Database user | | `db.user` | `WAKAPI_DB_USER` | - | Database user |
| `db.password` | `WAKAPI_DB_PASSWORD` | - | Database password | | `db.password` | `WAKAPI_DB_PASSWORD` | - | Database password |
| `db.name` | `WAKAPI_DB_NAME` | `wakapi_db.db` | Database name | | `db.name` | `WAKAPI_DB_NAME` | `wakapi_db.db` | Database name |
| `db.dialect` | `WAKAPI_DB_TYPE` | `sqlite3` | Database type (one of sqlite3, mysql, postgres) | | `db.dialect` | `WAKAPI_DB_TYPE` | `sqlite3` | Database type (one of `sqlite3`, `mysql`, `postgres`, `cockroach`) |
| `db.max_conn` | `WAKAPI_DB_MAX_CONNECTIONS` | `2` | Maximum number of database connections | | `db.max_conn` | `WAKAPI_DB_MAX_CONNECTIONS` | `2` | Maximum number of database connections |
| `db.ssl` | `WAKAPI_DB_SSL` | `false` | Whether to use TLS encryption for database connection (Postgres and CockroachDB only) |
### Supported databases
Wakapi uses [GORM](https://gorm.io) as an ORM. As a consequence, a set of different relational databases is supported.
* [SQLite](https://sqlite.org/) (_default, easy setup_)
* [MySQL](https://hub.docker.com/_/mysql) (_recommended, because most extensively tested_)
* [MariaDB](https://hub.docker.com/_/mariadb) (_open-source MySQL alternative_)
* [Postgres](https://hub.docker.com/_/postgres) (_open-source as well_)
* [CockroachDB](https://www.cockroachlabs.com/docs/stable/install-cockroachdb-linux.html) (_cloud-native, distributed, Postgres-compatible API_)
## 💻 Client Setup ## 💻 Client Setup
Wakapi relies on the open-source [WakaTime](https://github.com/wakatime/wakatime) client tools. In order to collect statistics to Wakapi, you need to set them up. Wakapi relies on the open-source [WakaTime](https://github.com/wakatime/wakatime) client tools. In order to collect statistics to Wakapi, you need to set them up.

View File

@ -22,7 +22,8 @@ db:
password: # leave blank when using sqlite3 password: # leave blank when using sqlite3
name: wakapi_db.db # database name for mysql / postgres or file path for sqlite (e.g. /tmp/wakapi.db) name: wakapi_db.db # database name for mysql / postgres or file path for sqlite (e.g. /tmp/wakapi.db)
dialect: sqlite3 # mysql, postgres, sqlite3 dialect: sqlite3 # mysql, postgres, sqlite3
max_conn: 2 max_conn: 2 # maximum number of concurrent connections to maintain
ssl: false # whether to use tls for db connection (must be true for cockroachdb) (ignored for mysql and sqlite)
security: security:
password_salt: # CHANGE ! password_salt: # CHANGE !

View File

@ -56,8 +56,10 @@ type dbConfig struct {
User string `env:"WAKAPI_DB_USER"` User string `env:"WAKAPI_DB_USER"`
Password string `env:"WAKAPI_DB_PASSWORD"` Password string `env:"WAKAPI_DB_PASSWORD"`
Name string `default:"wakapi_db.db" env:"WAKAPI_DB_NAME"` Name string `default:"wakapi_db.db" env:"WAKAPI_DB_NAME"`
Dialect string `default:"sqlite3" env:"WAKAPI_DB_TYPE"` Dialect string `yaml:"-"`
Type string `yaml:"dialect" default:"sqlite3" env:"WAKAPI_DB_TYPE"`
MaxConn uint `yaml:"max_conn" default:"2" env:"WAKAPI_DB_MAX_CONNECTIONS"` MaxConn uint `yaml:"max_conn" default:"2" env:"WAKAPI_DB_MAX_CONNECTIONS"`
Ssl bool `default:"false" env:"WAKAPI_DB_SSL"`
} }
type serverConfig struct { type serverConfig struct {
@ -170,12 +172,18 @@ func mysqlConnectionString(config *dbConfig) string {
} }
func postgresConnectionString(config *dbConfig) string { func postgresConnectionString(config *dbConfig) string {
return fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=disable", sslmode := "disable"
if config.Ssl {
sslmode = "require"
}
return fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s",
config.Host, config.Host,
config.Port, config.Port,
config.User, config.User,
config.Name, config.Name,
config.Password, config.Password,
sslmode,
) )
} }
@ -243,6 +251,13 @@ func mustReadConfigLocation() string {
return *cFlag return *cFlag
} }
func resolveDbDialect(dbType string) string {
if dbType == "cockroach" {
return "postgres"
}
return dbType
}
func Set(config *Config) { func Set(config *Config) {
cfg = config cfg = config
} }
@ -264,6 +279,7 @@ func Load() *Config {
config.Version = readVersion() config.Version = readVersion()
config.App.LanguageColors = readLanguageColors() config.App.LanguageColors = readLanguageColors()
config.Db.Dialect = resolveDbDialect(config.Db.Type)
config.Security.SecureCookie = securecookie.New( config.Security.SecureCookie = securecookie.New(
securecookie.GenerateRandomKey(64), securecookie.GenerateRandomKey(64),
securecookie.GenerateRandomKey(32), securecookie.GenerateRandomKey(32),

View File

@ -31,7 +31,7 @@ func loadTemplates() {
return config.Get().Version return config.Get().Version
}, },
"getDbType": func() string { "getDbType": func() string {
return strings.ToLower(config.Get().Db.Dialect) return strings.ToLower(config.Get().Db.Type)
}, },
"htmlSafe": func(html string) template.HTML { "htmlSafe": func(html string) template.HTML {
return template.HTML(html) return template.HTML(html)

View File

@ -1 +1 @@
1.18.2 1.18.3