Library/models/config.go

60 lines
1.1 KiB
Go
Raw Normal View History

package models
import (
"github.com/go-ini/ini"
"os"
)
2017-11-16 11:51:23 +00:00
// ConfigStruct holds the config struct
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
}
JWTLoginSecret []byte
2017-11-16 12:09:30 +00:00
Interface string
FirstUser User `ini:"User"`
}
2017-11-16 11:51:23 +00:00
// Config holds the configuration for the program
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
}
2017-11-16 12:04:45 +00:00
// Load the config
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
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"
}
// JWT secret
Config.JWTLoginSecret = []byte(cfg.Section("General").Key("JWTSecret").String())
return nil
2017-11-07 15:35:10 +00:00
}