2021-05-10 14:31:13 +00:00
|
|
|
cqueues = require "cqueues"
|
2021-05-10 13:23:30 +00:00
|
|
|
url = require "net.url"
|
|
|
|
|
|
|
|
class Server
|
2021-05-10 14:31:13 +00:00
|
|
|
new: (hdlr) =>
|
|
|
|
@cq = cqueues.new!
|
2021-05-10 13:23:30 +00:00
|
|
|
@hdlr = hdlr
|
|
|
|
|
2021-05-10 14:31:13 +00:00
|
|
|
attach: (cq) =>
|
|
|
|
cq\wrap ->
|
|
|
|
@cq\loop!
|
|
|
|
|
2021-05-10 13:23:30 +00:00
|
|
|
listen: (srv) =>
|
|
|
|
@cq\wrap ->
|
|
|
|
for conn in srv\clients!
|
|
|
|
@cq\wrap ->
|
|
|
|
with conn
|
|
|
|
_, ip = \peername!
|
|
|
|
request = \read "*l"
|
2021-05-10 14:31:13 +00:00
|
|
|
\shutdown "r"
|
2021-05-10 13:23:30 +00:00
|
|
|
u = url.parse request
|
|
|
|
ctx =
|
|
|
|
:ip
|
|
|
|
host: u.host
|
|
|
|
path: u.path
|
|
|
|
query: u.query
|
2021-05-10 14:13:42 +00:00
|
|
|
@hdlr\serve ctx, conn
|
2021-05-10 13:23:30 +00:00
|
|
|
\close!
|
|
|
|
|
2021-05-10 14:13:42 +00:00
|
|
|
{
|
|
|
|
:Server
|
|
|
|
}
|