Files
smartdeno/ts/classes.scriptserver.ts

57 lines
1.4 KiB
TypeScript

import type { DenoExecution } from './classes.denoexecution.js';
import * as plugins from './plugins.js';
export interface IScriptServerOptions {
port?: number;
}
export class ScriptServer {
private server: plugins.typedserver.servertools.Server;
private port: number;
public smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash'
});
public executionMap = new plugins.lik.ObjectMap<DenoExecution>();
constructor(options: IScriptServerOptions = {}) {
this.port = options.port ?? 3210;
}
public getPort(): number {
return this.port;
}
public async start() {
this.server = new plugins.typedserver.servertools.Server({
port: this.port,
cors: true,
});
this.server.addRoute(
'/getscript/:executionId',
new plugins.typedserver.servertools.Handler('GET', async (req, res) => {
const executionId = req.params.executionId;
const denoExecution = await this.executionMap.find(async denoExecutionArg => {
return denoExecutionArg.id === executionId;
});
if (!denoExecution) {
res.statusCode = 404;
res.write('Execution not found');
res.end();
return;
}
res.write(denoExecution.script);
res.end();
})
);
await this.server.start();
}
public async stop() {
if (this.server) {
await this.server.stop();
}
this.executionMap.wipe();
}
}