From 0b455ce627e14c85ecc90c67055dc2fc60c3667e Mon Sep 17 00:00:00 2001 From: kwar0715 Date: Tue, 25 May 2021 14:50:53 +0530 Subject: [PATCH 1/3] add active domain feature --- framework/jsondb.js | 22 +++++++++++++++++++++- framework/server.js | 9 +++++++-- index.js | 2 +- public/scripts/viewDomain.js | 15 ++++++++++++++- routes/domainRouter.js | 33 ++++++++++++++++++++++++++++++++- routes/pathRoutes.js | 5 +++-- views/domain/viewDomain.ejs | 18 ++++++++++++++++-- 7 files changed, 94 insertions(+), 10 deletions(-) mode change 100644 => 100755 framework/jsondb.js mode change 100644 => 100755 index.js mode change 100644 => 100755 routes/domainRouter.js mode change 100644 => 100755 routes/pathRoutes.js mode change 100644 => 100755 views/domain/viewDomain.ejs diff --git a/framework/jsondb.js b/framework/jsondb.js old mode 100644 new mode 100755 index ae64684..0dbb73d --- a/framework/jsondb.js +++ b/framework/jsondb.js @@ -38,6 +38,7 @@ Database.prototype.getAllDomains = async function() { domains.push({ domainId: domain.domainId, domainName: domain.domainName, + active: domain.active, pathCount: domain.paths.length }) } @@ -63,6 +64,7 @@ Database.prototype.addDomain = async function(domainName) { const record = { domainName: domainName, domainId: id, + active: true, paths: [] }; try { @@ -97,6 +99,19 @@ Database.prototype.updateDomainName = async function(domainId, domainName) { } }; +Database.prototype.updateDomainActive = async function(domainId, active) { + const domains = await db.getData("/domains") || [] + for (let i = 0; i < domains.length; i++) { + if (domains[i].domainId === domainId) { + const domain = { + ...domains[i], + active + } + db.push(`/domains[${i}]`, domain, true); + } + } +}; + Database.prototype.deleteDomain = async function(domainId) { const domains = await db.getData(`/domains`) || []; for (let i = 0; i < domains.length; i++) { @@ -116,9 +131,10 @@ Database.prototype.getPathsFromDomainId = async function(domainId) { if (domains.length > 0) { for (let i = 0; i < domains.length; i++) { if (domains[i].domainId === domainId) { - const { domainName, paths } = domains[i]; + const { domainName, paths, active } = domains[i]; return { domainName, + active, paths } } @@ -141,6 +157,7 @@ Database.prototype.getAllPaths = async function() { result.push({ domainId: domain.domainId, domainName: domain.domainName, + active: domain.active, ...domain.paths[j] }) } @@ -275,6 +292,7 @@ Database.prototype.getPath = async function(domainId, pathId) { return { domainId, domainName: domains[i].domainName, + active: domains[i].active, paths: [{...domains[i].paths[j], domainId }] } } @@ -282,6 +300,7 @@ Database.prototype.getPath = async function(domainId, pathId) { return { domainId, domainName: domains[i].domainName, + active: domains[i].active, paths: [] } } @@ -289,6 +308,7 @@ Database.prototype.getPath = async function(domainId, pathId) { } return { domainName: null, + active: true, paths: [] } }; diff --git a/framework/server.js b/framework/server.js index db5de9e..26ca462 100644 --- a/framework/server.js +++ b/framework/server.js @@ -369,7 +369,12 @@ async function filterCommands(pattern, commandType, str, url) { } } -Server.prototype.createEndpoint = async function(domainName, pathObject) { +Server.prototype.createEndpoint = async function(domainName, active, pathObject) { + + if(!active){ + Logger.info(`Domain : ${domainName} Deactivated`); + } + const path = `${domainName}${pathObject.pathUrl}`; Logger.info(`Path : ${path}`); try { @@ -518,7 +523,7 @@ Server.prototype.applyDomainList = async function() { try { const results = await Database.getAllPaths(); results.forEach(result => { - this.createEndpoint(result.domainName, { + this.createEndpoint(result.domainName, result.active, { ...result, header: JSON.parse(result.header) }); diff --git a/index.js b/index.js old mode 100644 new mode 100755 index 2de85da..2420b08 --- a/index.js +++ b/index.js @@ -322,7 +322,7 @@ systemApp.post("/upload", async function(req, res) { Server().removeRoute(`${data.domainName}${data.pathUrl}`, data.pathMethod); } - Server().createEndpoint(data.domainName, data); + Server().createEndpoint(data.domainName, true, data); logger.info( `Api created {${data.domainName}${data.pathUrl},domainId:${domainId},pathId:${pathId}}` ); diff --git a/public/scripts/viewDomain.js b/public/scripts/viewDomain.js index abe33fd..a3ca76c 100644 --- a/public/scripts/viewDomain.js +++ b/public/scripts/viewDomain.js @@ -81,4 +81,17 @@ function flushAllUserCommands() { alert("Error") }); } -} \ No newline at end of file +} + +$(function() { + $('#chk-domain-active').change(function() { + $.ajax({ + type: 'POST', + url: '/admin/domain/active', + data: { + status: $(this).prop('checked'), + id: $(this).attr('domain-id') + } + }); + }) +}) \ No newline at end of file diff --git a/routes/domainRouter.js b/routes/domainRouter.js old mode 100644 new mode 100755 index eeb04fb..44d8a2b --- a/routes/domainRouter.js +++ b/routes/domainRouter.js @@ -85,7 +85,7 @@ domainRouter.post("/edit/:domainId", async function(req, res) { header: JSON.parse(pathName.header) } Server().removeRoute(`${domainName}${pathName.pathUrl}`, pathName.pathMethod); - Server().createEndpoint(newDomainName, editableDate); + Server().createEndpoint(newDomainName, domain.active, editableDate); }); } @@ -119,6 +119,37 @@ domainRouter.get("/delete/:domainId", async function(req, res) { res.redirect(`${ADMIN_PREFIX}/domain`); }); +domainRouter.post("/active", async function(req, res) { + + const {id, status} = req.body; + const active = status === 'true' ? true :false; + const domainId = id; + + const domain = await Database.getDomainFromId(domainId); + const domainName = domain.domainName; + const pathNames = await Database.getPathNamesForDomain(domainId); + + if (pathNames.length > 0){ + if(!active){ + pathNames.forEach(function(pathName) { + Server().removeRoute(`${domainName}${pathName.pathUrl}`, pathName.pathMethod); + }); + }else{ + pathNames.forEach(function(pathName) { + const editableDate ={ + ...pathName, + header: JSON.parse(pathName.header) + } + Server().createEndpoint(domainName, true, editableDate); + }); + } + } + + // update database + await Database.updateDomainActive(domainId, active); + +}); + domainRouter.get("/restart", async function(req, res) { await Server().restart(); res.redirect(`${ADMIN_PREFIX}/domain`); diff --git a/routes/pathRoutes.js b/routes/pathRoutes.js old mode 100644 new mode 100755 index 85a3a6b..e1ee1a2 --- a/routes/pathRoutes.js +++ b/routes/pathRoutes.js @@ -98,7 +98,7 @@ pathRouter.post("/:domainId/new", async function(req, res) { }; await Database.addPath(domainId, record, pathId); - Server().createEndpoint(domain.domainName, record); + Server().createEndpoint(domain.domainName, domain.active, record); Logger.info( `Domain New Path Added {Id: ${domainId},domains:${JSON.stringify( record @@ -166,6 +166,7 @@ pathRouter.post("/:domainId/:pathId/edit", async function(req, res) { const pathId = req.params.pathId; const pathResult = await Database.getPath(domainId, pathId); const previousDomainName = pathResult.domainName; + const active = pathResult.active; const previousPathUrl = pathResult.paths[0].pathUrl; const previousPathMethod = pathResult.paths[0].pathMethod; let path = req.body.path; @@ -196,7 +197,7 @@ pathRouter.post("/:domainId/:pathId/edit", async function(req, res) { }; Server().removeRoute(`${previousDomainName}${previousPathUrl}`, previousPathMethod); - Server().createEndpoint(previousDomainName, record); + Server().createEndpoint(previousDomainName, active, record); await Database.updatePath(domainId, pathId, record); Logger.info( diff --git a/views/domain/viewDomain.ejs b/views/domain/viewDomain.ejs old mode 100644 new mode 100755 index 0b8f7af..8914abe --- a/views/domain/viewDomain.ejs +++ b/views/domain/viewDomain.ejs @@ -134,6 +134,7 @@ + @@ -142,12 +143,25 @@ <% domains.forEach(function(domain,index){ %> + +

+
Active Name Mocks Count Actions
+ + data-toggle="toggle" data-size="xs" + data-onstyle="success" data-on="Active" + data-off="Deactive" + data-offstyle="outline-danger" + > + <%= domain.domainName%>

<%= domain.pathCount%> -

-