2023-08-04 16:56:55 +00:00
|
|
|
import { DiskSpacePrimitives } from "../common/spaces/disk_space_primitives.ts";
|
|
|
|
import { AssetBundle } from "../plugos/asset_bundle/bundle.ts";
|
|
|
|
|
2023-08-29 19:17:29 +00:00
|
|
|
import { sleep } from "$sb/lib/async.ts";
|
2023-08-26 06:31:51 +00:00
|
|
|
import { ServerSystem } from "../server/server_system.ts";
|
|
|
|
import { AssetBundlePlugSpacePrimitives } from "../common/spaces/asset_bundle_space_primitives.ts";
|
2023-12-10 12:23:42 +00:00
|
|
|
import { determineDatabaseBackend } from "../server/db_backend.ts";
|
|
|
|
import { EndpointHook } from "../plugos/hooks/endpoint.ts";
|
2023-12-17 14:25:44 +00:00
|
|
|
import { determineShellBackend } from "../server/shell_backend.ts";
|
2024-01-13 17:07:02 +00:00
|
|
|
import { Hono } from "../server/deps.ts";
|
2023-08-11 18:37:13 +00:00
|
|
|
|
2023-08-04 16:56:55 +00:00
|
|
|
export async function runPlug(
|
|
|
|
spacePath: string,
|
2023-08-11 18:37:13 +00:00
|
|
|
functionName: string | undefined,
|
2023-08-04 16:56:55 +00:00
|
|
|
args: string[] = [],
|
|
|
|
builtinAssetBundle: AssetBundle,
|
2023-08-11 18:37:13 +00:00
|
|
|
httpServerPort = 3123,
|
|
|
|
httpHostname = "127.0.0.1",
|
2023-08-04 16:56:55 +00:00
|
|
|
) {
|
2023-08-11 18:37:13 +00:00
|
|
|
const serverController = new AbortController();
|
2024-01-13 17:07:02 +00:00
|
|
|
const app = new Hono();
|
2023-08-26 06:31:51 +00:00
|
|
|
|
2023-12-18 13:39:52 +00:00
|
|
|
const dbBackend = await determineDatabaseBackend(spacePath);
|
2023-12-10 12:23:42 +00:00
|
|
|
|
|
|
|
if (!dbBackend) {
|
|
|
|
console.error("Cannot run plugs in databaseless mode.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpointHook = new EndpointHook("/_/");
|
|
|
|
|
2023-08-26 06:31:51 +00:00
|
|
|
const serverSystem = new ServerSystem(
|
|
|
|
new AssetBundlePlugSpacePrimitives(
|
|
|
|
new DiskSpacePrimitives(spacePath),
|
|
|
|
builtinAssetBundle,
|
|
|
|
),
|
2023-12-10 12:23:42 +00:00
|
|
|
dbBackend,
|
2023-12-17 14:25:44 +00:00
|
|
|
determineShellBackend(spacePath),
|
2023-08-26 06:31:51 +00:00
|
|
|
);
|
2023-11-02 11:46:33 +00:00
|
|
|
await serverSystem.init(true);
|
2023-12-10 12:23:42 +00:00
|
|
|
app.use((context, next) => {
|
|
|
|
return endpointHook.handleRequest(serverSystem.system!, context, next);
|
|
|
|
});
|
2024-01-13 17:07:02 +00:00
|
|
|
Deno.serve({
|
2023-08-11 18:37:13 +00:00
|
|
|
hostname: httpHostname,
|
|
|
|
port: httpServerPort,
|
|
|
|
signal: serverController.signal,
|
2024-01-13 17:07:02 +00:00
|
|
|
}, app.fetch);
|
2023-08-11 18:37:13 +00:00
|
|
|
|
|
|
|
if (functionName) {
|
2024-01-20 18:16:07 +00:00
|
|
|
const result = await serverSystem.system.invokeFunction(functionName, args);
|
2023-08-26 06:31:51 +00:00
|
|
|
await serverSystem.close();
|
2023-12-10 12:23:42 +00:00
|
|
|
serverSystem.kvPrimitives.close();
|
2023-08-11 18:37:13 +00:00
|
|
|
serverController.abort();
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
console.log("Running in server mode, use Ctrl-c to stop");
|
|
|
|
while (true) {
|
|
|
|
await sleep(1000);
|
|
|
|
}
|
2023-08-04 16:56:55 +00:00
|
|
|
}
|
|
|
|
}
|