begin working on entry point

This commit is contained in:
mykola2312 2025-01-21 09:00:15 +02:00
parent 923c57ab70
commit a8534a5abc
3 changed files with 33 additions and 105 deletions

5
go.mod
View file

@ -3,7 +3,6 @@ module lux
go 1.23.4
require (
github.com/google/uuid v1.6.0 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
github.com/google/uuid v1.6.0
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
)

View file

@ -1,88 +0,0 @@
package host
import (
"fmt"
"log"
"lux/crypto"
"lux/proto"
"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"`
} `ini:"host"`
KeyStore struct {
Path string `ini:"path"`
} `ini:"keystore"`
Routes []luxConfigRoute
}
var config luxConfig
func LuxHostEntry(configPath string) error {
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)
if err := ks.Load(); 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(hostA.String())
fmt.Println(hostB.String())
return nil
}

45
main.go
View file

@ -2,27 +2,44 @@ package main
import (
"flag"
"log"
"lux/host"
"fmt"
"os"
"strings"
)
var configPath string
var isHost bool
var isNode bool
var isHost bool
var isRpc bool
var configPath string
var rpcPath string
func main() {
flag.StringVar(&configPath, "config", "", "/etc/lux/lux-host.conf or /etc/lux/lux-node.conf")
flag.BoolVar(&isHost, "host", false, "act as LUX host")
flag.BoolVar(&isNode, "node", false, "act as LUX node")
// first, we need to determine who we are: node, host or rpc.
// determine by executable name (lux binary will be symlinked to lux-node, lux-host, luc-rpc),
// or by explicit cli flag (--node, --host, --rpc)
flag.BoolVar(&isNode, "node", false, "LUX node")
flag.BoolVar(&isHost, "host", false, "LUX host")
flag.BoolVar(&isRpc, "rpc", false, "RPC tool")
flag.StringVar(&configPath, "config", "", "node or host config")
flag.StringVar(&rpcPath, "rpc-path", "", "path to RPC UNIX socket or TCP socket, must be in unix:// or tcp:// form")
flag.Parse()
if isHost {
if err := host.LuxHostEntry(configPath); err != nil {
log.Fatal(err)
if !isNode && !isHost && !isRpc {
// determine by argv[0]
if strings.Contains(os.Args[0], "node") {
isNode = true
} else if strings.Contains(os.Args[0], "host") {
isHost = true
} else if strings.Contains(os.Args[0], "rpc") {
isRpc = true
}
} else if isNode {
log.Fatal("not yet implemented")
} else {
log.Fatal("You must specify either --host or --node with appropriate --config")
}
if (isNode || isHost) && configPath == "" {
fmt.Fprintln(os.Stderr, "must provide config path")
os.Exit(1)
} else if isRpc && rpcPath == "" {
fmt.Fprintln(os.Stderr, "must provide RPC socket path")
os.Exit(1)
}
}