Skip to content

Commit

Permalink
WIP: structuralise config
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 23, 2024
1 parent caf13f4 commit 36e38f5
Show file tree
Hide file tree
Showing 28 changed files with 657 additions and 572 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@
"GOLOG_OUTPUT": "file"
}
},
{
"name": "Launch relay",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/relay/",
"console": "integratedTerminal",
"env": {
"GOLOG_FILE": "/tmp/debug.log",
"GOLOG_LOG_LEVEL": "debug",
"GOLOG_STDOUT": "false",
"GOLOG_OUTPUT": "file"
}
},
{
"name": "go-ma-actor generate",
"type": "go",
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ It provides The ability to browse IPFS properly, and to pin files and directorie

### Cros-compiling

Cross-cmpiling for Windows requires gcc-mingw-w64 and gcc-multilib tools to be installed.

CGO is required for sqlite to work on Windows™.
Cross-compiling for Windows requires gcc-mingw-w64 and gcc-multilib tools to be installed.

## TL;DR

Expand Down
3 changes: 1 addition & 2 deletions cmd/actor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma-actor/p2p"
"github.com/bahner/go-ma-actor/ui"
Expand All @@ -19,7 +18,7 @@ func main() {
)

fmt.Println("Initialising actor configuation...")
actor.InitConfig(config.Profile())
// actor.InitConfig(config.Profile())

// P2P
fmt.Println("Setting default p2p options...")
Expand Down
16 changes: 16 additions & 0 deletions cmd/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ func init() {
viper.SetDefault("node.debug_interval", defaultNodeDebugInterval)

}

func NodeSpace() string {
return viper.GetString("node.space")
}

func NodeCookie() string {
return viper.GetString("node.cookie")
}

func NodeName() string {
return viper.GetString("node.name")
}

func NodeDebugInterval() time.Duration {
return viper.GetDuration("node.debug_interval")
}
2 changes: 1 addition & 1 deletion cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {

// Init config and logger
config.SetProfile(name)
actor.InitConfig(config.Profile())
// actor.InitConfig(config.Profile())

p, err := p2p.Init(p2p.DefaultOptions())
if err != nil {
Expand Down
84 changes: 37 additions & 47 deletions cmd/node/template.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,48 @@
package main

import (
"time"

"github.com/bahner/go-ma-actor/config"
"github.com/spf13/viper"
)

func configTemplate(identity string, node string) map[string]interface{} {
type NodeStruct struct {
Cookie string `yaml:"cookie"`
Name string `yaml:"name"`
Space string `yaml:"space"`
DebugInterval time.Duration `yaml:"debug-interval"`
}

// Get the default settings as a map
// Note: Viper does not have a built-in way to directly extract only the config
// so we manually recreate the structure based on the config we have set.
return map[string]interface{}{
"actor": map[string]interface{}{
"identity": identity,
"location": config.ActorLocation(),
"nick": config.ActorNick(),
},
"db": map[string]interface{}{
"dir": config.DefaultDbPath,
},
// Use default log settings, so as not to pick up debug log settings
"log": map[string]interface{}{
"level": viper.GetString("log.level"),
"file": viper.GetString("log.file"),
},
// NB! This is a cross over from go-ma
"api": map[string]interface{}{
// This must be set corretly for generation to work
"maddr": viper.GetString("api.maddr"),
},
"http": map[string]interface{}{
"socket": config.HttpSocket(),
},
"p2p": map[string]interface{}{
"identity": node,
"port": config.P2PPort(),
"connmgr": map[string]interface{}{
"low-watermark": config.P2PConnmgrLowWatermark(),
"high-watermark": config.P2PConnmgrHighWatermark(),
"grace-period": config.P2PConnMgrGracePeriod(),
},
"discovery": map[string]interface{}{
"advertise-ttl": config.P2PDiscoveryAdvertiseTTL(),
"advertise-limit": config.P2PDiscoveryAdvertiseLimit(),
"advertise-interval": config.P2PDiscoveryAdvertiseInterval(),
"dht": config.P2PDiscoveryDHT(),
"mdns": config.P2PDiscoveryMDNS(),
type NodeConfigStruct struct {
Node NodeStruct `yaml:"node"`
}

type NodeConfig struct {
Actor config.ActorConfigStruct `yaml:"actor"`
API config.APIConfigStruct `yaml:"api"`
DB config.DBConfigStruct `yaml:"db"`
HTTP config.HTTPConfigStruct `yaml:"http"`
Node NodeConfigStruct `yaml:"node"`
Log config.LogConfigStruct `yaml:"log"`
P2P config.P2PConfigStruct `yaml:"p2p"`
}

func Config() NodeConfig {

return NodeConfig{
Actor: config.ActorConfig(),
API: config.APIConfig(),
DB: config.DBConfig(),
HTTP: config.HTTPConfig(),
Node: NodeConfigStruct{
Node: NodeStruct{
Cookie: NodeCookie(),
Name: NodeName(),
Space: NodeSpace(),
DebugInterval: NodeDebugInterval(),
},
},
"node": map[string]interface{}{
"cookie": defaultNodeCookie,
"name": defaultNodeName,
"debug_interval": defaultNodeDebugInterval,
"space": defautSpaceNodeName,
},
Log: config.LogConfig(),
P2P: config.P2PConfig(),
}
}
139 changes: 54 additions & 85 deletions cmd/pong/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ package main

import (
"errors"
"os"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/p2p"
"github.com/bahner/go-ma/did/doc"
"github.com/libp2p/go-libp2p"
p2pDHT "github.com/libp2p/go-libp2p-kad-dht"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

const (
defaultPongReply = "Pong!"
defaultFortuneMode = false
pong = "pong"
profile = pong
)

var defaultFortuneArgs = []string{"-s"}
Expand All @@ -34,47 +32,71 @@ func init() {
viper.SetDefault("mode.pong.fortune.args", defaultFortuneArgs)
}

func p2pOptions() p2p.Options {
return p2p.Options{
DHT: []p2pDHT.Option{
p2pDHT.Mode(p2pDHT.ModeServer),
},
P2P: []libp2p.Option{
libp2p.DefaultTransports,
libp2p.DefaultSecurity,
libp2p.EnableRelay(),
libp2p.EnableRelayService(),
libp2p.Ping(true),
}}
type FortuneStruct struct {
Enable bool `yaml:"enable"`
Args []string `yaml:"args"`
}

func initConfig(profile string) {
type PongStruct struct {
Reply string `yaml:"reply"`
Fortune FortuneStruct `yaml:"fortune"`
}

type PongConfigStruct struct {
Pong PongStruct `yaml:"pong"`
}

type ModeConfigStruct struct {
Mode PongConfigStruct `yaml:"mode"`
}

type PongConfig struct {
Mode ModeConfigStruct `yaml:"mode"`
API config.APIConfigStruct `yaml:"api"`
Actor config.ActorConfigStruct `yaml:"actor"`
DB config.DBConfigStruct `yaml:"db"`
Log config.LogConfigStruct `yaml:"log"`
}

func Config() PongConfig {

// Always parse the flags first
config.InitCommonFlags()
config.InitActorFlags()
pflag.Parse()

// Always parse the flags first
config.SetProfile(profile)
config.Init()

if config.GenerateFlag() {
// Reinit logging to STDOUT
log.SetOutput(os.Stdout)
log.Info("Generating new actor and node identity")
actor, node := generateActorIdentitiesOrPanic(pong)
pongConfig := configTemplate(actor, node)
config.Generate(pongConfig)
os.Exit(0)
return PongConfig{
Mode: ModeConfigStruct{
Mode: PongConfigStruct{
Pong: PongStruct{
Reply: pongReply(),
Fortune: FortuneStruct{
Enable: pongFortuneMode(),
Args: pongFortuneArgs(),
},
},
},
},
API: config.APIConfig(),
Actor: config.ActorConfig(),
DB: config.DBConfig(),
Log: config.LogConfig(),
}
}

config.InitActor()
func (c *PongConfig) MarshalToYAML() ([]byte, error) {
return yaml.Marshal(c)
}

// This flag is dependent on the actor to be initialized to make sense.
if config.ShowConfigFlag() {
config.Print()
os.Exit(0)
func (c *PongConfig) Print() {
y, err := c.MarshalToYAML()
if err != nil {
log.Fatal(err)
}

log.Println(string(y))
}

func pongFortuneMode() bool {
Expand All @@ -100,56 +122,3 @@ func generateActorIdentitiesOrPanic(name string) (string, string) {
}
return actor, node
}

func configTemplate(identity string, node string) map[string]interface{} {

// Get the default settings as a map
// Note: Viper does not have a built-in way to directly extract only the config
// so we manually recreate the structure based on the config we have set.
return map[string]interface{}{
"actor": map[string]interface{}{
"identity": identity,
"nick": pong,
},
"db": map[string]interface{}{
"dir": config.DefaultDbPath,
},
"log": map[string]interface{}{
"level": config.LogLevel(),
"file": config.LogFile(),
},
// NB! This is a cross over from go-ma
"api": map[string]interface{}{
// This must be set corretly for generation to work
"maddr": viper.GetString("api.maddr"),
},
"http": map[string]interface{}{
"socket": config.HttpSocket(),
},
"p2p": map[string]interface{}{
"identity": node,
"port": config.P2PPort(),
"connmgr": map[string]interface{}{
"low-watermark": config.P2PConnmgrLowWatermark(),
"high-watermark": config.P2PConnmgrHighWatermark(),
"grace-period": config.P2PConnMgrGracePeriod(),
},
"discovery": map[string]interface{}{
"advertise-ttl": config.P2PDiscoveryAdvertiseTTL(),
"advertise-limit": config.P2PDiscoveryAdvertiseLimit(),
"advertise-interval": config.P2PDiscoveryAdvertiseInterval(),
"dht": config.P2PDiscoveryDHT(),
"mdns": config.P2PDiscoveryMDNS(),
},
},
"mode": map[string]interface{}{
"pong": map[string]interface{}{
"reply": pongReply(),
"fortune": map[string]interface{}{
"enable": pongFortuneMode(),
"args": pongFortuneArgs(),
},
},
},
}
}
10 changes: 8 additions & 2 deletions cmd/pong/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"os"

"github.com/bahner/go-ma-actor/entity/actor"

Expand All @@ -13,10 +14,15 @@ import (
func main() {

ctx := context.Background()
initConfig(pong)

c := Config()
c.Print()
if true {
os.Exit(0)
}

// THese are the relay specific parts.
p, err := p2p.Init(p2pOptions())
p, err := p2p.Init(p2p.DefaultOptions())
if err != nil {
fmt.Printf("Failed to initialize p2p: %v\n", err)
return
Expand Down
Loading

0 comments on commit 36e38f5

Please sign in to comment.