diff --git a/config.ini.sample b/config.ini.sample index 864b3b7..2fb3767 100644 --- a/config.ini.sample +++ b/config.ini.sample @@ -4,11 +4,14 @@ JWTSecret = blablaGEHEMIN§)!§ Interface = :8080 [Database] +Type = mysql User = root Password = supersecret Host = 127.0.0.1 Database = library ShowQueries = false +; When using sqlite, this is the path where to store the data +; Path = ./library.db ; First user to be created, on every startup the program checks if he exists, if not it creates it [User] diff --git a/models/config.go b/models/config.go index 894b2b6..5d65cd1 100644 --- a/models/config.go +++ b/models/config.go @@ -8,10 +8,12 @@ import ( // ConfigStruct holds the config struct type ConfigStruct struct { Database struct { + Type string Host string User string Password string Database string + Path string ShowQueries bool } diff --git a/models/models.go b/models/models.go index 429532f..6a16e6b 100644 --- a/models/models.go +++ b/models/models.go @@ -2,7 +2,7 @@ package models import ( "fmt" - //_ "github.com/mattn/go-sqlite3" // Because. + _ "github.com/mattn/go-sqlite3" // Because. _ "github.com/go-sql-driver/mysql" // Because. "github.com/go-xorm/core" "github.com/go-xorm/xorm" @@ -11,10 +11,19 @@ import ( var x *xorm.Engine func getEngine() (*xorm.Engine, error) { - connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true", - Config.Database.User, Config.Database.Password, Config.Database.Host, Config.Database.Database) - return xorm.NewEngine("mysql", connStr) - //return xorm.NewEngine("sqlite3", "./db.db") + // Use Mysql if set + if Config.Database.Type == "mysql" { + connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true", + Config.Database.User, Config.Database.Password, Config.Database.Host, Config.Database.Database) + return xorm.NewEngine("mysql", connStr) + } else { + // Otherwise use sqlite + path := Config.Database.Path + if path == "" { + path = "./db.db" + } + return xorm.NewEngine("sqlite3", path) + } } // SetEngine sets the xorm.Engine @@ -40,6 +49,7 @@ func SetEngine() (err error) { x.Sync(&Quantity{}) x.Sync(&quantityRelation{}) x.Sync(&Item{}) + x.Sync(&UserLog{}) x.ShowSQL(Config.Database.ShowQueries)