@ -17,7 +17,7 @@ import (
//"bytes"
//"bytes"
)
)
type Config struct {
type Server Conf struct {
Server ServerConfig ` mapstructure:"server" `
Server ServerConfig ` mapstructure:"server" `
Reactors map [ string ] ReactorConfig ` mapstructure:"reactors" `
Reactors map [ string ] ReactorConfig ` mapstructure:"reactors" `
sync . RWMutex
sync . RWMutex
@ -34,21 +34,36 @@ type ServerConfig struct {
type ReactorConfig struct {
type ReactorConfig struct {
Token string ` mapstructure:"db-token" `
Token string ` mapstructure:"db-token" `
Bucket string ` mapstructure:"db-bucket" `
Bucket string ` mapstructure:"db-bucket" `
Name string
URL string ` mapstructure:"db-url",omitempty ` // only needed by reactor
Id uint32
Name string ` mapstructure:"name",omitempty ` // not always set
Id uint32 ` mapstructure:"id" `
}
}
var C * Config
type Config interface {
Store ( ) error
}
var C Config
func Load ( fname string ) {
func Load ( fname string ) error {
// read stored configs
// read stored configs
C = & Config { }
//C = &ServerConf{}
switch fname {
case "server" :
C = & ServerConf { }
case "reactor" :
C = & ReactorConf { }
default :
return fmt . Errorf ( "%s not recognized" , fname )
}
logging . Debug ( logging . DStart , "Loading config for %s" , fname )
viper . SetConfigName ( fname )
viper . SetConfigName ( fname )
viper . SetConfigType ( "yaml" )
viper . SetConfigType ( "yaml" )
viper . AddConfigPath ( "/etc/frms/config" ) //
viper . AddConfigPath ( "/etc/frms/config" )
// defaults
// defaults which we don't want to set nessecarily
viper . SetDefault ( "server.db-org" , "ForeLight" )
//viper.SetDefault("server.db-org", "ForeLight")
viper . SetDefault ( "server.db-url" , "http://192.168.100.2:8086" )
//viper.SetDefault("server.db-url", "http://192.168.100.2:8086")
//viper.SetDefault("reactor.db-url", "http://192.168.100.2:8086")
// unmarshalling
// unmarshalling
err := viper . ReadInConfig ( ) // the fact i never did this is infuriating
err := viper . ReadInConfig ( ) // the fact i never did this is infuriating
if err != nil {
if err != nil {
@ -65,23 +80,23 @@ func Load(fname string) {
// unmarshalled at this point
// unmarshalled at this point
}
}
func LoadConfig ( ) * Config {
func LoadConfig ( ) Config {
return C
return C
}
}
func ( c * Config ) GetURL ( ) ( string , error ) {
func ( c * Server Conf) GetURL ( ) ( string , error ) {
c . RLock ( )
c . RLock ( )
defer c . RUnlock ( )
defer c . RUnlock ( )
return C . Server . URL , nil
return C . Server . URL , nil
}
}
func ( c * Config ) GetOrg ( ) ( string , error ) {
func ( c * Server Conf) GetOrg ( ) ( string , error ) {
c . RLock ( )
c . RLock ( )
defer c . RUnlock ( )
defer c . RUnlock ( )
return c . Server . Orginization , nil
return c . Server . Orginization , nil
}
}
func ( c * Config ) GetPort ( port string ) ( int , error ) {
func ( c * Server Conf) GetPort ( port string ) ( int , error ) {
c . RLock ( )
c . RLock ( )
defer c . RUnlock ( )
defer c . RUnlock ( )
portString , ok := c . Server . Ports [ port ]
portString , ok := c . Server . Ports [ port ]
@ -94,46 +109,46 @@ func (c *Config) GetPort(port string) (int, error) {
return portString , nil
return portString , nil
}
}
func ( c * Config ) GetServerToken ( ) ( string , error ) {
func ( c * Server Conf) GetServerToken ( ) ( string , error ) {
c . RLock ( )
c . RLock ( )
defer c . RUnlock ( )
defer c . RUnlock ( )
return c . Server . Token , nil
return c . Server . Token , nil
}
}
func ( c * Config ) GetReactorClient ( id uint32 ) ( string , string , error ) {
func ( c * Server Conf) GetReactorClient ( id uint32 ) ( string , string , error ) {
c . RLock ( )
c . RLock ( )
defer c . RUnlock ( )
defer c . RUnlock ( )
idString := strconv . FormatUint ( uint64 ( id ) , 10 )
idString := strconv . FormatUint ( uint64 ( id ) , 10 )
if r , ok := c . Reactors [ idString ] ; ok {
if r , ok := c . Reactors [ idString ] ; ok {
return r . Bucket , r . Token , nil
return r . Bucket , r . Token , nil
}
}
return "" , "" , fmt . Errorf ( " Reactor %v config doesnt exist! ", id )
return "" , "" , fmt . Errorf ( " reactor %v config doesnt exist ", id )
}
}
// setters
// setters
func ( c * Config ) UpdateURL ( url string ) error {
func ( c * Server Conf) UpdateURL ( url string ) error {
c . Lock ( )
c . Lock ( )
defer c . Unlock ( )
defer c . Unlock ( )
if url == "" {
if url == "" {
return errors . New ( " String cannot be empty! ")
return errors . New ( " string cannot be empty ")
}
}
c . Server . URL = url
c . Server . URL = url
viper . Set ( "server.db-url" , url )
viper . Set ( "server.db-url" , url )
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
}
}
func ( c * Config ) UpdateOrg ( org string ) error {
func ( c * Server Conf) UpdateOrg ( org string ) error {
c . Lock ( )
c . Lock ( )
defer c . Unlock ( )
defer c . Unlock ( )
if org == "" {
if org == "" {
return errors . New ( " String cannot be empty! ")
return errors . New ( " string cannot be empty ")
}
}
c . Server . Orginization = org
c . Server . Orginization = org
viper . Set ( "server.db-org" , org )
viper . Set ( "server.db-org" , org )
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
}
}
func ( c * Config ) UpdatePort ( pName string , port int ) error {
func ( c * Server Conf) UpdatePort ( pName string , port int ) error {
c . Lock ( )
c . Lock ( )
defer c . Unlock ( )
defer c . Unlock ( )
if port < 1024 || port > 65535 {
if port < 1024 || port > 65535 {
@ -149,7 +164,7 @@ func (c *Config) UpdatePort(pName string, port int) error {
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
}
}
func ( c * Config ) UpdateServerToken ( token string ) error {
func ( c * Server Conf) UpdateServerToken ( token string ) error {
c . Lock ( )
c . Lock ( )
defer c . Unlock ( )
defer c . Unlock ( )
if token == "" {
if token == "" {
@ -160,7 +175,7 @@ func (c *Config) UpdateServerToken(token string) error {
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
}
}
func ( c * Config ) UpdateReactorClient ( id uint32 , bucket , token string ) error {
func ( c * Server Conf) UpdateReactorClient ( id uint32 , bucket , token string ) error {
c . Lock ( )
c . Lock ( )
c . Unlock ( )
c . Unlock ( )
sid := strconv . FormatUint ( uint64 ( id ) , 10 )
sid := strconv . FormatUint ( uint64 ( id ) , 10 )
@ -181,6 +196,6 @@ func (c *Config) UpdateReactorClient(id uint32, bucket, token string) error {
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
}
}
func ( c * Config ) Store ( ) error {
func ( c * Server Conf) Store ( ) error {
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
return viper . WriteConfigAs ( viper . ConfigFileUsed ( ) )
}
}