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 *.tar.gz
# logs # logs
*.log *.log
# binaries generated in testing
cmd/server/server
cmd/reactor/reactor
cmd/tui/tui
# machine dependent
tokens/ tokens/
logs/ logs/

@ -9,7 +9,7 @@ import (
"os" "os"
"fmt" "fmt"
"FRMS/internal/pkg/logging" "FRMS/internal/pkg/logging"
//"FRMS/internal/pkg/config" "FRMS/internal/pkg/config"
"FRMS/internal/pkg/server" "FRMS/internal/pkg/server"
) )
@ -21,13 +21,17 @@ func NewCoordinator(port int, ch chan error) coordinator {
return server.NewCentralCoordinator(port, ch) return server.NewCentralCoordinator(port, ch)
} }
func LoadConfig(fname string) {
config.Load(fname)
}
func main() { func main() {
// lets get this bread // lets get this bread
// go func() { // go func() {
// fmt.Println(http.ListenAndServe("localhost:6060",nil)) // fmt.Println(http.ListenAndServe("localhost:6060",nil))
// }() // }()
LoadConfig("server")
ch := make(chan error) ch := make(chan error)
var port int var port int
var err error 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 // package serves to store/load config files for server
import ( import (
_ "fmt" "fmt"
"strconv"
"github.com/spf13/viper" "github.com/spf13/viper"
"FRMS/internal/pkg/logging" "FRMS/internal/pkg/logging"
"log" "errors"
"os/exec" //"os"
"bytes" //"log"
"strings" //"os/exec"
//"bytes"
//"strings"
) )
type serverconfig struct { type Config struct {
URL string Server ServerConfig `mapstructure:"server"`
Token string Reactors map[string]ReactorConfig `mapstructure:"reactors"`
Bucket string
Orginization string
} }
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.SetConfigType("yaml")
viper.AddConfigPath("./internal/configs") viper.AddConfigPath("./configs")
viper.SetDefault("Orginization","ForeLight") viper.AddConfigPath("../../internal/configs")
viper.SetDefault("URL","http://localhost:8086") // defaults
var C serverconfig viper.SetDefault("server.db-org", "ForeLight")
err := viper.Unmarshal(&C) 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 { if err != nil {
logging.Debug(logging.DError,"Cannot unmarshal! %v",err) logging.Debug(logging.DError,"Cannot unmarshall Server! %v",err)
log.Fatal(err) panic(err)
} }
if C.Token == "" { fmt.Printf("Outcome: %#v\n \n",C)
// token unset fmt.Printf("%v\n",C)
logging.Debug(logging.DClient,"CON Grabbing adming token") // unmarshalled at this point
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
} }
func (s *serverconfig) GetUrl() string { func LoadStruct() *Config {
return s.URL return C
} }
func (c *Config) GetURL() (string, error) {
return C.Server.URL, nil
}
func (s *serverconfig) GetOrg() string { func (c *Config) GetOrg() (string, error) {
return s.Orginization return c.Server.Orginization, nil
} }
func (s *serverconfig) GetBucket() string { func (c *Config) GetServerToken() (string, error) {
return s.Bucket return c.Server.Token, nil
} }
func (s *serverconfig) GetToken() string { func (c *Config) GetReactorToken(id uint32) (string, error) {
return s.Token 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 package influxdb
import ( import (
"fmt" _ "fmt"
"github.com/influxdata/influxdb-client-go/v2" _ "github.com/influxdata/influxdb-client-go/v2"
) )

@ -9,15 +9,44 @@ import (
"FRMS/internal/pkg/logging" "FRMS/internal/pkg/logging"
"google.golang.org/grpc" "google.golang.org/grpc"
pb "FRMS/internal/pkg/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 // 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 { type CentralCoordinator struct {
ClientConnections *ClientPacket ClientConnections *ClientPacket
CLisPort int CLisPort int
*SubCoordinators *SubCoordinators
*SystemViewer *SystemViewer
Config
Err chan error Err chan error
} }
@ -28,6 +57,7 @@ type SubCoordinators struct {
func NewCentralCoordinator(port int, ch chan error) *CentralCoordinator { func NewCentralCoordinator(port int, ch chan error) *CentralCoordinator {
c := &CentralCoordinator{CLisPort: port, Err: ch} c := &CentralCoordinator{CLisPort: port, Err: ch}
c.Config = LoadConfig()
c.SystemViewer = NewSystemViewer() c.SystemViewer = NewSystemViewer()
go c.SystemViewer.Start() go c.SystemViewer.Start()
s := make(map[string]*SubCoordinator) s := make(map[string]*SubCoordinator)

Loading…
Cancel
Save