Commit 9d04b23e authored by Christian Pointner's avatar Christian Pointner
Browse files

improved config for store database

parent 0ae85997
Pipeline #208 failed with stages
in 1 minute and 37 seconds
......@@ -10,8 +10,14 @@ To install tank, use the `go get` command:
$ go get gitlab.servus.at/autoradio/tank/...
```
Create configuration file see:
https://gitlab.servus.at/autoradio/tank/blob/master/cmd/tank/sample-cfg.yaml
Make sure that the directory pointed to by ```store.path``` exists and is writeable
for the user running the daemon
And then run the `tank` binary:
```sh
$ $GOPATH/bin/tank run --listen localhost:8000
$ $GOPATH/bin/tank --config config.yaml run --listen localhost:8000
```
......@@ -4,10 +4,21 @@ store:
format: flac
sample-rate: 44100
db:
type: "mysql"
connection: "tank:aura@tcp(127.0.0.1:3306)/tank?charset=utf8&parseTime=True&loc=Local"
# type: "postgres"
# connection: "host=127.0.0.1 port=5432 user=tank dbname=tank password=aura sslmode=disable"
type: mysql
#type: postgres
host: 127.0.0.1
#port:
# mysql: false, true, skip-verify
# postgres: require , verify-full, verify-ca, disable
tls: false
username: tank
password: aura
database: tank
debug: false
importer:
temp-path: "/tmp"
......
......@@ -83,8 +83,14 @@ type AudioConfig struct {
}
type DBConfig struct {
Type string `json:"type" yaml:"type" toml:"type"`
Connection string `json:"connection" yaml:"connection" toml:"connection"`
Type string `json:"type" yaml:"type" toml:"type"`
Host string `json:"host" yaml:"host" toml:"host"`
Port uint16 `json:"port" yaml:"port" toml:"port"`
TLS string `json:"tls" yaml:"tls" toml:"tls"`
Username string `json:"username" yaml:"username" toml:"username"`
Password string `json:"password" yaml:"password" toml:"password"`
DB string `json:"database" yaml:"database" toml:"database"`
Debug bool `json:"debug" yaml:"debug" toml:"debug"`
}
type Config struct {
......
#!/bin/bash
img="mysql"
tag="5.7"
# img="mariadb"
# tag="10.1" ## mariadb in debian stretch
case "$1" in
mariadb)
img="mariadb"
tag="10.1" ## mariadb in debian stretch
;;
*)
img="mysql"
tag="5.7"
;;
esac
SCRIPTS_D=$(realpath "${BASH_SOURCE%/*}/")
exec docker run -it --rm --name aura-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -v "$SCRIPTS_D:/scripts:ro" -p 127.0.0.1:3306:3306 "$img:$tag"
......@@ -26,6 +26,7 @@ package store
import (
"errors"
"fmt"
"os"
"github.com/jinzhu/gorm"
......@@ -46,13 +47,58 @@ type Store struct {
// Initialization and Destruction
//
func mysqlConnectionString(cfg DBConfig) (conn string) {
// "tank:aura@tcp(127.0.0.1:3306)/tank?charset=utf8&parseTime=True&loc=Local"
conn = cfg.Username
if cfg.Password != "" {
conn += ":" + cfg.Password
}
if cfg.Port > 0 {
conn += fmt.Sprintf("@tcp(%s:%d)", cfg.Host, cfg.Port)
} else {
conn += "@tcp(" + cfg.Host + ")"
}
conn += "/" + cfg.DB + "?parseTime=True&loc=Local"
if cfg.TLS != "" {
conn += "&tls=" + cfg.TLS
}
return
}
func postgresConnectionString(cfg DBConfig) (conn string) {
// "host=127.0.0.1 port=5432 user=tank password=aura dbname=tank sslmode=disable"
conn = "host=" + cfg.Host
if cfg.Port > 0 {
conn += fmt.Sprintf(" port=%d", cfg.Port)
}
conn += " user=" + cfg.Username
if cfg.Password != "" {
conn += " password=" + cfg.Password
}
conn += " dbname=" + cfg.DB
if cfg.TLS != "" {
conn += " sslmode=" + cfg.TLS
}
return
}
func openDB(cfg DBConfig) (db *gorm.DB, err error) {
if db, err = gorm.Open(cfg.Type, cfg.Connection); err != nil {
var conn string
switch cfg.Type {
case "mysql":
conn = mysqlConnectionString(cfg)
case "postgres":
conn = postgresConnectionString(cfg)
default:
return nil, errors.New("unknown database engine: " + cfg.Type)
}
if db, err = gorm.Open(cfg.Type, conn); err != nil {
return nil, errors.New("gorm.open(): " + err.Error())
}
// TODO: make debugging configurable
db.LogMode(true)
db.LogMode(cfg.Debug)
if err = db.DB().Ping(); err != nil {
return nil, errors.New("gorm.Ping(): " + err.Error())
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment