2022-03-27 07:55:29 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
import express from "express";
|
|
|
|
import http from "http";
|
2022-03-27 07:55:29 +00:00
|
|
|
import {Server} from "socket.io";
|
|
|
|
import {SocketServer} from "./api_server";
|
2022-03-20 08:56:28 +00:00
|
|
|
import yargs from "yargs";
|
2022-03-27 07:55:29 +00:00
|
|
|
import {hideBin} from "yargs/helpers";
|
|
|
|
import {SilverBulletHooks} from "../common/manifest";
|
|
|
|
import {ExpressServer} from "./express_server";
|
|
|
|
import {DiskPlugLoader} from "../plugbox/plug_loader";
|
2022-03-23 14:41:12 +00:00
|
|
|
import { NodeCronFeature } from "../plugbox/feature/node_cron";
|
2022-03-25 11:03:06 +00:00
|
|
|
import shellSyscalls from "../plugbox/syscall/shell.node";
|
2022-03-23 14:41:12 +00:00
|
|
|
import { System } from "../plugbox/system";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
let args = yargs(hideBin(process.argv))
|
|
|
|
.option("port", {
|
|
|
|
type: "number",
|
|
|
|
default: 3000,
|
|
|
|
})
|
|
|
|
.parse();
|
|
|
|
|
2022-03-27 07:55:29 +00:00
|
|
|
if (!args._.length) {
|
|
|
|
console.error("Usage: silverbullet <path-to-pages>");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:21:34 +00:00
|
|
|
const pagesPath = args._[0] as string;
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
const app = express();
|
|
|
|
const server = http.createServer(app);
|
2022-03-23 14:41:12 +00:00
|
|
|
const system = new System<SilverBulletHooks>("server");
|
2022-03-21 14:21:34 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
const io = new Server(server, {
|
|
|
|
cors: {
|
|
|
|
methods: "GET,HEAD,PUT,OPTIONS,POST,DELETE",
|
|
|
|
preflightContinue: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const port = args.port;
|
|
|
|
const distDir = `${__dirname}/../webapp`;
|
|
|
|
|
|
|
|
app.use("/", express.static(distDir));
|
|
|
|
|
2022-03-21 14:21:34 +00:00
|
|
|
let socketServer = new SocketServer(pagesPath, io, system);
|
|
|
|
socketServer.init().catch((e) => {
|
2022-03-27 07:55:29 +00:00
|
|
|
console.error(e);
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
2022-03-21 14:21:34 +00:00
|
|
|
|
|
|
|
const expressServer = new ExpressServer(app, pagesPath, distDir, system);
|
|
|
|
expressServer
|
2022-03-27 07:55:29 +00:00
|
|
|
.init()
|
|
|
|
.then(async () => {
|
|
|
|
let plugLoader = new DiskPlugLoader(
|
|
|
|
system,
|
|
|
|
`${__dirname}/../../plugs/dist`
|
|
|
|
);
|
|
|
|
await plugLoader.loadPlugs();
|
|
|
|
plugLoader.watcher();
|
|
|
|
system.registerSyscalls("shell", ["shell"], shellSyscalls(pagesPath));
|
|
|
|
system.addFeature(new NodeCronFeature());
|
|
|
|
server.listen(port, () => {
|
|
|
|
console.log(`Server listening on port ${port}`);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
2022-03-21 14:21:34 +00:00
|
|
|
});
|