2017-10-10 08:40:36 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-ini/ini"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2017-11-16 11:51:23 +00:00
|
|
|
// ConfigStruct holds the config struct
|
2017-11-16 11:28:27 +00:00
|
|
|
type ConfigStruct struct {
|
2017-11-07 15:35:10 +00:00
|
|
|
Database struct {
|
2017-12-05 10:49:19 +00:00
|
|
|
Type string
|
2017-11-16 12:09:30 +00:00
|
|
|
Host string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Database string
|
2017-12-05 10:49:19 +00:00
|
|
|
Path string
|
2017-11-16 12:09:30 +00:00
|
|
|
ShowQueries bool
|
2017-10-10 08:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JWTLoginSecret []byte
|
2017-11-16 12:09:30 +00:00
|
|
|
Interface string
|
2017-11-16 11:28:27 +00:00
|
|
|
|
|
|
|
FirstUser User `ini:"User"`
|
2017-10-10 08:40:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 11:51:23 +00:00
|
|
|
// Config holds the configuration for the program
|
2017-11-16 11:28:27 +00:00
|
|
|
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 {
|
2017-10-10 08:40:36 +00:00
|
|
|
|
|
|
|
// File Checks
|
|
|
|
if _, err := os.Stat("config.ini"); os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-16 12:04:45 +00:00
|
|
|
// Load the config
|
2017-10-10 08:40:36 +00:00
|
|
|
cfg, err := ini.Load("config.ini")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-16 12:04:45 +00:00
|
|
|
// Map the config to our struct
|
2017-11-16 11:28:27 +00:00
|
|
|
err = cfg.MapTo(Config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-16 12:04:45 +00:00
|
|
|
// Set default value for interface to listen on
|
|
|
|
Config.Interface = cfg.Section("General").Key("Interface").String()
|
|
|
|
if Config.Interface == "" {
|
|
|
|
Config.Interface = ":8080"
|
|
|
|
}
|
|
|
|
|
2017-10-10 08:40:36 +00:00
|
|
|
// JWT secret
|
|
|
|
Config.JWTLoginSecret = []byte(cfg.Section("General").Key("JWTSecret").String())
|
|
|
|
|
|
|
|
return nil
|
2017-11-07 15:35:10 +00:00
|
|
|
}
|