Skip to content

Commit

Permalink
feat: adding json string support to bindings config (#2685)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer authored May 10, 2024
1 parent 026d804 commit be5471c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 317 deletions.
10 changes: 5 additions & 5 deletions examples/cbindings/waku_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,13 @@ int main(int argc, char** argv) {

char jsonConfig[2048];
snprintf(jsonConfig, 2048, "{ \
\"host\": \"%s\", \
\"port\": %d, \
\"key\": \"%s\", \
\"listenAddress\": \"%s\", \
\"tcpPort\": %d, \
\"nodekey\": \"%s\", \
\"relay\": %s, \
\"store\": %s, \
\"storeDbUrl\": \"%s\", \
\"storeRetentionPolicy\": \"%s\", \
\"storeMessageDbUrl\": \"%s\", \
\"storeMessageRetentionPolicy\": \"%s\", \
\"storeMaxNumDbConnections\": %d \
}", cfgNode.host,
cfgNode.port,
Expand Down
272 changes: 0 additions & 272 deletions library/waku_thread/config.nim

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/options
import std/sequtils
import chronos, chronicles, stew/results, stew/shims/net
import std/[options, sequtils, json, strutils]
import chronos, chronicles, stew/results, stew/shims/net, confutils, confutils/std/net

import
../../../../waku/common/enr/builder,
../../../../waku/waku_enr/capabilities,
Expand All @@ -24,8 +24,7 @@ import
../../../../waku/factory/node_factory,
../../../../waku/factory/networks_config,
../../../events/[json_message_event, json_base_event],
../../../alloc,
../../config
../../../alloc

type NodeLifecycleMsgType* = enum
CREATE_NODE
Expand All @@ -49,19 +48,22 @@ proc destroyShared(self: ptr NodeLifecycleRequest) =
deallocShared(self)

proc createWaku(configJson: cstring): Future[Result[Waku, string]] {.async.} =
var conf: WakuNodeConf
var conf = defaultWakuNodeConf().valueOr:
return err("Failed creating node: " & error)

var errorResp: string

try:
if not parseConfig($configJson, conf, errorResp):
return err(errorResp)
except Exception:
return err("exception calling parseConfig: " & getCurrentExceptionMsg())
let jsonNode = parseJson($configJson)

# TODO: figure out how to extract default values from the config pragma
conf.nat = "any"
conf.maxConnections = 50.uint16
conf.maxMessageSize = default_values.DefaultMaxWakuMessageSizeStr
for confField, confValue in fieldPairs(conf):
if jsonNode.contains(confField):
# Make sure string doesn't contain the leading or trailing " character
let formattedString = ($jsonNode[confField]).strip(chars = {'\"'})
# Override conf field with the value set in the json-string
confValue = parseCmdArg(typeof(confValue), formattedString)
except Exception:
return err("exception parsing configuration: " & getCurrentExceptionMsg())

# The Waku Network config (cluster-id=1)
if conf.clusterId == 1:
Expand Down
Loading

0 comments on commit be5471c

Please sign in to comment.