Skip to content

Commit

Permalink
Make config modeless
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 17, 2024
1 parent f78a5ca commit ae0aaee
Show file tree
Hide file tree
Showing 26 changed files with 89 additions and 913 deletions.
35 changes: 3 additions & 32 deletions cmd/actor/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package main

import (
"context"
"fmt"
"os"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/config/db"
"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma-actor/mode/pong"
"github.com/bahner/go-ma-actor/p2p"
"github.com/bahner/go-ma-actor/ui"
"github.com/spf13/pflag"
Expand All @@ -24,15 +21,10 @@ func main() {
)

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

// MODE

// Then init the config
// There's a lot of stuff going on in here.
mode := config.Mode()
config.Init(mode)
fmt.Printf("Starting in %s mode.\n", mode)
config.Init()

// DB
fmt.Println("Initialising DB ...")
Expand All @@ -54,33 +46,12 @@ func main() {
panic(fmt.Sprintf("failed to initialize peer: %v", err))
}

// P2P Relay mode
// Relay mode doesn't need either ui or an actor.
// So let's just start it quickly and stop here.
if config.RelayMode() {
fmt.Println("Starting relay mode...")
go p2P.StartDiscoveryLoop(context.Background())
startWebServer(p2P, nil)
os.Exit(0) // This won't be reached.
}

// ACTOR
a := initActorOrPanic()

// Start the webserver in the background. Ignore - but log - errors.
go startWebServer(p2P, a)

// Pong mode needs the
if config.PongMode() {
// In Pong we can just stop here. We dont' need to display anything.
// or handle input events. Hence this is a blocking call.
log.Infof("Running in Pong mode")
pong.Run(a, p2P)
log.Warnf("Pong run loop ended, exiting...")
os.Exit(0)

}

// We have a valid actor, but for it to be useful, we need to discover peers.
// discoverPeersOrPanic(p2P)

Expand Down
20 changes: 0 additions & 20 deletions cmd/actor/mode.go

This file was deleted.

22 changes: 0 additions & 22 deletions cmd/actor/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package main
import (
"fmt"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/mode/pong"
"github.com/bahner/go-ma-actor/mode/relay"
"github.com/bahner/go-ma-actor/p2p"
"github.com/bahner/go-ma-actor/p2p/connmgr"
)
Expand All @@ -20,25 +17,6 @@ func initP2P() (P2P *p2p.P2P, err error) {
}
cg := connmgr.NewConnectionGater(cm)

if config.RelayMode() {
fmt.Println("Relay mode enabled.")
d, err := relay.DHT(cg)
if err != nil {
panic(fmt.Sprintf("failed to initialize dht: %v", err))
}
return p2p.Init(d)
}

if config.PongMode() {
fmt.Println("Pong mode enabled.")
d, err := pong.DHT(cg)
if err != nil {
panic(fmt.Sprintf("failed to initialize dht: %v", err))
}
return p2p.Init(d)
}

fmt.Println("Actor mode enabled.")
d, err := DHT(cg)
if err != nil {
panic(fmt.Sprintf("failed to initialize dht: %v", err))
Expand Down
2 changes: 1 addition & 1 deletion mode/mode.go → cmd/actor/peers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mode
package main

import (
"sort"
Expand Down
20 changes: 4 additions & 16 deletions cmd/actor/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/entity/actor"
mactor "github.com/bahner/go-ma-actor/mode/actor"
"github.com/bahner/go-ma-actor/mode/relay"

"github.com/bahner/go-ma-actor/p2p"
log "github.com/sirupsen/logrus"
Expand All @@ -25,21 +23,11 @@ func startWebServer(p *p2p.P2P, a *actor.Actor) {
// Start a simple web server to handle incoming requests.
// This is defined in web.go. It makes it possible to add extra parameters to the handler.
mux := http.NewServeMux()

// Different handlers for diiferent modes.
if config.RelayMode() {
h := &relay.WebHandlerData{
P2P: p,
}
mux.HandleFunc("/", h.WebHandler)

} else {
h := &mactor.WebHandlerData{
P2P: p,
Entity: a.Entity,
}
mux.HandleFunc("/", h.WebHandler)
h := &WebHandlerData{
P2P: p,
Entity: a.Entity,
}
mux.HandleFunc("/", h.WebHandler)

log.Infof("Listening on %s", config.HttpSocket())

Expand Down
16 changes: 3 additions & 13 deletions mode/actor/web.go → cmd/actor/web_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package entity
package main

import (
"fmt"
Expand All @@ -7,7 +7,6 @@ import (
"github.com/bahner/go-ma"
"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/mode"
"github.com/bahner/go-ma-actor/p2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -43,15 +42,6 @@ func webHandler(w http.ResponseWriter, _ *http.Request, p *p2p.P2P, e *entity.En

titleStr := fmt.Sprintf("Entity: %s", e.DID.Id)
h1str := titleStr

if config.PongMode() {
h1str = fmt.Sprintf("%s (Pong mode)", titleStr)
}

if config.PongFortuneMode() {
h1str = fmt.Sprintf("%s (Pong mode with fortune cookies)", titleStr)
}

doc.Title = titleStr
doc.H1 = h1str
doc.H2 = fmt.Sprintf("%s@%s", ma.RENDEZVOUS, (p.Host.ID().String()))
Expand Down Expand Up @@ -93,12 +83,12 @@ func (d *WebHandlerDocument) String() string {
// Peers with Same Rendezvous
if len(d.PeersWithSameRendez) > 0 {
html += fmt.Sprintf("<h2>Discovered peers (%d):</h2>\n", len(d.PeersWithSameRendez))
html += mode.UnorderedListFromPeerIDSlice(d.PeersWithSameRendez)
html += UnorderedListFromPeerIDSlice(d.PeersWithSameRendez)
}
// All Connected Peers
if len(d.AllConnectedPeers) > 0 {
html += fmt.Sprintf("<h2>libp2p Network Peers (%d):</h2>\n", len(d.AllConnectedPeers))
html += mode.UnorderedListFromPeerIDSlice(d.AllConnectedPeers)
html += UnorderedListFromPeerIDSlice(d.AllConnectedPeers)
}

html += "</body>\n</html>"
Expand Down
24 changes: 10 additions & 14 deletions config/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,22 @@ var (
ErrEmptyNick = fmt.Errorf("nick is empty")
)

func init() {
pflag.Bool("generate", false, "Generates a new keyset")
pflag.Bool("publish", false, "Publishes keyset to IPFS")
pflag.Bool("force", false, "Forces regneration of config keyset and publishing")
func InitActorFlags() {

}

// Load a keyset from string and initiate an Actor.
// This is optional, but if you want to use the actor package, you need to call this.
func InitActor() {

pflag.StringP("nick", "n", defaultNick, "Nickname to use in character creation")
pflag.StringP("nick", "n", "", "Nickname to use in character creation")
pflag.StringP("location", "l", defaultLocation, "DID of the location to visit")

// Settings required for config file generation.
viper.BindPFlag("actor.nick", pflag.Lookup("nick"))
viper.SetDefault("actor.nick", defaultNick)

viper.BindPFlag("actor.location", pflag.Lookup("location"))

viper.SetDefault("actor.location", defaultLocation)
viper.SetDefault("actor.nick", defaultNick)

}

// Load a keyset from string and initiate an Actor.
// This is optional, but if you want to use the actor package, you need to call this.
func InitActor() {

keyset_string := actorIdentity()
if keyset_string == fakeActorIdentity {
Expand Down
10 changes: 0 additions & 10 deletions config/api.go

This file was deleted.

Loading

0 comments on commit ae0aaee

Please sign in to comment.