Browse Source

refine code

czhen 3 years ago
parent
commit
4078412ec2

+ 3 - 1
express/package.json

@@ -6,6 +6,7 @@
   "dependencies": {
     "@zilliz/milvus2-sdk-node": "^1.0.18",
     "cors": "^2.8.5",
+    "cross-env": "^7.0.3",
     "express": "^4.17.1",
     "glob": "^7.2.0",
     "helmet": "^4.6.0",
@@ -30,6 +31,7 @@
     "prestart": "yarn build",
     "start": "node .",
     "test": "echo \"Error: no test specified\" && exit 1",
-    "clean": "rimraf dist"
+    "clean": "rimraf dist",
+    "start:plugin": "yarn build && cross-env PLUGIN_DEV=1 node dist/app.js"
   }
 }

+ 15 - 11
express/src/app.ts

@@ -25,6 +25,7 @@ import {
 } from "./interceptors";
 
 const PLUGIN_DEV = process.env?.PLUGIN_DEV;
+const pluginDir = PLUGIN_DEV ? "../../src/*/server" : "src/plugins";
 
 const app = express();
 const PORT = 3000;
@@ -54,6 +55,7 @@ app.use(TransformResInterceptor);
 app.use(LoggingInterceptor);
 
 const router = express.Router();
+const pluginsRouter = express.Router();
 
 // Init WebSocket server event listener
 io.on("connection", (socket: Socket) => {
@@ -95,40 +97,40 @@ const getDirectories = (
 };
 
 // Read plugin files and start express server
-getDirectories("../../src", (dirErr: Error, dirRes: [string]) => {
+getDirectories(pluginDir, (dirErr: Error, dirRes: [string]) => {
   const cfgs: any = [];
   if (dirErr) {
     console.log("Reading plugin directory Error", dirErr);
   } else {
     dirRes.forEach((item: string) => {
-      console.log(item);
       if (item.endsWith("/config.json")) {
         const fileData = fs.readFileSync(item);
-        const json = JSON.parse(fileData.toString());
+        const jsonData = JSON.parse(fileData.toString());
+        const apiPath = jsonData?.server?.api;
         const cfg = {
           path: item,
           dir: item.split("/config.json").shift(),
-          data: json,
+          dirName: item.split("/config.json").shift().split("/").pop(),
+          api: apiPath,
+          data: jsonData,
         };
-        // console.log(cfg);
         cfgs.push(cfg);
       }
     });
   }
   console.log(cfgs);
   cfgs.forEach(async (cfg: any) => {
-    // const pluginPath = cfg.data.api;
     const {
       dir,
-      data: { api: pluginPath },
+      dirName,
+      api: pluginPath,
     } = cfg;
     if (!pluginPath) return;
+    const componentPath = PLUGIN_DEV ? `../${dir}/app` : `./plugins/${dirName}/app`;
     const {
       default: { router: pluginRouter },
-    } = await import(`../${dir}/server/app`);
-    console.log(pluginPath);
-    console.log(pluginRouter);
-    router.use(`/${pluginPath}`, pluginRouter);
+    } = await import(componentPath);
+    pluginsRouter.use(`/${pluginPath}`, pluginRouter);
   });
 
   router.use("/milvus", connectRouter);
@@ -143,6 +145,8 @@ getDirectories("../../src", (dirErr: Error, dirRes: [string]) => {
   });
 
   app.use("/api/v1", router);
+  app.use("/api/plugins", pluginsRouter);
+
   // ErrorInterceptor
   app.use(ErrorInterceptor);
   // start server

+ 9 - 0
express/src/plugins/getServerTime/app.ts

@@ -0,0 +1,9 @@
+import express from "express";
+
+const router = express.Router();
+
+router.get("/", (req, res, next) => {
+  return res.json({ timestamp: new Date().getTime()});
+});
+
+export default { router };

+ 7 - 0
express/src/plugins/getServerTime/config.json

@@ -0,0 +1,7 @@
+{
+  "name": "get-server-time",
+  "version": "0.1.0",
+  "server": {
+    "api": "getServerTime"
+  }
+}

+ 7 - 0
express/src/plugins/getServerTime/package.json

@@ -0,0 +1,7 @@
+{
+  "name": "get-server-time",
+  "devDependencies": {},
+  "dependencies": {
+    "express": "^4.17.1"
+  }
+}

+ 45 - 0
express/yarn.lock

@@ -558,6 +558,22 @@ create-require@^1.1.0:
   resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
   integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
 
+cross-env@^7.0.3:
+  version "7.0.3"
+  resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
+  integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==
+  dependencies:
+    cross-spawn "^7.0.1"
+
+cross-spawn@^7.0.1:
+  version "7.0.3"
+  resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
+  integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
+  dependencies:
+    path-key "^3.1.0"
+    shebang-command "^2.0.0"
+    which "^2.0.1"
+
 debug@2.6.9:
   version "2.6.9"
   resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
@@ -846,6 +862,11 @@ is-fullwidth-code-point@^3.0.0:
   resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz#f116f8064fe90b3f7844a38997c0b75051269f1d"
   integrity sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==
 
+isexe@^2.0.0:
+  version "2.0.0"
+  resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
+  integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=
+
 jju@~1.4.0:
   version "1.4.0"
   resolved "https://registry.yarnpkg.com/jju/-/jju-1.4.0.tgz#a3abe2718af241a2b2904f84a625970f389ae32a"
@@ -1040,6 +1061,11 @@ path-is-absolute@^1.0.0:
   resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
   integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=
 
+path-key@^3.1.0:
+  version "3.1.1"
+  resolved "https://registry.yarnpkg.com/path-key/-/path-key-3.1.1.tgz#581f6ade658cbba65a0d3380de7753295054f375"
+  integrity sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==
+
 path-parse@^1.0.6:
   version "1.0.7"
   resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
@@ -1193,6 +1219,18 @@ setprototypeof@1.1.1:
   resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.1.tgz#7e95acb24aa92f5885e0abef5ba131330d4ae683"
   integrity sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==
 
+shebang-command@^2.0.0:
+  version "2.0.0"
+  resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea"
+  integrity sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==
+  dependencies:
+    shebang-regex "^3.0.0"
+
+shebang-regex@^3.0.0:
+  version "3.0.0"
+  resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
+  integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==
+
 socket.io-adapter@~2.3.2:
   version "2.3.2"
   resolved "https://registry.yarnpkg.com/socket.io-adapter/-/socket.io-adapter-2.3.2.tgz#039cd7c71a52abad984a6d57da2c0b7ecdd3c289"
@@ -1371,6 +1409,13 @@ vary@^1, vary@~1.1.2:
   resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
   integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=
 
+which@^2.0.1:
+  version "2.0.2"
+  resolved "https://registry.yarnpkg.com/which/-/which-2.0.2.tgz#7c6a8dd0a636a0327e10b59c9286eee93f3f51b1"
+  integrity sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==
+  dependencies:
+    isexe "^2.0.0"
+
 wrap-ansi@^7.0.0:
   version "7.0.0"
   resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"