2022-03-27 07:55:29 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
import express from "express";
|
|
|
|
import yargs from "yargs";
|
2022-03-29 09:21:32 +00:00
|
|
|
import {hideBin} from "yargs/helpers";
|
|
|
|
import {DiskPlugLoader} from "../plug_loader";
|
|
|
|
import {CronHookT, NodeCronHook} from "../hooks/node_cron";
|
|
|
|
import shellSyscalls from "../syscalls/shell.node";
|
|
|
|
import {System} from "../system";
|
|
|
|
import {EndpointHook, EndpointHookT} from "../hooks/endpoint";
|
|
|
|
import {safeRun} from "../util";
|
2022-03-27 07:55:29 +00:00
|
|
|
import knex from "knex";
|
|
|
|
import {
|
2022-03-29 15:02:28 +00:00
|
|
|
ensureTable,
|
|
|
|
storeReadSyscalls,
|
|
|
|
storeWriteSyscalls,
|
2022-03-29 09:21:32 +00:00
|
|
|
} from "../syscalls/store.knex_node";
|
|
|
|
import {fetchSyscalls} from "../syscalls/fetch.node";
|
|
|
|
import {EventHook, EventHookT} from "../hooks/event";
|
|
|
|
import {eventSyscalls} from "../syscalls/event";
|
2022-03-27 07:55:29 +00:00
|
|
|
|
|
|
|
let args = yargs(hideBin(process.argv))
|
2022-03-29 09:21:32 +00:00
|
|
|
.option("port", {
|
2022-03-29 15:02:28 +00:00
|
|
|
type: "number",
|
|
|
|
default: 1337,
|
2022-03-29 09:21:32 +00:00
|
|
|
})
|
|
|
|
.parse();
|
2022-03-27 07:55:29 +00:00
|
|
|
|
|
|
|
if (!args._.length) {
|
2022-03-29 15:02:28 +00:00
|
|
|
console.error("Usage: plugos-server <path-to-plugs>");
|
2022-03-27 07:55:29 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const plugPath = args._[0] as string;
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
type ServerHook = EndpointHookT & CronHookT & EventHookT;
|
2022-03-27 07:55:29 +00:00
|
|
|
const system = new System<ServerHook>("server");
|
|
|
|
|
|
|
|
safeRun(async () => {
|
|
|
|
const db = knex({
|
|
|
|
client: "better-sqlite3",
|
|
|
|
connection: {
|
2022-03-27 09:31:12 +00:00
|
|
|
filename: "plugos.db",
|
2022-03-27 07:55:29 +00:00
|
|
|
},
|
|
|
|
useNullAsDefault: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
await ensureTable(db, "item");
|
|
|
|
|
|
|
|
let plugLoader = new DiskPlugLoader(system, plugPath);
|
|
|
|
await plugLoader.loadPlugs();
|
|
|
|
plugLoader.watcher();
|
2022-03-29 09:21:32 +00:00
|
|
|
system.addHook(new NodeCronHook());
|
|
|
|
let eventHook = new EventHook();
|
|
|
|
system.addHook(eventHook);
|
|
|
|
system.registerSyscalls("event", [], eventSyscalls(eventHook));
|
|
|
|
system.addHook(new EndpointHook(app, ""));
|
2022-03-27 07:55:29 +00:00
|
|
|
system.registerSyscalls("shell", [], shellSyscalls("."));
|
|
|
|
system.registerSyscalls("fetch", [], fetchSyscalls());
|
|
|
|
system.registerSyscalls(
|
|
|
|
"store",
|
|
|
|
[],
|
|
|
|
storeWriteSyscalls(db, "item"),
|
|
|
|
storeReadSyscalls(db, "item")
|
|
|
|
);
|
|
|
|
app.listen(args.port, () => {
|
|
|
|
console.log(`Plugbox server listening on port ${args.port}`);
|
|
|
|
});
|
|
|
|
});
|