-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,14 @@ | ||
const http = require("http"); | ||
|
||
class HttpServer { | ||
startServer(body) { | ||
return new Promise((resolve, reject) => { | ||
this.server = http.createServer(); | ||
this.server.on("request", (_, res) => res.end(body)); | ||
this.server.on("error", reject); | ||
this.server.on("listening", () => resolve(this.server.address().port)); | ||
this.server.listen(); | ||
}); | ||
} | ||
|
||
stopServer() { | ||
return new Promise((resolve, _) => { | ||
this.server.close(resolve); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = new HttpServer(); | ||
exports.createServer = async function(body) { | ||
return new Promise((resolve, reject) => { | ||
const server = http.createServer(); | ||
server.on("request", (_, res) => res.end(body)); | ||
server.on("error", reject); | ||
server.on("listening", () => resolve({ | ||
address: () => server.address(), | ||
close: () => new Promise((resolve) => server.close(resolve)), | ||
})); | ||
server.listen(); | ||
}); | ||
}; |
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,72 @@ | ||
bring cloud; | ||
bring util; | ||
bring http; | ||
|
||
struct Address { | ||
port: num; | ||
} | ||
|
||
interface IHttpServer { | ||
inflight address(): Address; | ||
inflight close(): void; | ||
} | ||
|
||
|
||
// hack: only supported in the "sim" target for now | ||
if util.env("WING_TARGET") == "sim" { | ||
class MyService { | ||
b: cloud.Bucket; | ||
body: str; | ||
|
||
pub s: cloud.Service; | ||
|
||
init(body: str) { | ||
this.b = new cloud.Bucket(); | ||
this.body = body; | ||
|
||
// it's idiomatic to just pass `this` here and implement the callbacks on the current object. | ||
this.s = new cloud.Service(inflight () => { | ||
log("starting service"); | ||
let server = MyService.createServer(this.body); | ||
let port = server.address().port; | ||
log("listening on port ${port}"); | ||
this.b.put("port", "${port}"); | ||
|
||
return () => { | ||
log("closing server..."); | ||
server.close(); | ||
}; | ||
}); | ||
} | ||
|
||
pub inflight port(): num { | ||
return num.fromStr(this.b.get("port")); | ||
} | ||
|
||
extern "./http-server.js" static inflight createServer(body: str): IHttpServer; | ||
} | ||
|
||
let foo = new MyService("bang bang!"); | ||
|
||
test "http server is started with the service" { | ||
let response = http.get("http://localhost:${foo.port()}"); | ||
log(response.body ?? ""); | ||
assert(response.body ?? "" == "bang bang!"); | ||
} | ||
|
||
test "service.stop() closes the http server" { | ||
let before = http.get("http://localhost:${foo.port()}"); | ||
assert(before.ok); | ||
|
||
foo.s.stop(); | ||
|
||
// now the http server is expected to be closed | ||
let var error = false; | ||
try { | ||
http.get("http://localhost:${foo.port()}"); | ||
} catch { | ||
error = true; | ||
} | ||
assert(error); | ||
} | ||
} |
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