Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion framework/jsondb.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Database.prototype.getAllDomains = async function() {
domains.push({
domainId: domain.domainId,
domainName: domain.domainName,
active: domain.active,
pathCount: domain.paths.length
})
}
Expand All @@ -63,6 +64,7 @@ Database.prototype.addDomain = async function(domainName) {
const record = {
domainName: domainName,
domainId: id,
active: true,
paths: []
};
try {
Expand Down Expand Up @@ -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++) {
Expand All @@ -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
}
}
Expand All @@ -141,6 +157,7 @@ Database.prototype.getAllPaths = async function() {
result.push({
domainId: domain.domainId,
domainName: domain.domainName,
active: domain.active,
...domain.paths[j]
})
}
Expand Down Expand Up @@ -275,20 +292,23 @@ Database.prototype.getPath = async function(domainId, pathId) {
return {
domainId,
domainName: domains[i].domainName,
active: domains[i].active,
paths: [{...domains[i].paths[j], domainId }]
}
}
}
return {
domainId,
domainName: domains[i].domainName,
active: domains[i].active,
paths: []
}
}
}
}
return {
domainName: null,
active: true,
paths: []
}
};
Expand Down
10 changes: 8 additions & 2 deletions framework/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,13 @@ 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`);
return;
}

const path = `${domainName}${pathObject.pathUrl}`;
Logger.info(`Path : ${path}`);
try {
Expand Down Expand Up @@ -518,7 +524,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)
});
Expand Down
2 changes: 1 addition & 1 deletion index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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}}`
);
Expand Down
15 changes: 14 additions & 1 deletion public/scripts/viewDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ function flushAllUserCommands() {
alert("Error")
});
}
}
}

$(function() {
$('[type="checkbox"]').change(function(e) {
$.ajax({
type: 'POST',
url: '/admin/domain/active',
data: {
status: $(this).prop('checked'),
id: $(this).attr('domain-id')
}
});
})
})
33 changes: 32 additions & 1 deletion routes/domainRouter.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

}
Expand Down Expand Up @@ -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`);
Expand Down
8 changes: 6 additions & 2 deletions routes/pathRoutes.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -164,8 +164,10 @@ pathRouter.post("/:domainId/:pathId/edit", async function(req, res) {

const domainId = req.params.domainId;
const pathId = req.params.pathId;
const domain = await Database.getDomainFromId(domainId);
const pathResult = await Database.getPath(domainId, pathId);
const previousDomainName = pathResult.domainName;
const active = domain.active;
const previousPathUrl = pathResult.paths[0].pathUrl;
const previousPathMethod = pathResult.paths[0].pathMethod;
let path = req.body.path;
Expand Down Expand Up @@ -195,8 +197,10 @@ pathRouter.post("/:domainId/:pathId/edit", async function(req, res) {
body: req.body.body
};

console.log(record, active)

Server().removeRoute(`${previousDomainName}${previousPathUrl}`, previousPathMethod);
Server().createEndpoint(previousDomainName, record);
Server().createEndpoint(previousDomainName, active, record);
await Database.updatePath(domainId, pathId, record);

Logger.info(
Expand Down
18 changes: 16 additions & 2 deletions views/domain/viewDomain.ejs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Active</th>
<th scope="col">Name</th>
<th scope="col">Mocks Count</th>
<th scope="col">Actions</th>
Expand All @@ -142,12 +143,25 @@
<tbody>
<% domains.forEach(function(domain,index){ %>
<tr>
<td>
<input
id='chk-domain-active'
domain-id="<%=domain.domainId%>"
type="checkbox"
style="width: 20px;"
<%= (domain.active || false)? 'checked':null%>
data-toggle="toggle" data-size="xs"
data-onstyle="success" data-on="Active"
data-off="Deactive"
data-offstyle="outline-danger"
>
</td>
<th scope="row"><%= domain.domainName%></th>
<td>
<p class="domaincount">
<%= domain.pathCount%>
</p>
</td>
</p>
</td>
<td>
<button type="button" class="btn btn-primary" onClick="location.href='/admin/domain/paths/<%=domain.domainId%>'">
Manage
Expand Down