2023-04-25 13:34:57 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[options, strutils, sequtils],
|
|
|
|
stew/results,
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
2023-10-27 07:11:47 +00:00
|
|
|
libp2p/wire,
|
|
|
|
libp2p/multicodec,
|
2023-04-25 13:34:57 +00:00
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/nameresolving/dnsresolver,
|
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
|
|
|
libp2p/peerid,
|
|
|
|
eth/keys,
|
2023-04-26 17:25:18 +00:00
|
|
|
presto,
|
|
|
|
metrics,
|
|
|
|
metrics/chronos_httpserver
|
2023-04-25 13:34:57 +00:00
|
|
|
import
|
2023-05-17 16:32:53 +00:00
|
|
|
../../waku/common/utils/nat,
|
2024-01-03 12:11:50 +00:00
|
|
|
../../waku/common/utils/parse_size_units,
|
2023-06-22 09:27:40 +00:00
|
|
|
../../waku/common/databases/db_sqlite,
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/waku_archive/driver/builder,
|
|
|
|
../../waku/waku_archive/retention_policy/builder,
|
|
|
|
../../waku/waku_core,
|
|
|
|
../../waku/waku_node,
|
|
|
|
../../waku/node/waku_metrics,
|
|
|
|
../../waku/node/peer_manager,
|
|
|
|
../../waku/node/peer_manager/peer_store/waku_peer_storage,
|
2023-09-22 13:36:46 +00:00
|
|
|
../../waku/waku_api/message_cache,
|
2023-10-27 19:43:54 +00:00
|
|
|
../../waku/waku_api/handlers,
|
2023-09-22 13:36:46 +00:00
|
|
|
../../waku/waku_api/rest/server,
|
|
|
|
../../waku/waku_api/rest/debug/handlers as rest_debug_api,
|
|
|
|
../../waku/waku_api/rest/relay/handlers as rest_relay_api,
|
|
|
|
../../waku/waku_api/rest/filter/handlers as rest_filter_api,
|
|
|
|
../../waku/waku_api/rest/lightpush/handlers as rest_lightpush_api,
|
|
|
|
../../waku/waku_api/rest/store/handlers as rest_store_api,
|
|
|
|
../../waku/waku_api/rest/health/handlers as rest_health_api,
|
2023-10-05 12:00:09 +00:00
|
|
|
../../waku/waku_api/rest/admin/handlers as rest_admin_api,
|
2023-09-26 11:33:52 +00:00
|
|
|
../../waku/waku_archive,
|
2024-04-17 19:48:20 +00:00
|
|
|
../../waku/discovery/waku_dnsdisc,
|
|
|
|
../../waku/discovery/waku_discv5,
|
2024-01-30 12:15:23 +00:00
|
|
|
../../waku/waku_enr/sharding,
|
2023-09-26 11:33:52 +00:00
|
|
|
../../waku/waku_peer_exchange,
|
|
|
|
../../waku/waku_rln_relay,
|
|
|
|
../../waku/waku_store,
|
2024-01-30 12:28:21 +00:00
|
|
|
../../waku/waku_lightpush/common,
|
2023-09-26 11:33:52 +00:00
|
|
|
../../waku/waku_filter_v2,
|
2024-03-03 00:59:53 +00:00
|
|
|
../../waku/factory/node_factory,
|
|
|
|
../../waku/factory/internal_config,
|
|
|
|
../../waku/factory/external_config
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "wakunode app"
|
|
|
|
|
|
|
|
# Git version in git describe format (defined at compile time)
|
|
|
|
const git_version* {.strdefine.} = "n/a"
|
|
|
|
|
|
|
|
type
|
|
|
|
App* = object
|
|
|
|
version: string
|
|
|
|
conf: WakuNodeConf
|
|
|
|
rng: ref HmacDrbgContext
|
2023-06-22 20:58:14 +00:00
|
|
|
key: crypto.PrivateKey
|
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
wakuDiscv5: Option[WakuDiscoveryV5]
|
2023-04-25 13:34:57 +00:00
|
|
|
dynamicBootstrapNodes: seq[RemotePeerInfo]
|
|
|
|
|
|
|
|
node: WakuNode
|
|
|
|
|
2024-02-29 08:48:14 +00:00
|
|
|
restServer: Option[WakuRestServerRef]
|
2023-04-26 17:25:18 +00:00
|
|
|
metricsServer: Option[MetricsHttpServerRef]
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
|
|
AppResult*[T] = Result[T, string]
|
|
|
|
|
|
|
|
func node*(app: App): WakuNode =
|
|
|
|
app.node
|
|
|
|
|
|
|
|
func version*(app: App): string =
|
|
|
|
app.version
|
|
|
|
|
2024-03-12 13:44:54 +00:00
|
|
|
## Retrieve dynamic bootstrap nodes (DNS discovery)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc retrieveDynamicBootstrapNodes*(
|
|
|
|
dnsDiscovery: bool, dnsDiscoveryUrl: string, dnsDiscoveryNameServers: seq[IpAddress]
|
|
|
|
): Result[seq[RemotePeerInfo], string] =
|
2024-03-12 13:44:54 +00:00
|
|
|
if dnsDiscovery and dnsDiscoveryUrl != "":
|
|
|
|
# DNS discovery
|
2024-03-15 23:08:47 +00:00
|
|
|
debug "Discovering nodes using Waku DNS discovery", url = dnsDiscoveryUrl
|
2024-03-12 13:44:54 +00:00
|
|
|
|
|
|
|
var nameServers: seq[TransportAddress]
|
|
|
|
for ip in dnsDiscoveryNameServers:
|
|
|
|
nameServers.add(initTAddress(ip, Port(53))) # Assume all servers use port 53
|
|
|
|
|
|
|
|
let dnsResolver = DnsResolver.new(nameServers)
|
|
|
|
|
|
|
|
proc resolver(domain: string): Future[string] {.async, gcsafe.} =
|
2024-03-15 23:08:47 +00:00
|
|
|
trace "resolving", domain = domain
|
2024-03-12 13:44:54 +00:00
|
|
|
let resolved = await dnsResolver.resolveTxt(domain)
|
|
|
|
return resolved[0] # Use only first answer
|
|
|
|
|
|
|
|
var wakuDnsDiscovery = WakuDnsDiscovery.init(dnsDiscoveryUrl, resolver)
|
|
|
|
if wakuDnsDiscovery.isOk():
|
2024-03-15 23:08:47 +00:00
|
|
|
return wakuDnsDiscovery.get().findPeers().mapErr(
|
|
|
|
proc(e: cstring): string =
|
|
|
|
$e
|
|
|
|
)
|
2024-03-12 13:44:54 +00:00
|
|
|
else:
|
|
|
|
warn "Failed to init Waku DNS discovery"
|
|
|
|
|
|
|
|
debug "No method for retrieving dynamic bootstrap nodes specified."
|
|
|
|
ok(newSeq[RemotePeerInfo]()) # Return an empty seq by default
|
|
|
|
|
2023-04-25 13:34:57 +00:00
|
|
|
## Initialisation
|
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
proc init*(T: type App, conf: WakuNodeConf): Result[App, string] =
|
|
|
|
var confCopy = conf
|
|
|
|
let rng = crypto.newRng()
|
|
|
|
|
|
|
|
if not confCopy.nodekey.isSome():
|
|
|
|
let keyRes = crypto.PrivateKey.random(Secp256k1, rng[])
|
2024-03-15 23:08:47 +00:00
|
|
|
if keyRes.isErr():
|
2024-03-08 22:46:42 +00:00
|
|
|
error "Failed to generate key", error = $keyRes.error
|
|
|
|
return err("Failed to generate key: " & $keyRes.error)
|
|
|
|
confCopy.nodekey = some(keyRes.get())
|
|
|
|
|
|
|
|
debug "Retrieve dynamic bootstrap nodes"
|
2024-03-15 23:08:47 +00:00
|
|
|
let dynamicBootstrapNodesRes = retrieveDynamicBootstrapNodes(
|
|
|
|
confCopy.dnsDiscovery, confCopy.dnsDiscoveryUrl, confCopy.dnsDiscoveryNameServers
|
|
|
|
)
|
2024-03-08 22:46:42 +00:00
|
|
|
if dynamicBootstrapNodesRes.isErr():
|
2024-03-15 23:08:47 +00:00
|
|
|
error "Retrieving dynamic bootstrap nodes failed",
|
|
|
|
error = dynamicBootstrapNodesRes.error
|
|
|
|
return err(
|
|
|
|
"Retrieving dynamic bootstrap nodes failed: " & dynamicBootstrapNodesRes.error
|
|
|
|
)
|
2024-03-08 22:46:42 +00:00
|
|
|
|
|
|
|
let nodeRes = setupNode(confCopy, some(rng))
|
2024-03-15 23:08:47 +00:00
|
|
|
if nodeRes.isErr():
|
|
|
|
error "Failed setting up node", error = nodeRes.error
|
2024-03-08 22:46:42 +00:00
|
|
|
return err("Failed setting up node: " & nodeRes.error)
|
|
|
|
|
|
|
|
var app = App(
|
2024-03-15 23:08:47 +00:00
|
|
|
version: git_version,
|
|
|
|
conf: confCopy,
|
|
|
|
rng: rng,
|
|
|
|
key: confCopy.nodekey.get(),
|
|
|
|
node: nodeRes.get(),
|
|
|
|
dynamicBootstrapNodes: dynamicBootstrapNodesRes.get(),
|
|
|
|
)
|
2024-03-08 22:46:42 +00:00
|
|
|
|
|
|
|
ok(app)
|
2023-04-25 13:34:57 +00:00
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
## Setup DiscoveryV5
|
|
|
|
|
2024-04-19 18:03:36 +00:00
|
|
|
proc setupDiscoveryV5(app: App): WakuDiscoveryV5 =
|
2024-03-15 23:08:47 +00:00
|
|
|
let dynamicBootstrapEnrs =
|
|
|
|
app.dynamicBootstrapNodes.filterIt(it.hasUdpPort()).mapIt(it.enr.get())
|
2023-06-27 13:50:11 +00:00
|
|
|
|
|
|
|
var discv5BootstrapEnrs: seq[enr.Record]
|
|
|
|
|
|
|
|
# parse enrURIs from the configuration and add the resulting ENRs to the discv5BootstrapEnrs seq
|
|
|
|
for enrUri in app.conf.discv5BootstrapNodes:
|
|
|
|
addBootstrapNode(enrUri, discv5BootstrapEnrs)
|
|
|
|
|
|
|
|
discv5BootstrapEnrs.add(dynamicBootstrapEnrs)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let discv5Config = DiscoveryConfig.init(
|
|
|
|
app.conf.discv5TableIpLimit, app.conf.discv5BucketIpLimit, app.conf.discv5BitsPerHop
|
|
|
|
)
|
2023-06-27 13:50:11 +00:00
|
|
|
|
|
|
|
let discv5UdpPort = Port(uint16(app.conf.discv5UdpPort) + app.conf.portsShift)
|
|
|
|
|
|
|
|
let discv5Conf = WakuDiscoveryV5Config(
|
|
|
|
discv5Config: some(discv5Config),
|
2023-06-28 12:57:10 +00:00
|
|
|
address: app.conf.listenAddress,
|
2023-06-27 13:50:11 +00:00
|
|
|
port: discv5UdpPort,
|
|
|
|
privateKey: keys.PrivateKey(app.key.skkey),
|
|
|
|
bootstrapRecords: discv5BootstrapEnrs,
|
|
|
|
autoupdateRecord: app.conf.discv5EnrAutoUpdate,
|
|
|
|
)
|
|
|
|
|
2023-11-21 20:15:39 +00:00
|
|
|
WakuDiscoveryV5.new(
|
2024-01-16 16:27:40 +00:00
|
|
|
app.rng,
|
2023-11-21 20:15:39 +00:00
|
|
|
discv5Conf,
|
2024-03-08 22:46:42 +00:00
|
|
|
some(app.node.enr),
|
2023-11-21 20:15:39 +00:00
|
|
|
some(app.node.peerManager),
|
|
|
|
app.node.topicSubscriptionQueue,
|
|
|
|
)
|
2023-04-25 13:34:57 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc getPorts(
|
|
|
|
listenAddrs: seq[MultiAddress]
|
|
|
|
): AppResult[tuple[tcpPort, websocketPort: Option[Port]]] =
|
2023-10-27 07:11:47 +00:00
|
|
|
var tcpPort, websocketPort = none(Port)
|
|
|
|
|
|
|
|
for a in listenAddrs:
|
|
|
|
if a.isWsAddress():
|
|
|
|
if websocketPort.isNone():
|
|
|
|
let wsAddress = initTAddress(a).valueOr:
|
|
|
|
return err("getPorts wsAddr error:" & $error)
|
|
|
|
websocketPort = some(wsAddress.port)
|
|
|
|
elif tcpPort.isNone():
|
|
|
|
let tcpAddress = initTAddress(a).valueOr:
|
|
|
|
return err("getPorts tcpAddr error:" & $error)
|
|
|
|
tcpPort = some(tcpAddress.port)
|
|
|
|
|
|
|
|
return ok((tcpPort: tcpPort, websocketPort: websocketPort))
|
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
proc getRunningNetConfig(app: App): AppResult[NetConfig] =
|
2023-10-27 07:11:47 +00:00
|
|
|
var conf = app.conf
|
|
|
|
let (tcpPort, websocketPort) = getPorts(app.node.switch.peerInfo.listenAddrs).valueOr:
|
|
|
|
return err("Could not retrieve ports " & error)
|
|
|
|
|
|
|
|
if tcpPort.isSome():
|
|
|
|
conf.tcpPort = tcpPort.get()
|
|
|
|
|
|
|
|
if websocketPort.isSome():
|
|
|
|
conf.websocketPort = websocketPort.get()
|
|
|
|
|
|
|
|
# Rebuild NetConfig with bound port values
|
|
|
|
let netConf = networkConfiguration(conf, clientId).valueOr:
|
|
|
|
return err("Could not update NetConfig: " & error)
|
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
return ok(netConf)
|
2023-10-27 07:11:47 +00:00
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
proc updateEnr(app: var App, netConf: NetConfig): AppResult[void] =
|
|
|
|
let record = enrConfiguration(app.conf, netConf, app.key).valueOr:
|
2023-11-21 20:15:39 +00:00
|
|
|
return err("ENR setup failed: " & error)
|
|
|
|
|
2024-01-30 12:15:23 +00:00
|
|
|
if isClusterMismatched(record, app.conf.clusterId):
|
|
|
|
return err("cluster id mismatch configured shards")
|
2023-10-27 07:11:47 +00:00
|
|
|
|
|
|
|
app.node.enr = record
|
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
proc updateApp(app: var App): AppResult[void] =
|
|
|
|
if app.conf.tcpPort == Port(0) or app.conf.websocketPort == Port(0):
|
2024-03-08 22:46:42 +00:00
|
|
|
let netConf = getRunningNetConfig(app).valueOr:
|
2023-10-27 07:11:47 +00:00
|
|
|
return err("error calling updateNetConfig: " & $error)
|
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
updateEnr(app, netConf).isOkOr:
|
2023-10-27 07:11:47 +00:00
|
|
|
return err("error calling updateEnr: " & $error)
|
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
app.node.announcedAddresses = netConf.announcedAddresses
|
2023-10-27 07:11:47 +00:00
|
|
|
|
|
|
|
printNodeNetworkInfo(app.node)
|
|
|
|
|
|
|
|
return ok()
|
2023-04-25 13:34:57 +00:00
|
|
|
|
2023-10-27 07:11:47 +00:00
|
|
|
proc startApp*(app: var App): AppResult[void] =
|
2024-03-15 23:08:47 +00:00
|
|
|
let nodeRes = catch:
|
|
|
|
(waitFor startNode(app.node, app.conf, app.dynamicBootstrapNodes))
|
2023-11-21 20:15:39 +00:00
|
|
|
if nodeRes.isErr():
|
|
|
|
return err("exception starting node: " & nodeRes.error.msg)
|
|
|
|
|
|
|
|
nodeRes.get().isOkOr:
|
|
|
|
return err("exception starting node: " & error)
|
2023-10-27 07:11:47 +00:00
|
|
|
|
|
|
|
# Update app data that is set dynamically on node start
|
|
|
|
app.updateApp().isOkOr:
|
|
|
|
return err("Error in updateApp: " & $error)
|
|
|
|
|
2024-03-08 22:46:42 +00:00
|
|
|
## Discv5
|
|
|
|
if app.conf.discv5Discovery:
|
|
|
|
app.wakuDiscV5 = some(app.setupDiscoveryV5())
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
if app.wakuDiscv5.isSome():
|
2023-08-23 13:53:17 +00:00
|
|
|
let wakuDiscv5 = app.wakuDiscv5.get()
|
2024-03-15 23:08:47 +00:00
|
|
|
let catchRes = catch:
|
|
|
|
(waitFor wakuDiscv5.start())
|
2023-11-21 20:15:39 +00:00
|
|
|
let startRes = catchRes.valueOr:
|
|
|
|
return err("failed to start waku discovery v5: " & catchRes.error.msg)
|
2023-06-27 13:50:11 +00:00
|
|
|
|
2023-11-21 20:15:39 +00:00
|
|
|
startRes.isOkOr:
|
|
|
|
return err("failed to start waku discovery v5: " & error)
|
2023-06-27 13:50:11 +00:00
|
|
|
|
2023-10-27 07:11:47 +00:00
|
|
|
return ok()
|
|
|
|
|
2023-04-25 13:34:57 +00:00
|
|
|
## Monitoring and external interfaces
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc startRestServer(
|
|
|
|
app: App, address: IpAddress, port: Port, conf: WakuNodeConf
|
|
|
|
): AppResult[WakuRestServerRef] =
|
2023-10-27 14:31:57 +00:00
|
|
|
# Used to register api endpoints that are not currently installed as keys,
|
|
|
|
# values are holding error messages to be returned to the client
|
|
|
|
var notInstalledTab: Table[string, string] = initTable[string, string]()
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let requestErrorHandler: RestRequestErrorHandler = proc(
|
|
|
|
error: RestRequestError, request: HttpRequestRef
|
|
|
|
): Future[HttpResponseRef] {.async: (raises: [CancelledError]).} =
|
2024-02-29 08:48:14 +00:00
|
|
|
try:
|
|
|
|
case error
|
|
|
|
of RestRequestError.Invalid:
|
|
|
|
return await request.respond(Http400, "Invalid request", HttpTable.init())
|
|
|
|
of RestRequestError.NotFound:
|
|
|
|
let paths = request.rawPath.split("/")
|
2024-03-15 23:08:47 +00:00
|
|
|
let rootPath =
|
|
|
|
if len(paths) > 1:
|
|
|
|
paths[1]
|
|
|
|
else:
|
|
|
|
""
|
2024-02-29 08:48:14 +00:00
|
|
|
notInstalledTab.withValue(rootPath, errMsg):
|
|
|
|
return await request.respond(Http404, errMsg[], HttpTable.init())
|
|
|
|
do:
|
2024-03-15 23:08:47 +00:00
|
|
|
return await request.respond(
|
|
|
|
Http400,
|
|
|
|
"Bad request initiated. Invalid path or method used.",
|
|
|
|
HttpTable.init(),
|
|
|
|
)
|
2024-02-29 08:48:14 +00:00
|
|
|
of RestRequestError.InvalidContentBody:
|
|
|
|
return await request.respond(Http400, "Invalid content body", HttpTable.init())
|
|
|
|
of RestRequestError.InvalidContentType:
|
|
|
|
return await request.respond(Http400, "Invalid content type", HttpTable.init())
|
|
|
|
of RestRequestError.Unexpected:
|
|
|
|
return defaultResponse()
|
|
|
|
except HttpWriteError:
|
|
|
|
error "Failed to write response to client", error = getCurrentExceptionMsg()
|
|
|
|
discard
|
2023-10-27 14:31:57 +00:00
|
|
|
|
|
|
|
return defaultResponse()
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let allowedOrigin =
|
|
|
|
if len(conf.restAllowOrigin) > 0:
|
|
|
|
some(conf.restAllowOrigin.join(","))
|
|
|
|
else:
|
|
|
|
none(string)
|
|
|
|
|
|
|
|
let server =
|
|
|
|
?newRestHttpServer(
|
|
|
|
address,
|
|
|
|
port,
|
|
|
|
allowedOrigin = allowedOrigin,
|
|
|
|
requestErrorHandler = requestErrorHandler,
|
|
|
|
)
|
2024-02-29 08:48:14 +00:00
|
|
|
|
2023-10-05 12:00:09 +00:00
|
|
|
## Admin REST API
|
2023-11-24 09:13:20 +00:00
|
|
|
if conf.restAdmin:
|
|
|
|
installAdminApiHandlers(server.router, app.node)
|
2023-10-05 12:00:09 +00:00
|
|
|
|
2023-04-26 17:25:18 +00:00
|
|
|
## Debug REST API
|
|
|
|
installDebugApiHandlers(server.router, app.node)
|
2023-04-25 13:34:57 +00:00
|
|
|
|
2023-09-08 09:19:47 +00:00
|
|
|
## Health REST API
|
|
|
|
installHealthApiHandler(server.router, app.node)
|
|
|
|
|
2023-04-26 17:25:18 +00:00
|
|
|
## Relay REST API
|
|
|
|
if conf.relay:
|
2023-11-28 12:21:41 +00:00
|
|
|
let cache = MessageCache.init(int(conf.restRelayCacheCapacity))
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
let handler = messageCacheHandler(cache)
|
|
|
|
|
|
|
|
for pubsubTopic in conf.pubsubTopics:
|
2023-11-28 12:21:41 +00:00
|
|
|
cache.pubsubSubscribe(pubsubTopic)
|
2023-09-26 11:33:52 +00:00
|
|
|
app.node.subscribe((kind: PubsubSub, topic: pubsubTopic), some(handler))
|
|
|
|
|
|
|
|
for contentTopic in conf.contentTopics:
|
2023-11-28 12:21:41 +00:00
|
|
|
cache.contentSubscribe(contentTopic)
|
|
|
|
app.node.subscribe((kind: ContentSub, topic: contentTopic), some(handler))
|
2023-09-26 11:33:52 +00:00
|
|
|
|
|
|
|
installRelayApiHandlers(server.router, app.node, cache)
|
2023-10-27 14:31:57 +00:00
|
|
|
else:
|
2024-03-15 23:08:47 +00:00
|
|
|
notInstalledTab["relay"] =
|
|
|
|
"/relay endpoints are not available. Please check your configuration: --relay"
|
2023-04-26 17:25:18 +00:00
|
|
|
|
2023-08-04 09:34:22 +00:00
|
|
|
## Filter REST API
|
2024-04-17 19:48:20 +00:00
|
|
|
if conf.filternode != "" and app.node.wakuFilterClient != nil:
|
2023-11-28 12:21:41 +00:00
|
|
|
let filterCache = MessageCache.init()
|
2023-10-27 19:43:54 +00:00
|
|
|
|
2024-01-16 16:27:40 +00:00
|
|
|
let filterDiscoHandler =
|
2023-10-27 19:43:54 +00:00
|
|
|
if app.wakuDiscv5.isSome():
|
|
|
|
some(defaultDiscoveryHandler(app.wakuDiscv5.get(), Filter))
|
2024-03-15 23:08:47 +00:00
|
|
|
else:
|
|
|
|
none(DiscoveryHandler)
|
2023-10-27 19:43:54 +00:00
|
|
|
|
|
|
|
rest_filter_api.installFilterRestApiHandlers(
|
2024-03-15 23:08:47 +00:00
|
|
|
server.router, app.node, filterCache, filterDiscoHandler
|
2023-10-27 19:43:54 +00:00
|
|
|
)
|
2023-10-27 14:31:57 +00:00
|
|
|
else:
|
2024-03-15 23:08:47 +00:00
|
|
|
notInstalledTab["filter"] =
|
|
|
|
"/filter endpoints are not available. Please check your configuration: --filternode"
|
2023-10-27 14:31:57 +00:00
|
|
|
|
2023-04-26 17:25:18 +00:00
|
|
|
## Store REST API
|
2024-01-16 16:27:40 +00:00
|
|
|
let storeDiscoHandler =
|
2023-10-27 19:43:54 +00:00
|
|
|
if app.wakuDiscv5.isSome():
|
|
|
|
some(defaultDiscoveryHandler(app.wakuDiscv5.get(), Store))
|
2024-03-15 23:08:47 +00:00
|
|
|
else:
|
|
|
|
none(DiscoveryHandler)
|
2023-10-27 19:43:54 +00:00
|
|
|
|
|
|
|
installStoreApiHandlers(server.router, app.node, storeDiscoHandler)
|
2023-04-26 17:25:18 +00:00
|
|
|
|
2023-09-22 13:36:46 +00:00
|
|
|
## Light push API
|
2024-03-15 23:08:47 +00:00
|
|
|
if conf.lightpushnode != "" and app.node.wakuLightpushClient != nil:
|
2024-01-16 16:27:40 +00:00
|
|
|
let lightDiscoHandler =
|
2023-10-27 19:43:54 +00:00
|
|
|
if app.wakuDiscv5.isSome():
|
|
|
|
some(defaultDiscoveryHandler(app.wakuDiscv5.get(), Lightpush))
|
2024-03-15 23:08:47 +00:00
|
|
|
else:
|
|
|
|
none(DiscoveryHandler)
|
2023-10-27 19:43:54 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
rest_lightpush_api.installLightPushRequestHandler(
|
|
|
|
server.router, app.node, lightDiscoHandler
|
|
|
|
)
|
2023-10-27 14:31:57 +00:00
|
|
|
else:
|
2024-03-15 23:08:47 +00:00
|
|
|
notInstalledTab["lightpush"] =
|
|
|
|
"/lightpush endpoints are not available. Please check your configuration: --lightpushnode"
|
2023-09-22 11:46:55 +00:00
|
|
|
|
2023-04-26 17:25:18 +00:00
|
|
|
server.start()
|
|
|
|
info "Starting REST HTTP server", url = "http://" & $address & ":" & $port & "/"
|
|
|
|
|
|
|
|
ok(server)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc startMetricsServer(
|
|
|
|
serverIp: IpAddress, serverPort: Port
|
|
|
|
): AppResult[MetricsHttpServerRef] =
|
|
|
|
info "Starting metrics HTTP server", serverIp = $serverIp, serverPort = $serverPort
|
2023-04-26 17:25:18 +00:00
|
|
|
|
|
|
|
let metricsServerRes = MetricsHttpServerRef.new($serverIp, serverPort)
|
|
|
|
if metricsServerRes.isErr():
|
|
|
|
return err("metrics HTTP server start failed: " & $metricsServerRes.error)
|
|
|
|
|
|
|
|
let server = metricsServerRes.value
|
|
|
|
try:
|
|
|
|
waitFor server.start()
|
|
|
|
except CatchableError:
|
|
|
|
return err("metrics HTTP server start failed: " & getCurrentExceptionMsg())
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
info "Metrics HTTP server started", serverIp = $serverIp, serverPort = $serverPort
|
2023-04-26 17:25:18 +00:00
|
|
|
ok(server)
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
|
|
proc startMetricsLogging(): AppResult[void] =
|
|
|
|
startMetricsLog()
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc setupMonitoringAndExternalInterfaces*(app: var App): AppResult[void] =
|
|
|
|
if app.conf.rest:
|
2024-03-15 23:08:47 +00:00
|
|
|
let startRestServerRes = startRestServer(
|
|
|
|
app, app.conf.restAddress, Port(app.conf.restPort + app.conf.portsShift), app.conf
|
|
|
|
)
|
2023-04-25 13:34:57 +00:00
|
|
|
if startRestServerRes.isErr():
|
2024-03-15 23:08:47 +00:00
|
|
|
error "Starting REST server failed. Continuing in current state.",
|
|
|
|
error = startRestServerRes.error
|
2023-04-25 13:34:57 +00:00
|
|
|
else:
|
|
|
|
app.restServer = some(startRestServerRes.value)
|
|
|
|
|
|
|
|
if app.conf.metricsServer:
|
2024-03-15 23:08:47 +00:00
|
|
|
let startMetricsServerRes = startMetricsServer(
|
|
|
|
app.conf.metricsServerAddress,
|
|
|
|
Port(app.conf.metricsServerPort + app.conf.portsShift),
|
|
|
|
)
|
2023-04-25 13:34:57 +00:00
|
|
|
if startMetricsServerRes.isErr():
|
2024-03-15 23:08:47 +00:00
|
|
|
error "Starting metrics server failed. Continuing in current state.",
|
|
|
|
error = startMetricsServerRes.error
|
2023-04-26 17:25:18 +00:00
|
|
|
else:
|
|
|
|
app.metricsServer = some(startMetricsServerRes.value)
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
|
|
if app.conf.metricsLogging:
|
|
|
|
let startMetricsLoggingRes = startMetricsLogging()
|
|
|
|
if startMetricsLoggingRes.isErr():
|
2024-03-15 23:08:47 +00:00
|
|
|
error "Starting metrics console logging failed. Continuing in current state.",
|
|
|
|
error = startMetricsLoggingRes.error
|
2023-04-25 13:34:57 +00:00
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# App shutdown
|
|
|
|
|
2023-12-14 06:16:39 +00:00
|
|
|
proc stop*(app: App): Future[void] {.async: (raises: [Exception]).} =
|
2023-04-25 13:34:57 +00:00
|
|
|
if app.restServer.isSome():
|
|
|
|
await app.restServer.get().stop()
|
|
|
|
|
2023-04-26 17:25:18 +00:00
|
|
|
if app.metricsServer.isSome():
|
|
|
|
await app.metricsServer.get().stop()
|
|
|
|
|
2023-06-27 13:50:11 +00:00
|
|
|
if app.wakuDiscv5.isSome():
|
|
|
|
await app.wakuDiscv5.get().stop()
|
|
|
|
|
2023-04-25 13:34:57 +00:00
|
|
|
if not app.node.isNil():
|
|
|
|
await app.node.stop()
|