Moved database connection to configfile

This commit is contained in:
konrad 2017-11-16 12:57:08 +01:00 committed by kolaente
parent 9630e8fda2
commit c98c593307
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 21 additions and 21 deletions

View File

@ -15,14 +15,14 @@ type UserLogin struct {
func main() {
// Set Engine
err := models.SetEngine()
// Init Config
err := models.SetConfig()
if err != nil {
fmt.Println(err)
}
// Init Config
err = models.SetConfig()
// Set Engine
err = models.SetEngine()
if err != nil {
fmt.Println(err)
}

View File

@ -1,7 +1,6 @@
package models
import (
"fmt"
"github.com/go-ini/ini"
"os"
)
@ -41,21 +40,6 @@ func SetConfig() error {
return err
}
// Check if the first user already exists, aka a user with the ID = 1. If not, insert it
_, exists, err := GetUserByID(1)
if err != nil {
return err
}
// If it doesn't exist, create it
if !exists {
_, err = CreateUser(Config.FirstUser)
if err != nil {
return err
}
fmt.Println("Created new user " + Config.FirstUser.Username)
}
// JWT secret
Config.JWTLoginSecret = []byte(cfg.Section("General").Key("JWTSecret").String())

View File

@ -11,7 +11,7 @@ var x *xorm.Engine
func getEngine() (*xorm.Engine, error) {
connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true",
"root", "jup2000", "127.0.0.1", "library2")
Config.Database.User, Config.Database.Password, Config.Database.Host, Config.Database.Database)
return xorm.NewEngine("mysql", connStr)
}
@ -34,5 +34,21 @@ func SetEngine() (err error) {
x.Sync(&Quantity{})
x.ShowSQL(true)
// Check if the first user already exists, aka a user with the ID = 1. If not, insert it
_, exists, err := GetUserByID(1)
if err != nil {
return err
}
// If it doesn't exist, create it
if !exists {
_, err = CreateUser(Config.FirstUser)
if err != nil {
return err
}
fmt.Println("Created new user " + Config.FirstUser.Username)
}
return nil
}