-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce an API that allows forwarding various events to containers. Currently only implemented for `sim`. The `workload.forward()` method returns an `IForward` object with a `fromXxx()` method for each supported handler type. For example, this is how you can forward `cloud.Api` requests: ```js let work = new containers.Workload(...); let api = new cloud.Api(); api.get("/my_request", work.forward().fromApi()); ``` You can pass an optional `route` and `method` to `forward()` in order to customize the behavior: ```js work.forward(route: "/your_request", method: cloud.HttpMethod.PUT); ```
- Loading branch information
Showing
11 changed files
with
310 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
bring cloud; | ||
bring util; | ||
bring http; | ||
bring expect; | ||
bring "./workload.w" as w; | ||
|
||
let workload = new w.Workload( | ||
image: "./test/forwarders", | ||
name: "forwarders", | ||
port: 3000, | ||
public: true, | ||
); | ||
|
||
let requests = inflight (): Array<Json> => { | ||
let response = http.get("{workload.publicUrl!}/requests"); | ||
assert(response.ok); | ||
return Json.values(Json.parse(response.body)); | ||
}; | ||
|
||
let api = new cloud.Api(); | ||
api.get("/get-api", workload.forward().fromApi()); | ||
api.post("/post-api", workload.forward().fromApi()); | ||
|
||
api.get("/foof", workload.forward(route: "/foo").fromApi()); | ||
|
||
test "api forwarding" { | ||
http.get("{api.url}/get-api?hello=world"); | ||
|
||
expect.equal(requests(), [ | ||
{ method: "GET", url: "/get-api" } | ||
]); | ||
|
||
http.post("{api.url}/post-api", body: "hello, body!"); | ||
|
||
expect.equal(requests(), [ | ||
{ method: "GET", url: "/get-api" }, | ||
{ method: "POST", url: "/post-api", body: "hello, body!" } | ||
]); | ||
} | ||
|
||
let queue1 = new cloud.Queue() as "queue1"; | ||
let queue2 = new cloud.Queue() as "queue2"; | ||
queue1.setConsumer(workload.forward(route: "/queue_message", method: cloud.HttpMethod.PUT).fromQueue()); | ||
queue2.setConsumer(workload.forward().fromQueue()); | ||
|
||
test "queue forwarding" { | ||
queue1.push("message1"); | ||
util.waitUntil(() => { return requests().length == 1; }); | ||
|
||
queue1.push("message2"); | ||
util.waitUntil(() => { return requests().length == 2; }); | ||
|
||
expect.equal(requests(), [ | ||
{ method: "PUT", url: "/queue_message", body: "message1" }, | ||
{ method: "PUT", url: "/queue_message", body: "message2" }, | ||
]); | ||
|
||
queue2.push("message3"); | ||
util.waitUntil(() => { return requests().length == 3; }); | ||
|
||
expect.equal(requests(), [ | ||
{ method: "PUT", url: "/queue_message", body: "message1" }, | ||
{ method: "PUT", url: "/queue_message", body: "message2" }, | ||
{ method: "POST", url: "/", body: "message3" }, | ||
]); | ||
} | ||
|
||
let topic = new cloud.Topic(); | ||
topic.onMessage(workload.forward(route: "/my_topic").fromTopic()); | ||
|
||
test "subscribe to topic" { | ||
topic.publish("message from topic!"); | ||
util.waitUntil(() => { return requests().length == 1; }); | ||
|
||
expect.equal(requests(), [ | ||
{ method: "POST", url: "/my_topic", body: "message from topic!" }, | ||
]); | ||
} | ||
|
||
let bucket = new cloud.Bucket(); | ||
bucket.onCreate(workload.forward(route: "/object-created").fromBucketEvent()); | ||
|
||
test "forward bucket events" { | ||
bucket.put("object1", "content1"); | ||
util.waitUntil(() => { return requests().length == 1; }); | ||
|
||
expect.equal(requests(), [ | ||
{ method: "POST", url: "/object-created", body: Json.stringify({"key":"object1","type":"create"}), }, | ||
]); | ||
} | ||
|
||
let schedule = new cloud.Schedule(rate: 1m); | ||
schedule.onTick(workload.forward(route: "/tick", method: cloud.HttpMethod.GET).fromSchedule()); | ||
|
||
test "forward schedule events" { | ||
util.waitUntil(() => { return requests().length >= 1; }); | ||
|
||
expect.equal(requests()[0], { method: "GET", url: "/tick" }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FROM node:20.8.0-alpine | ||
EXPOSE 3000 | ||
ADD index.js /app/index.js | ||
ENTRYPOINT [ "/app/index.js" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env node | ||
const http = require('http'); | ||
|
||
const requests = []; | ||
|
||
process.on('SIGTERM', () => { | ||
console.info("Interrupted") | ||
process.exit(0) | ||
}); | ||
|
||
const server = http.createServer((req, res) => { | ||
|
||
if (req.url === '/requests') { | ||
res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
return res.end(JSON.stringify(requests)); | ||
} | ||
|
||
console.log(`request received: ${req.method} ${req.url}`); | ||
|
||
const body = []; | ||
req.on("data", (data) => { | ||
body.push(data); | ||
}); | ||
|
||
req.on("end", () => { | ||
let s = Buffer.concat(body).toString(); | ||
if (s.length === 0) { | ||
s = undefined; | ||
} | ||
|
||
requests.push({ | ||
method: req.method, | ||
url: req.url, | ||
body: s, | ||
}); | ||
|
||
res.end('OK'); | ||
}); | ||
}); | ||
|
||
console.log('listening on port 3000'); | ||
server.listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.