diff --git a/.gitmodules b/.gitmodules index 5e8ddd4ba..acc36aff9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -140,3 +140,6 @@ url = https://github.com/ba0f3/dnsclient.nim.git ignore = untracked branch = master +[submodule "vendor/nim-toml-serialization"] + path = vendor/nim-toml-serialization + url = https://github.com/status-im/nim-toml-serialization.git diff --git a/CHANGELOG.md b/CHANGELOG.md index ff16c0299..351a6c030 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The full list of changes is below. - Support for bootstrapping [`33/WAKU-DISCV5`](https://rfc.vac.dev/spec/33) via [DNS discovery](https://rfc.vac.dev/spec/10/#discovery-methods) - Support for GossipSub [Peer Exchange](https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#prune-backoff-and-peer-exchange) +- Support for TOML config files via `--config-file=` ### Changes diff --git a/examples/v2/example_config.toml b/examples/v2/example_config.toml new file mode 100644 index 000000000..05335507e --- /dev/null +++ b/examples/v2/example_config.toml @@ -0,0 +1,11 @@ +relay=true +store=true +filter=true +db-path="./dbs/db1" +rpc-admin=true +persist-peers=true +keep-alive=true +persist-messages=true +lightpush=true +nodekey="f157b19b13e9ee818acfc9d3d7eec6b81f70c0a978dec19def261172acbe26e6" +ports-shift=1 diff --git a/vendor/nim-confutils b/vendor/nim-confutils index 05a438414..d06f6187d 160000 --- a/vendor/nim-confutils +++ b/vendor/nim-confutils @@ -1 +1 @@ -Subproject commit 05a438414a91ae6729c6f1966358909840368e0c +Subproject commit d06f6187dca702ad884b9b948f52b26bf6596765 diff --git a/vendor/nim-toml-serialization b/vendor/nim-toml-serialization new file mode 160000 index 000000000..90369dd67 --- /dev/null +++ b/vendor/nim-toml-serialization @@ -0,0 +1 @@ +Subproject commit 90369dd67b4a41109e26716829f6f3f077eddf38 diff --git a/waku/v2/node/config.nim b/waku/v2/node/config.nim index a50719901..ca40fe614 100644 --- a/waku/v2/node/config.nim +++ b/waku/v2/node/config.nim @@ -1,6 +1,8 @@ import std/strutils, confutils, confutils/defs, confutils/std/net, + confutils/toml/defs as confTomlDefs, + confutils/toml/std/net as confTomlNet, chronicles, chronos, libp2p/crypto/crypto, libp2p/crypto/secp, @@ -8,11 +10,19 @@ import eth/keys, ../protocol/waku_rln_relay/waku_rln_relay_types, ../protocol/waku_message + +export + confTomlDefs, + confTomlNet type WakuNodeConf* = object ## General node config + configFile* {. + desc: "Loads configuration from a TOML file (cmd-line parameters take precedence)" + name: "config-file" }: Option[InputFile] + logLevel* {. desc: "Sets the log level." defaultValue: LogLevel.INFO @@ -348,3 +358,11 @@ func defaultListenAddress*(conf: WakuNodeConf): ValidIpAddress = # TODO: How should we select between IPv4 and IPv6 # Maybe there should be a config option for this. (static ValidIpAddress.init("0.0.0.0")) + + +proc readValue*(r: var TomlReader, val: var crypto.PrivateKey) + {.raises: [Defect, IOError, SerializationError].} = + val = try: parseCmdArg(crypto.PrivateKey, r.readValue(string)) + except CatchableError as err: + raise newException(SerializationError, err.msg) + diff --git a/waku/v2/node/wakunode2.nim b/waku/v2/node/wakunode2.nim index decf1058c..95ab79728 100644 --- a/waku/v2/node/wakunode2.nim +++ b/waku/v2/node/wakunode2.nim @@ -972,7 +972,7 @@ when isMainModule: ## 6. Setup graceful shutdown hooks import - confutils, + confutils, toml_serialization, system/ansi_c, libp2p/nameresolving/dnsresolver, ../../common/utils/nat, @@ -1293,10 +1293,19 @@ when isMainModule: Port(conf.metricsServerPort + conf.portsShift)) ok(true) # Success - - let - conf = WakuNodeConf.load() - + + {.push warning[ProveInit]: off.} + let conf = try: + WakuNodeConf.load( + secondarySources = proc (conf: WakuNodeConf, sources: auto) = + if conf.configFile.isSome: + sources.addConfigFile(Toml, conf.configFile.get) + ) + except CatchableError as err: + error "Failure while loading the configuration: \n", err_msg=err.msg + quit 1 # if we don't leave here, the initialization of conf does not work in the success case + {.pop.} + var node: WakuNode # This is the node we're going to setup using the conf