You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.1 KiB
Go

package config
// package serves to store/load config files for server
import (
"fmt"
"strconv"
"github.com/spf13/viper"
"FRMS/internal/pkg/logging"
"errors"
"sync"
"strings"
//"os"
//"log"
//"os/exec"
//"bytes"
)
type Config struct {
Server ServerConfig `mapstructure:"server"`
Reactors map[string]ReactorConfig `mapstructure:"reactors"`
sync.RWMutex
}
type ServerConfig struct {
URL string `mapstructure:"db-url"`
Token string `mapstructure:"db-token"`
Orginization string `mapstructure:"db-org"`
Ports map[string]int `mapstructure:"ports"` // changed from map[string]string to map[string]int
Name string `mapstructure:"name"`
}
type ReactorConfig struct {
Token string `mapstructure:"db-token"`
Bucket string `mapstructure:"db-bucket"`
Name string
Id uint32
}
var C *Config
func Load(fname string) {
// read stored configs
C = &Config{}
viper.SetConfigName(fname)
viper.SetConfigType("yaml")
viper.AddConfigPath("./configs")
//viper.AddConfigPath("../../internal/configs")
// defaults
viper.SetDefault("server.db-org", "ForeLight")
viper.SetDefault("server.db-url", "http://192.168.100.2:8086")
// unmarshalling
viper.ReadInConfig() // the fact i never did this is infuriating
logging.Debug(logging.DStart,"CON Loaded configs from %v", viper.ConfigFileUsed())
err := viper.Unmarshal(C)
if err != nil {
logging.Debug(logging.DError,"Cannot unmarshall Server! %v",err)
panic(err)
}
fmt.Printf("Outcome: %#v\n \n",C)
//fmt.Printf("%v\n",C)
// unmarshalled at this point
}
func LoadConfig() *Config {
return C
}
func (c *Config) GetURL() (string, error) {
c.RLock()
defer c.RUnlock()
return C.Server.URL, nil
}
func (c *Config) GetOrg() (string, error) {
c.RLock()
defer c.RUnlock()
return c.Server.Orginization, nil
}
func (c *Config) GetPort(port string) (int, error) {
c.RLock()
defer c.RUnlock()
portString, ok := c.Server.Ports[port]
if !ok {
portEnv := strings.ToUpper(port) + "_PORT"
return 0, fmt.Errorf("%s port doesnt exist! Please set using env %s=####",port, portEnv)
}
// returns int, err
//return strconv.Atoi(portString)
return portString, nil
}
func (c *Config) GetServerToken() (string, error) {
c.RLock()
defer c.RUnlock()
return c.Server.Token, nil
}
func (c *Config) GetReactorClient(id uint32) (string, string, error) {
c.RLock()
defer c.RUnlock()
idString := strconv.FormatUint(uint64(id),10)
if r, ok := c.Reactors[idString]; ok {
return r.Bucket, r.Token, nil
}
return "", "", fmt.Errorf("Reactor %v config doesnt exist!",id)
}
// setters
func (c *Config) UpdateURL(url string) error {
c.Lock()
defer c.Unlock()
if url == "" {
return errors.New("String cannot be empty!")
}
c.Server.URL = url
return viper.WriteConfig()
}
func (c *Config) UpdateOrg(org string) error {
c.Lock()
defer c.Unlock()
if org == "" {
return errors.New("String cannot be empty!")
}
c.Server.Orginization = org
return viper.WriteConfig()
}
func (c *Config) UpdatePort(pName string, port int) error {
c.Lock()
defer c.Unlock()
if port < 1024 || port > 65535 {
// OOB
return fmt.Errorf("Port %d out of bounds! [1024,65535]",port)
}
c.Server.Ports[pName] = port
return nil
}
func (c *Config) UpdateServerToken(token string) error {
c.Lock()
defer c.Unlock()
if token == "" {
return errors.New("String cannot be empty!")
}
c.Server.Token = token
return viper.WriteConfig()
}
func (c *Config) UpdateReactorClient(id uint32, bucket, token string) error {
c.Lock()
c.Unlock()
sid := strconv.FormatUint(uint64(id), 10)
if token == "" || bucket == "" {
return errors.New("String cannot be empty!")
}
if reactor, ok := c.Reactors[sid]; !ok {
c.Reactors[sid] = ReactorConfig{Token:token,Bucket:bucket,Id:id}
} else {
reactor.Bucket = bucket
reactor.Token = token
c.Reactors[sid] = reactor
}
return viper.WriteConfig()
}