mirror of
https://github.com/morten-olsen/mini-loader.git
synced 2026-02-08 01:36:26 +01:00
feat: add http gateway (#3)
This commit is contained in:
34
packages/server/src/gateway/gateway.ts
Normal file
34
packages/server/src/gateway/gateway.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { FastifyPluginAsync } from 'fastify';
|
||||
import FastifyReplyFrom from '@fastify/reply-from';
|
||||
import { escape } from 'querystring';
|
||||
import { Runtime } from '../runtime/runtime.js';
|
||||
|
||||
type Options = {
|
||||
runtime: Runtime;
|
||||
};
|
||||
|
||||
const gateway: FastifyPluginAsync<Options> = async (fastify, { runtime }) => {
|
||||
await fastify.register(FastifyReplyFrom, {
|
||||
http: {},
|
||||
});
|
||||
|
||||
fastify.all('/gateway/*', (req, res) => {
|
||||
const [runId, ...pathSegments] = (req.params as any)['*'].split('/').filter(Boolean);
|
||||
const run = runtime.runner.getInstance(runId);
|
||||
if (!run) {
|
||||
res.statusCode = 404;
|
||||
res.send({ error: 'Run not found' });
|
||||
return;
|
||||
}
|
||||
const socketPath = run.run?.httpGatewaySocket;
|
||||
if (!socketPath) {
|
||||
res.statusCode = 404;
|
||||
res.send({ error: 'No socket path to run' });
|
||||
return;
|
||||
}
|
||||
const path = pathSegments.join('/');
|
||||
res.from(`unix+http://${escape(socketPath)}/${path}`);
|
||||
});
|
||||
};
|
||||
|
||||
export { gateway };
|
||||
Reference in New Issue
Block a user