added config parser for reactor and server settings, need to add store method

main
Keegan 2 years ago
parent a887028e7a
commit 247ba5fdbc

5
.gitignore vendored

@ -21,6 +21,11 @@ bin
*.tar.gz
# logs
*.log
# binaries generated in testing
cmd/server/server
cmd/reactor/reactor
cmd/tui/tui
# machine dependent
tokens/
logs/

@ -9,7 +9,7 @@ import (
"os"
"fmt"
"FRMS/internal/pkg/logging"
//"FRMS/internal/pkg/config"
"FRMS/internal/pkg/config"
"FRMS/internal/pkg/server"
)
@ -21,13 +21,17 @@ func NewCoordinator(port int, ch chan error) coordinator {
return server.NewCentralCoordinator(port, ch)
}
func LoadConfig(fname string) {
config.Load(fname)
}
func main() {
// lets get this bread
// go func() {
// fmt.Println(http.ListenAndServe("localhost:6060",nil))
// }()
LoadConfig("server")
ch := make(chan error)
var port int
var err error

@ -1,21 +0,0 @@
server:
id: 1000213123
name: "Rack Server"
db-url: "http://192.168.100.2:3000"
db-token: ""
l-port: 2022
r-port: 2023
t-port: 2024
reactor:
id: 102233
name: "Beaglebone Black"
i2c-bus: 2
db-token: ""
db-bucket: ""
id: 220123123
name: "Raspberry Pi"
tui:
id: 10000
name: "kdeppe"
db-token: ""
db-bucket: ""

@ -0,0 +1,17 @@
server:
name: "Rack Server"
db-url: "http://192.168.100.2:8086"
db-org: "ForeLight"
db-token: ""
ports:
lis: 2022
reactor: 2023
tui: 2024
db: 8086
reactors:
10002123:
name: "Beaglebone Black"
db-token: ""
db-bucket: ""

@ -3,68 +3,83 @@ package config
// package serves to store/load config files for server
import (
_ "fmt"
"fmt"
"strconv"
"github.com/spf13/viper"
"FRMS/internal/pkg/logging"
"log"
"os/exec"
"bytes"
"strings"
"errors"
//"os"
//"log"
//"os/exec"
//"bytes"
//"strings"
)
type serverconfig struct {
URL string
Token string
Bucket string
Orginization string
type Config struct {
Server ServerConfig `mapstructure:"server"`
Reactors map[string]ReactorConfig `mapstructure:"reactors"`
}
func ReadServerConfig() *serverconfig {
type ServerConfig struct {
URL string `mapstructure:"db-url"`
Token string `mapstructure:"db-token"`
Orginization string `mapstructure:"db-org"`
Ports map[string]string `mapstructure:"ports"`
Name string `mapstructure:"name"`
}
type ReactorConfig struct {
Token string `mapstructure:"db-token"`
Bucket string `mapstructure:"db-bucket"`
Name string
Id uint32
}
var C *Config
viper.SetConfigName("database")
func Load(fname string) {
// read stored configs
C = &Config{}
viper.SetConfigName(fname)
viper.SetConfigType("yaml")
viper.AddConfigPath("./internal/configs")
viper.SetDefault("Orginization","ForeLight")
viper.SetDefault("URL","http://localhost:8086")
var C serverconfig
err := viper.Unmarshal(&C)
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 unmarshal! %v",err)
log.Fatal(err)
logging.Debug(logging.DError,"Cannot unmarshall Server! %v",err)
panic(err)
}
if C.Token == "" {
// token unset
logging.Debug(logging.DClient,"CON Grabbing adming token")
cmd := exec.Command("cat","tokens/admin_token")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
logging.Debug(logging.DError,"CON Error grabbing token %v",err)
log.Fatal(err)
}
outstring := out.String()
C.Token = strings.Trim(outstring," \n")
viper.Set("token",C.Token)
viper.WriteConfig()
}
return &C
fmt.Printf("Outcome: %#v\n \n",C)
fmt.Printf("%v\n",C)
// unmarshalled at this point
}
func (s *serverconfig) GetUrl() string {
return s.URL
func LoadStruct() *Config {
return C
}
func (c *Config) GetURL() (string, error) {
return C.Server.URL, nil
}
func (s *serverconfig) GetOrg() string {
return s.Orginization
func (c *Config) GetOrg() (string, error) {
return c.Server.Orginization, nil
}
func (s *serverconfig) GetBucket() string {
return s.Bucket
func (c *Config) GetServerToken() (string, error) {
return c.Server.Token, nil
}
func (s *serverconfig) GetToken() string {
return s.Token
func (c *Config) GetReactorToken(id uint32) (string, error) {
idString := strconv.FormatUint(uint64(id),10)
if r, ok := c.Reactors[idString]; ok {
return r.Token, nil
}
return "", errors.New(fmt.Sprintf("Reactor %v config not found!",id))
}

@ -1,6 +1,6 @@
package influxdb
import (
"fmt"
"github.com/influxdata/influxdb-client-go/v2"
_ "fmt"
_ "github.com/influxdata/influxdb-client-go/v2"
)

@ -9,15 +9,44 @@ import (
"FRMS/internal/pkg/logging"
"google.golang.org/grpc"
pb "FRMS/internal/pkg/grpc"
"FRMS/internal/pkg/config"
_ "FRMS/internal/pkg/influxdb"
)
// this package creates the central coordiantor and sub coordiantors for clients
// interfaces
// config interface
func LoadConfig() Config {
return config.LoadStruct()
}
type Config interface {
GetURL() (string, error)
GetOrg() (string, error)
GetServerToken() (string,error)
GetReactorToken(uint32) (string, error)
}
// db client interface
type DBClient interface{
GetBucket(uint32) (string, error) // will create if it doesn't exist
GetToken(uint32) (string, error) // will create if it doesn't exist
GetURL() (string, error) // returns the ipaddres + dbport
}
/*func NewDBClient() DBClient {
return influxdb.NewServerClient()
}*/
type CentralCoordinator struct {
ClientConnections *ClientPacket
CLisPort int
*SubCoordinators
*SystemViewer
Config
Err chan error
}
@ -28,6 +57,7 @@ type SubCoordinators struct {
func NewCentralCoordinator(port int, ch chan error) *CentralCoordinator {
c := &CentralCoordinator{CLisPort: port, Err: ch}
c.Config = LoadConfig()
c.SystemViewer = NewSystemViewer()
go c.SystemViewer.Start()
s := make(map[string]*SubCoordinator)

Loading…
Cancel
Save