preparing config for lux routing
This commit is contained in:
parent
3172def440
commit
bd15749c24
3 changed files with 65 additions and 10 deletions
|
|
@ -130,12 +130,17 @@ func (ks *LuxKeyStore) Put(key LuxKey) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (ks *LuxKeyStore) Count() int {
|
||||
return len(ks.keys)
|
||||
}
|
||||
|
||||
func (ks *LuxKeyStore) Keys() []LuxKey {
|
||||
values := make([]LuxKey, len(ks.keys))
|
||||
|
||||
i := 0
|
||||
for _, value := range ks.keys {
|
||||
values[i] = value
|
||||
i += 1
|
||||
}
|
||||
|
||||
return values
|
||||
|
|
|
|||
|
|
@ -9,43 +9,80 @@ import (
|
|||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
type luxConfigRoute struct {
|
||||
Id string `ini:"id"`
|
||||
Channel string `ini:"channel"`
|
||||
Address string `ini:"address"`
|
||||
}
|
||||
|
||||
type luxConfig struct {
|
||||
Host struct {
|
||||
Name string `ini:"name"`
|
||||
Nodes string `ini:"nodes"`
|
||||
Name string `ini:"name"`
|
||||
} `ini:"host"`
|
||||
|
||||
KeyStore struct {
|
||||
Path string `ini:"path"`
|
||||
} `ini:"keystore"`
|
||||
|
||||
Routes []luxConfigRoute
|
||||
}
|
||||
|
||||
var config luxConfig
|
||||
|
||||
func LuxHostEntry(configPath string) error {
|
||||
ini, err := ini.Load(configPath)
|
||||
ini, err := ini.LoadSources(ini.LoadOptions{
|
||||
AllowNonUniqueSections: true,
|
||||
}, configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = ini.MapTo(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// parse routes
|
||||
{
|
||||
sections, err := ini.SectionsByName("route")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
config.Routes = make([]luxConfigRoute, 0)
|
||||
for _, section := range sections {
|
||||
route := luxConfigRoute{}
|
||||
if err = section.MapTo(&route); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
config.Routes = append(config.Routes, route)
|
||||
}
|
||||
}
|
||||
fmt.Println(config)
|
||||
|
||||
ks := crypto.NewLuxKeyStore(config.KeyStore.Path)
|
||||
key, err := crypto.NewLuxKey(proto.LuxTypeHost)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := ks.Load(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := ks.Put(key); err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
var hostA, hostB crypto.LuxKey
|
||||
if ks.Count() < 2 {
|
||||
hostA, _ = crypto.NewLuxKey(proto.LuxTypeHost)
|
||||
hostB, _ = crypto.NewLuxKey(proto.LuxTypeHost)
|
||||
|
||||
if err := ks.Put(hostA); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := ks.Put(hostB); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
keys := ks.Keys()
|
||||
hostA, hostB = keys[0], keys[1]
|
||||
}
|
||||
|
||||
fmt.Println(key.String())
|
||||
fmt.Println(hostA.String())
|
||||
fmt.Println(hostB.String())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package proto
|
||||
|
||||
import "errors"
|
||||
|
||||
type LuxType uint
|
||||
|
||||
const (
|
||||
|
|
@ -9,6 +11,17 @@ const (
|
|||
|
||||
const LUX_PROTO_TYPE_SIZE = 2
|
||||
|
||||
func LuxTypeFromString(name string) (LuxType, error) {
|
||||
switch name {
|
||||
case "host":
|
||||
return LuxTypeHost, nil
|
||||
case "node":
|
||||
return LuxTypeNode, nil
|
||||
default:
|
||||
return LuxTypeHost, errors.New("unknown lux type")
|
||||
}
|
||||
}
|
||||
|
||||
func (luxType *LuxType) Read(rd *LuxBuffer) error {
|
||||
if val, err := rd.ReadUint16(); err != nil {
|
||||
return err
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue