refactor: move app code to cmd/waku

This commit is contained in:
Richard Ramos 2023-07-06 17:40:57 -04:00 committed by richΛrd
parent b26859fc6a
commit ca297713fa
14 changed files with 27 additions and 23 deletions

View File

@ -79,11 +79,11 @@ var (
Destination: &options.Websocket.CertPath,
EnvVars: []string{"WAKUNODE2_WEBSOCKET_SECURE_CERT_PATH"},
})
Dns4DomainName = altsrc.NewStringFlag(&cli.StringFlag{
DNS4DomainName = altsrc.NewStringFlag(&cli.StringFlag{
Name: "dns4-domain-name",
Value: "",
Usage: "The domain name resolving to the node's public IPv4 address",
Destination: &options.Dns4DomainName,
Destination: &options.DNS4DomainName,
EnvVars: []string{"WAKUNODE2_WEBSOCKET_DNS4_DOMAIN_NAME"},
})
NodeKey = cliutils.NewGenericFlagSingleValue(&cli.GenericFlag{

View File

@ -5,11 +5,10 @@ import (
cli "github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
"github.com/waku-org/go-waku/waku"
"github.com/waku-org/go-waku/waku/v2/node"
)
var options waku.Options
var options Options
func main() {
// Defaults
@ -27,7 +26,7 @@ func main() {
WebsocketSecureSupport,
WebsocketSecureKeyPath,
WebsocketSecureCertPath,
Dns4DomainName,
DNS4DomainName,
NodeKey,
KeyFile,
KeyPassword,
@ -110,7 +109,7 @@ func main() {
Before: altsrc.InitInputSourceWithContext(cliFlags, altsrc.NewTomlSourceFromFlagFunc("config-file")),
Flags: cliFlags,
Action: func(c *cli.Context) error {
waku.Execute(options)
Execute(options)
return nil
},
}

View File

@ -1,4 +1,4 @@
package waku
package main
import (
"context"
@ -18,6 +18,7 @@ import (
"github.com/pbnjay/memory"
"github.com/waku-org/go-waku/waku/persistence/sqlite"
dbutils "github.com/waku-org/go-waku/waku/persistence/utils"
wmetrics "github.com/waku-org/go-waku/waku/v2/metrics"
"github.com/waku-org/go-waku/waku/v2/peers"
"github.com/waku-org/go-waku/waku/v2/rendezvous"
@ -104,7 +105,7 @@ func Execute(options Options) {
var db *sql.DB
var migrationFn func(*sql.DB) error
if requiresDB(options) {
db, migrationFn, err = ExtractDBAndMigration(options.Store.DatabaseURL)
db, migrationFn, err = dbutils.ExtractDBAndMigration(options.Store.DatabaseURL)
failOnErr(err, "Could not connect to DB")
}
@ -141,8 +142,8 @@ func Execute(options Options) {
nodeOpts = append(nodeOpts, node.WithExternalIP(ip))
}
if options.Dns4DomainName != "" {
nodeOpts = append(nodeOpts, node.WithDns4Domain(options.Dns4DomainName))
if options.DNS4DomainName != "" {
nodeOpts = append(nodeOpts, node.WithDns4Domain(options.DNS4DomainName))
}
libp2pOpts := node.DefaultLibP2POptions
@ -386,12 +387,12 @@ func Execute(options Options) {
if options.PeerExchange.Enable && options.PeerExchange.Node != nil {
logger.Info("retrieving peer info via peer exchange protocol")
peerId, err := wakuNode.AddPeer(*options.PeerExchange.Node, peers.Static, peer_exchange.PeerExchangeID_v20alpha1)
peerID, err := wakuNode.AddPeer(*options.PeerExchange.Node, peers.Static, peer_exchange.PeerExchangeID_v20alpha1)
if err != nil {
logger.Error("adding peer exchange peer", logging.MultiAddrs("node", *options.PeerExchange.Node), zap.Error(err))
} else {
desiredOutDegree := wakuNode.Relay().Params().D
if err = wakuNode.PeerExchange().Request(ctx, desiredOutDegree, peer_exchange.WithPeer(peerId)); err != nil {
if err = wakuNode.PeerExchange().Request(ctx, desiredOutDegree, peer_exchange.WithPeer(peerID)); err != nil {
logger.Error("requesting peers via peer exchange", zap.Error(err))
}
}

View File

@ -1,7 +1,7 @@
//go:build !gowaku_rln
// +build !gowaku_rln
package waku
package main
import (
"github.com/waku-org/go-waku/waku/v2/node"

View File

@ -1,7 +1,7 @@
//go:build gowaku_rln
// +build gowaku_rln
package waku
package main
import (
"crypto/ecdsa"

View File

@ -1,4 +1,4 @@
package waku
package main
import (
"crypto/ecdsa"
@ -30,6 +30,9 @@ type RelayOptions struct {
MinRelayPeersToPublish int
}
// RLNRelayOptions are settings used to enable RLN Relay. This is a protocol
// used to rate limit messages and penalize those attempting to send more than
// N messages per epoch
type RLNRelayOptions struct {
Enable bool
CredentialsPath string
@ -134,6 +137,7 @@ type PeerExchangeOptions struct {
Node *multiaddr.Multiaddr
}
// RendezvousOptions are settings used with the rendezvous protocol
type RendezvousOptions struct {
Enable bool
Server bool
@ -145,7 +149,7 @@ type RendezvousOptions struct {
type Options struct {
Port int
Address string
Dns4DomainName string
DNS4DomainName string
NodeKey *ecdsa.PrivateKey
KeyFile string
KeyPasswd string

View File

@ -1,6 +1,6 @@
//go:build !linux && !darwin && !windows
package waku
package main
import "runtime"

View File

@ -1,6 +1,6 @@
//go:build linux || darwin
package waku
package main
import (
"fmt"

View File

@ -1,6 +1,6 @@
//go:build windows
package waku
package main
import (
"math"

View File

@ -107,7 +107,6 @@ int main(int argc, char *argv[])
return 1;
printf("%s\n",response);
*/
response = waku_relay_subscribe(NULL);
if (isError(response))
return 1;

View File

@ -25,8 +25,8 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
libp2pProtocol "github.com/libp2p/go-libp2p/core/protocol"
"github.com/multiformats/go-multiaddr"
"github.com/waku-org/go-waku/waku"
"github.com/waku-org/go-waku/waku/persistence"
dbutils "github.com/waku-org/go-waku/waku/persistence/utils"
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/payload"
"github.com/waku-org/go-waku/waku/v2/peers"
@ -115,7 +115,7 @@ func NewNode(configJSON string) string {
if *config.EnableStore {
var db *sql.DB
var migrationFn func(*sql.DB) error
db, migrationFn, err = waku.ExtractDBAndMigration(*config.DatabaseURL)
db, migrationFn, err = dbutils.ExtractDBAndMigration(*config.DatabaseURL)
if err != nil {
return MakeJSONResponse(err)
}

View File

@ -1,4 +1,4 @@
package waku
package utils
import (
"database/sql"
@ -18,6 +18,7 @@ func validateDBUrl(val string) error {
return nil
}
// ExtractDBAndMigration will return a database connection, and migration function that should be used depending on a database connection string
func ExtractDBAndMigration(databaseURL string) (*sql.DB, func(*sql.DB) error, error) {
var db *sql.DB
var migrationFn func(*sql.DB) error