|
@@ -1,13 +1,4 @@
|
|
|
-import fs from "fs";
|
|
|
-import path from "path";
|
|
|
-import glob from "glob";
|
|
|
-import express, {
|
|
|
- application,
|
|
|
- Request,
|
|
|
- Response,
|
|
|
- NextFunction,
|
|
|
- Errback,
|
|
|
-} from "express";
|
|
|
+import express from "express";
|
|
|
import cors from "cors";
|
|
|
import helmet from "helmet";
|
|
|
import * as http from "http";
|
|
@@ -23,9 +14,11 @@ import {
|
|
|
LoggingInterceptor,
|
|
|
ErrorInterceptor,
|
|
|
} from "./interceptors";
|
|
|
+import { getDirectories, generateCfgs } from "./utils";
|
|
|
|
|
|
const PLUGIN_DEV = process.env?.PLUGIN_DEV;
|
|
|
-const pluginDir = PLUGIN_DEV ? "../../src/*/server" : "src/plugins";
|
|
|
+const SRC_PLUGIN_DIR = "src/plugins";
|
|
|
+const DEV_PLUGIN_DIR = "../../src/*/server";
|
|
|
|
|
|
const app = express();
|
|
|
const PORT = 3000;
|
|
@@ -60,73 +53,50 @@ const pluginsRouter = express.Router();
|
|
|
// Init WebSocket server event listener
|
|
|
io.on("connection", (socket: Socket) => {
|
|
|
console.log("socket.io connected");
|
|
|
- // socket.emit("greeting-from-server", {
|
|
|
- // greeting: "Hello Client",
|
|
|
- // });
|
|
|
- // socket.on("greeting-from-client", (message) => {
|
|
|
- // console.log(message);
|
|
|
- // });
|
|
|
socket.on("COLLECTION", (message: any) => {
|
|
|
- // console.log("received COLLECTION: %s", message);
|
|
|
socket.emit("COLLECTION", { data: message });
|
|
|
});
|
|
|
socket.on("events", (message: any) => {
|
|
|
- // console.log("received events: %s", message);
|
|
|
const response = [1, 2, 3];
|
|
|
response.map((item) => {
|
|
|
setImmediate(() => socket.emit("events", { data: item }));
|
|
|
});
|
|
|
});
|
|
|
socket.on("identity", (message: any) => {
|
|
|
- // console.log("received identity: %s", message);
|
|
|
socket.emit("identity", `identity data: ${message}`);
|
|
|
});
|
|
|
pubSub.on("ws_pubsub", (msg) => {
|
|
|
const { event, data } = msg;
|
|
|
- // console.log(`pubsub: ${event}`);
|
|
|
socket.emit(event, data);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
-// Utils: read files under specified directories
|
|
|
-const getDirectories = (
|
|
|
- src: string,
|
|
|
- callback: (err: Error, res: [string]) => void
|
|
|
-) => {
|
|
|
- glob(src + "/**/*", callback);
|
|
|
-};
|
|
|
-
|
|
|
// Read plugin files and start express server
|
|
|
-getDirectories(pluginDir, (dirErr: Error, dirRes: [string]) => {
|
|
|
+// Import all plguins under "src/plugins"
|
|
|
+getDirectories(SRC_PLUGIN_DIR, async (dirErr: Error, dirRes: [string]) => {
|
|
|
const cfgs: any = [];
|
|
|
if (dirErr) {
|
|
|
console.log("Reading plugin directory Error", dirErr);
|
|
|
} else {
|
|
|
- dirRes.forEach((item: string) => {
|
|
|
- if (item.endsWith("/config.json")) {
|
|
|
- const fileData = fs.readFileSync(item);
|
|
|
- const jsonData = JSON.parse(fileData.toString());
|
|
|
- const apiPath = jsonData?.server?.api;
|
|
|
- const cfg = {
|
|
|
- path: item,
|
|
|
- dir: item.split("/config.json").shift(),
|
|
|
- dirName: item.split("/config.json").shift().split("/").pop(),
|
|
|
- api: apiPath,
|
|
|
- data: jsonData,
|
|
|
- };
|
|
|
- cfgs.push(cfg);
|
|
|
+ generateCfgs(cfgs, dirRes);
|
|
|
+ }
|
|
|
+ // If under plugin dev mode, import all plugins under "../../src/*/server"
|
|
|
+ if (PLUGIN_DEV) {
|
|
|
+ await getDirectories(
|
|
|
+ DEV_PLUGIN_DIR,
|
|
|
+ (devDirErr: Error, devDirRes: [string]) => {
|
|
|
+ if (devDirErr) {
|
|
|
+ console.log("Reading plugin directory Error", dirErr);
|
|
|
+ } else {
|
|
|
+ generateCfgs(cfgs, devDirRes, false);
|
|
|
+ }
|
|
|
}
|
|
|
- });
|
|
|
+ );
|
|
|
}
|
|
|
console.log(cfgs);
|
|
|
cfgs.forEach(async (cfg: any) => {
|
|
|
- const {
|
|
|
- dir,
|
|
|
- dirName,
|
|
|
- api: pluginPath,
|
|
|
- } = cfg;
|
|
|
+ const { api: pluginPath, componentPath } = cfg;
|
|
|
if (!pluginPath) return;
|
|
|
- const componentPath = PLUGIN_DEV ? `../${dir}/app` : `./plugins/${dirName}/app`;
|
|
|
const {
|
|
|
default: { router: pluginRouter },
|
|
|
} = await import(componentPath);
|