Library/models/config.go

63 lines
1.1 KiB
Go
Raw Normal View History

package models
import (
"github.com/go-ini/ini"
"os"
"fmt"
)
2017-11-08 09:55:17 +00:00
// Config holds the config struct
type ConfigStruct struct {
2017-11-07 15:35:10 +00:00
Database struct {
Host string
User string
Password string
Database string
}
JWTLoginSecret []byte
FirstUser User `ini:"User"`
}
var Config = new(ConfigStruct)
2017-11-08 09:55:17 +00:00
// SetConfig initianlises the config and publishes it for other functions to use
2017-11-07 15:35:10 +00:00
func SetConfig() error {
// File Checks
if _, err := os.Stat("config.ini"); os.IsNotExist(err) {
return err
}
cfg, err := ini.Load("config.ini")
if err != nil {
return err
}
err = cfg.MapTo(Config)
if err != nil {
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())
return nil
2017-11-07 15:35:10 +00:00
}