mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-16 09:55:07 +00:00
32668f43f5
This reverts commit dba9820c1fa00f414f18d57f7a3ff38b67d2bb1a.
We need to revert this commit because
the waku-simulator stopped working. i.e. the nodes couldn't establish
connections among them: 054ba9e33f
Also, the following js-waku test fails due to this commit:
"same cluster, different shard: nodes connect"
* waku_lightpush/protocol.nim: minor changes to make it compile after revert
86 lines
2.3 KiB
Nim
86 lines
2.3 KiB
Nim
when (NimMajor, NimMinor) < (1, 4):
|
|
{.push raises: [Defect].}
|
|
else:
|
|
{.push raises: [].}
|
|
|
|
import
|
|
std/strformat,
|
|
std/sequtils,
|
|
stew/byteutils,
|
|
chronicles,
|
|
json_serialization,
|
|
json_serialization/std/options,
|
|
presto/route,
|
|
presto/common
|
|
|
|
import
|
|
../../../waku_core,
|
|
../../waku/node/peer_manager,
|
|
../../../waku_node,
|
|
../../waku/waku_lightpush,
|
|
../../handlers,
|
|
../serdes,
|
|
../responses,
|
|
../rest_serdes,
|
|
./types
|
|
|
|
export types
|
|
|
|
logScope:
|
|
topics = "waku node rest lightpush api"
|
|
|
|
const futTimeoutForPushRequestProcessing* = 5.seconds
|
|
|
|
const NoPeerNoDiscoError = RestApiResponse.serviceUnavailable(
|
|
"No suitable service peer & no discovery method")
|
|
|
|
const NoPeerNoneFoundError = RestApiResponse.serviceUnavailable(
|
|
"No suitable service peer & none discovered")
|
|
|
|
#### Request handlers
|
|
|
|
const ROUTE_LIGHTPUSH* = "/lightpush/v1/message"
|
|
|
|
proc installLightPushRequestHandler*(
|
|
router: var RestRouter,
|
|
node: WakuNode,
|
|
discHandler: Option[DiscoveryHandler] = none(DiscoveryHandler),
|
|
) =
|
|
|
|
router.api(MethodPost, ROUTE_LIGHTPUSH) do (contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
## Send a request to push a waku message
|
|
debug "post", ROUTE_LIGHTPUSH, contentBody
|
|
|
|
let decodedBody = decodeRequestBody[PushRequest](contentBody)
|
|
|
|
if decodedBody.isErr():
|
|
return decodedBody.error()
|
|
|
|
let req: PushRequest = decodedBody.value()
|
|
|
|
let msg = req.message.toWakuMessage().valueOr:
|
|
return RestApiResponse.badRequest("Invalid message: " & $error)
|
|
|
|
let peer = node.peerManager.selectPeer(WakuLightPushCodec).valueOr:
|
|
let handler = discHandler.valueOr:
|
|
return NoPeerNoDiscoError
|
|
|
|
let peerOp = (await handler()).valueOr:
|
|
return RestApiResponse.internalServerError("No value in peerOp: " & $error)
|
|
|
|
peerOp.valueOr:
|
|
return NoPeerNoneFoundError
|
|
|
|
let subFut = node.lightpushPublish(req.pubsubTopic, msg, peer)
|
|
|
|
if not await subFut.withTimeout(futTimeoutForPushRequestProcessing):
|
|
error "Failed to request a message push due to timeout!"
|
|
return RestApiResponse.serviceUnavailable("Push request timed out")
|
|
|
|
if subFut.value().isErr():
|
|
return RestApiResponse.serviceUnavailable(
|
|
fmt("Failed to request a message push: {subFut.value().error}")
|
|
)
|
|
|
|
return RestApiResponse.ok()
|