diff --git a/.gitignore b/.gitignore index 1d4bf1b..1566793 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/cmd/server/main.go b/cmd/server/main.go index fa6497d..eaef3c2 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -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 diff --git a/internal/configs/boards.yaml b/internal/configs/boards.yaml deleted file mode 100644 index c20aacf..0000000 --- a/internal/configs/boards.yaml +++ /dev/null @@ -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: "" diff --git a/internal/configs/server.yaml b/internal/configs/server.yaml new file mode 100644 index 0000000..f09fcc1 --- /dev/null +++ b/internal/configs/server.yaml @@ -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: "" + + diff --git a/internal/pkg/config/server.go b/internal/pkg/config/server.go index 1f37ccb..7cadc3d 100644 --- a/internal/pkg/config/server.go +++ b/internal/pkg/config/server.go @@ -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)) } diff --git a/internal/pkg/influxdb/client.go b/internal/pkg/influxdb/client.go index 36a1f03..edac4cd 100644 --- a/internal/pkg/influxdb/client.go +++ b/internal/pkg/influxdb/client.go @@ -1,6 +1,6 @@ package influxdb import ( - "fmt" - "github.com/influxdata/influxdb-client-go/v2" + _ "fmt" + _ "github.com/influxdata/influxdb-client-go/v2" ) diff --git a/internal/pkg/server/coordinator.go b/internal/pkg/server/coordinator.go index dc1a215..3ddfefd 100644 --- a/internal/pkg/server/coordinator.go +++ b/internal/pkg/server/coordinator.go @@ -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)