瀏覽代碼

Remote hosts UI. Added search to router.

lllllllillllllillll 1 年之前
父節點
當前提交
35e72e1b0d
共有 2 個文件被更改,包括 100 次插入36 次删除
  1. 69 21
      controllers/dashboard.js
  2. 31 15
      router/index.js

+ 69 - 21
controllers/dashboard.js

@@ -9,6 +9,19 @@ import Docker from 'dockerode';
 let [ hidden, alert, newCards, stats ] = [ '', '', '', {} ];
 let logString = '';
 
+async function hostInfo(host) {
+    let info = await ServerSettings.findOne({ where: {key: host}});
+    try {
+        if (info.value != 'off' && info.value != '') {
+            let values = info.value.split(',');
+            return { tag: values[0], ip: values[1], port: values[2] };
+        }
+    } catch {
+        console.log(`${host}: No Value Set`);
+    }
+}
+
+
 // The page
 export const Dashboard = async (req, res) => {
 
@@ -16,40 +29,48 @@ export const Dashboard = async (req, res) => {
     let role = req.session.role;
     alert = req.session.alert;
 
-    let link1 = `<a href="#" class="btn text-green">
-                    Host 1
-                </a>`
-
-    async function hostInfo(host) {
-        let info = await ServerSettings.findOne({ where: {key: host}});
-        try {
-            if (info.value != 'off' && info.value != '') {
-                let values = info.value.split(',');
-                return { tag: values[0], ip: values[1], port: values[2] };
-            }
-        } catch {
-            console.log(`${host}: No Value Set`);
-        }
-    }
-
+    let link1 = '';
     let link2 = '';
+    let link3 = '';
+    let link4 = '';
+
     let host2 = await hostInfo('host2');
     if (host2) {
-        link2 = `<button class="btn text-green" hx-post="/dashboard/host2" hx-trigger="mousedown" hx-swap="none">
+        link2 = `<button class="btn text-yellow" name="host2" hx-post="/dashboard/checkhost" hx-trigger="load delay:2s " hx-swap="outerHTML">
                     ${host2.tag}
-                </button>`
+                </button>`;
     }
     
+    let host3 = await hostInfo('host3');
+    if (host3) {
+        link3 = `<button class="btn text-yellow" name="host3" hx-post="/dashboard/checkhost" hx-trigger="load delay:2s " hx-swap="outerHTML">
+                    ${host3.tag}
+                </button>`;
+    }
+
+    let host4 = await hostInfo('host4');
+    if (host4) {
+        link4 = `<button class="btn text-yellow" name="host4" hx-post="/dashboard/checkhost" hx-trigger="load delay:2s " hx-swap="outerHTML">
+                    ${host4.tag}
+                </button>`;
+    }
+
+    if (host2 || host3 || host4) {
+        link1 = `<a href="#" class="btn text-green">
+                    Host 1
+                </a>`;
+    }
+
     
     res.render("dashboard", {
         name: name,
         avatar: name.charAt(0).toUpperCase(),
         role: role,
         alert: alert,
-        link1: '',
+        link1: link1,
         link2: link2,
-        link3: '',
-        link4: '',
+        link3: link3,
+        link4: link4,
         link5: '',
         link6: '',
         link7: '',
@@ -64,8 +85,35 @@ export const DashboardAction = async (req, res) => {
     let value = req.header('hx-trigger');
     let action = req.params.action;
     let modal = '';
+
+    console.log(`Action: ${action} Name: ${name} Value: ${value}`);
+
+    if (req.body.search) { 
+        console.log(req.body.search);
+        res.send('search');
+        return;
+    }
     
     switch (action) {
+        case 'checkhost':
+            let link = '';
+            console.log(`checking host`);
+            let host_info = await hostInfo(name);
+            try {
+                var docker2 = new Docker({ protocol: 'http', host: host_info.ip, port: host_info.port });
+                let containers = await docker2.listContainers({ all: true });
+                console.log(containers);
+                link = `<button class="btn text-green" name="host2">
+                            ${host_info.tag}
+                        </button>`;
+            } catch {
+                console.log(`Error connecting to ${name}`);
+                link = `<button class="btn text-red" name="host2">
+                            ${host_info.tag}
+                        </button>`;
+            }
+            res.send(link);
+        return;
         case 'permissions':
             let title = name.charAt(0).toUpperCase() + name.slice(1);
             let permissions_list = '';

+ 31 - 15
router/index.js

@@ -20,21 +20,6 @@ import { Syslogs } from "../controllers/syslogs.js";
 import { Install } from "../utils/install.js"
 import { Uninstall } from "../utils/uninstall.js"
 
-// Utils
-router.post("/install", adminOnly, Install);
-router.post("/uninstall", adminOnly, Uninstall);
-
-// Search (testing)
-router.post("/search", function (req, res) {
-    let path = req.header('hx-current-url');
-    // http://localhost:8000/dashboard
-    let search_from = path.split("/").pop();
-    // dashboard
-    if (search_from == "dashboard") {
-        DashboardAction(req, res);
-    }
-});
-
 // Routes
 router.get("/login", Login);
 router.post("/login", submitLogin);
@@ -54,10 +39,12 @@ router.get("/images", adminOnly, Images);
 router.post("/images/:action", adminOnly, Images);
 
 router.get("/volumes", adminOnly, Volumes);
+router.post("/volumes", adminOnly, Volumes);
 router.post("/addVolume", adminOnly, addVolume);
 router.post("/removeVolume", adminOnly, removeVolume);
 
 router.get("/networks", adminOnly, Networks);
+router.post("/networks", adminOnly, Networks);
 router.post("/removeNetwork", adminOnly, removeNetwork);
 
 router.get("/apps/:page?/:template?", adminOnly, Apps);
@@ -79,3 +66,32 @@ router.get("/account", sessionCheck, Account);
 router.get("/supporters", sessionCheck, Supporters);
 router.post("/thank", sessionCheck, Thanks);
 
+// Utils
+router.post("/install", adminOnly, Install);
+router.post("/uninstall", adminOnly, Uninstall);
+
+// Search
+router.post("/search", function (req, res) {
+    // Check which page your searching from
+    let page = (req.header('hx-current-url')).split("/").pop();
+    // Redirect to the controller
+    switch(page) {
+        case "dashboard":
+            DashboardAction(req, res);
+            break;
+        case "images":
+            Images(req, res);
+            break;
+        case "volumes":
+            Volumes(req, res);
+            break;
+        case "networks":
+            Networks(req, res);
+            break;
+        case "apps":
+            appSearch(req, res);
+            break;
+        default:
+            res.send("Invalid search");
+    }
+});