Super broken but what are you gonna do
parent
b1520db055
commit
0ccac301f1
@ -0,0 +1,52 @@
|
||||
package config
|
||||
|
||||
// def.go serves as a central place to view/edit config structs and device manager map
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Server Config
|
||||
|
||||
type ServerConf struct {
|
||||
// Structure to demarshall into
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
Reactors map[string]ReactorConfig `mapstructure:"reactors"`
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
// Server config settings
|
||||
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"`
|
||||
}
|
||||
|
||||
// Reactor Config
|
||||
|
||||
type ReactorConf struct {
|
||||
// Structure to demarshall to
|
||||
Reactor ReactorConfig `mapstructure:"reactor"`
|
||||
Devices map[int]DeviceConfig `mapstructure:"devices"`
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type ReactorConfig struct {
|
||||
// Reactor config settings
|
||||
Token string `mapstructure:"db-token"`
|
||||
Bucket string `mapstructure:"db-bucket"`
|
||||
URL string `mapstructure:"db-url",omitempty` // only needed by reactor
|
||||
Name string `mapstructure:"name",omitempty` // not always set
|
||||
Id uint32 `mapstructure:"id"`
|
||||
}
|
||||
|
||||
// Device Config
|
||||
|
||||
type DeviceConfig struct {
|
||||
// Device config settings
|
||||
Address uint32 `mapstructure:"address"`
|
||||
Interval uint32 `mapstructure:"interval"`
|
||||
Name string `mapstructure:"name"`
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package config
|
||||
|
||||
/*
|
||||
Package provides a way to update current config based on values passed in flags
|
||||
*/
|
@ -0,0 +1,172 @@
|
||||
package config
|
||||
|
||||
/*
|
||||
Load.go contains methods to load values from config, flags and env.
|
||||
*/
|
||||
|
||||
import (
|
||||
"FRMS/internal/pkg/logging"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
//"os"
|
||||
//"log"
|
||||
//"os/exec"
|
||||
//"bytes"
|
||||
)
|
||||
|
||||
func NewConfig(name string) (Config, error) {
|
||||
// returns a Config Structure of assocaited name
|
||||
return
|
||||
|
||||
type ConfigStruct interface {
|
||||
// structure to do demarshall config into
|
||||
LoadFile(string) error
|
||||
}
|
||||
|
||||
func LoadConfigFile(fname string, strct ConfigStruct) error {
|
||||
// Demarshalls a given filename into the struct
|
||||
// returns nil if successful
|
||||
logging.Debug(logging.DStart, "Loading config for %s", fname)
|
||||
viper.SetConfigName(fname)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath("/etc/frms/config")
|
||||
|
||||
// unmarshalling
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logging.Debug(logging.DStart, "CON Loaded configs from %v", viper.ConfigFileUsed())
|
||||
if err = viper.Unmarshal(strct); err != nil {
|
||||
logging.Debug(logging.DError, "Cannot unmarshall %v", err)
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Outcome: %#v\n \n", C)
|
||||
// unmarshalled at this point
|
||||
}
|
||||
|
||||
////
|
||||
|
||||
func LoadConfig() Config {
|
||||
return C
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetURL() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return C.Server.URL, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) GetOrg() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return c.Server.Orginization, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) 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 *ServerConf) GetServerToken() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return c.Server.Token, nil
|
||||
}
|
||||
|
||||
func (c *ServerConf) 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 *ServerConf) UpdateURL(url string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if url == "" {
|
||||
return errors.New("string cannot be empty")
|
||||
}
|
||||
c.Server.URL = url
|
||||
viper.Set("server.db-url", url)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) UpdateOrg(org string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if org == "" {
|
||||
return errors.New("string cannot be empty")
|
||||
}
|
||||
c.Server.Orginization = org
|
||||
viper.Set("server.db-org", org)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) 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)
|
||||
}
|
||||
if c.Server.Ports == nil {
|
||||
c.Server.Ports = make(map[string]int)
|
||||
}
|
||||
c.Server.Ports[pName] = port
|
||||
pname := fmt.Sprintf("server.ports.%s", pName)
|
||||
viper.Set(pname, port)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) UpdateServerToken(token string) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if token == "" {
|
||||
return errors.New("String cannot be empty!")
|
||||
}
|
||||
c.Server.Token = token
|
||||
viper.Set("server.token", token)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) 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
|
||||
}
|
||||
reactorbucket := fmt.Sprintf("%s.db-bucket", id)
|
||||
reactortoken := fmt.Sprintf("%s.db-token", id)
|
||||
viper.Set(reactorbucket, bucket)
|
||||
viper.Set(reactortoken, token)
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
func (c *ServerConf) Store() error {
|
||||
return viper.WriteConfigAs(viper.ConfigFileUsed())
|
||||
}
|
@ -1,26 +1,3 @@
|
||||
package config
|
||||
|
||||
// pacakge serves to store/load config files for reactor
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ReactorConf struct {
|
||||
Reactor ReactorConfig `mapstructure:"reactor"`
|
||||
Devices map[int]DeviceConfig `mapstructure:"devices"`
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type DeviceConfig struct {
|
||||
Address uint32 `mapstructure:"address"`
|
||||
Interval uint32 `mapstructure:"interval"`
|
||||
Name string `mapstructure:"name"`
|
||||
}
|
||||
|
||||
// loaded in other file
|
||||
func (c *ReactorConf) GetURL() (string, error) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
return c.Reactor.URL, nil
|
||||
}
|
||||
// pacakge serves to provide reactor config implementation
|
||||
|
Loading…
Reference in New Issue