parent
85205593e4
commit
817cc92dd3
@ -1,2 +1,8 @@
|
|||||||
node_modules
|
node_modules
|
||||||
bun.lockb
|
bun.lockb
|
||||||
|
pnpm-lock.yaml
|
||||||
|
|
||||||
|
.idea
|
||||||
|
.vscode
|
||||||
|
*.js
|
||||||
|
*.tsbuildinfo
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
# labyrinth
|
||||||
|
|
||||||
|
The HTTP backend for the Sandkasten website.
|
@ -1,21 +1,56 @@
|
|||||||
import { Client, TcpClient } from 'msgpack-rpc-node';
|
import http from 'http';
|
||||||
|
import { nanoid } from 'nanoid';
|
||||||
|
import { Pull, Push } from 'zeromq';
|
||||||
|
|
||||||
const RPC_PORT = 9000;
|
const host = 'localhost';
|
||||||
const client = new Client(TcpClient, RPC_PORT);
|
const port = 3000;
|
||||||
await client.connect();
|
|
||||||
|
|
||||||
const server = Bun.serve({
|
const sender = new Push();
|
||||||
port: 3000,
|
await sender.bind(`tcp://127.0.0.1:5567`);
|
||||||
fetch(request) {
|
const receiver = new Pull();
|
||||||
const url = new URL(request.url);
|
await receiver.bind(`tcp://127.0.0.1:5568`);
|
||||||
switch (url.pathname) {
|
|
||||||
|
const generateId = () => nanoid(32);
|
||||||
|
|
||||||
|
const clients: Record<string, http.ServerResponse> = {};
|
||||||
|
|
||||||
|
const server = http.createServer((req, res) => {
|
||||||
|
switch (req.url) {
|
||||||
case '/run':
|
case '/run':
|
||||||
client.call('Add', 'echo $(( 1 + 2 ))', 'msh').then(console.log);
|
const jobId = generateId();
|
||||||
return new Response('Your code is running...');
|
res.writeHead(200, {
|
||||||
|
'Content-Type': 'text/event-stream',
|
||||||
|
Connection: 'keep-alive',
|
||||||
|
'Cache-Control': 'no-cache',
|
||||||
|
});
|
||||||
|
sender.send(`${jobId}echo a`).then(() => {
|
||||||
|
res.write('event: connected\n');
|
||||||
|
res.write(`data: ${jobId}\n`);
|
||||||
|
res.write('id: 0\n');
|
||||||
|
});
|
||||||
|
req.on('close', () => {
|
||||||
|
res.end('OK');
|
||||||
|
delete clients[jobId];
|
||||||
|
});
|
||||||
|
clients[jobId] = res;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return new Response('404!', { status: 404 });
|
res.writeHead(404);
|
||||||
|
res.end('404!');
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
|
server.listen(port, () => {
|
||||||
|
console.log(`Server is running on http://${host}:${port}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(`Listening on http://localhost:${server.port}`);
|
for await (const [buff] of receiver) {
|
||||||
|
const jobId = buff.slice(0, 32).toString();
|
||||||
|
console.log(`Received ${jobId}`);
|
||||||
|
const res = clients[jobId];
|
||||||
|
if (!res) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
res.write('event: message\n');
|
||||||
|
res.write(`data: ${encodeURIComponent(buff.slice(32).toString())}\n`);
|
||||||
|
res.write('id: 0\n');
|
||||||
|
}
|
||||||
|
Loading…
Reference in new issue