Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #54 add rest status end points #55

Merged
merged 3 commits into from
Mar 7, 2018
Merged
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
143 changes: 104 additions & 39 deletions src/server/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
*/

'use strict';
var express = require('express'),
Q = require('q'),
path = require('path'),
fs = require('fs'),
webgme = require('../../../index'),
StorageUtil = webgme.requirejs('common/storage/util'),
webgmeUtils = require('../../utils'),
GUID = webgme.requirejs('common/util/guid'),

CONSTANTS = webgme.requirejs('common/Constants');


/**
* Mounts the API functions to a given express app.
Expand All @@ -19,14 +30,8 @@
* @param middlewareOpts
*/
function createAPI(app, mountPath, middlewareOpts) {
var express = require('express'),
router = express.Router(),

Q = require('q'),
path = require('path'),
fs = require('fs'),
var router = express.Router(),
apiDocumentationMountPoint = '/developer/api',

logger = middlewareOpts.logger.fork('api'),
gmeAuth = middlewareOpts.gmeAuth,
metadataStorage = gmeAuth.metadataStorage,
Expand All @@ -35,16 +40,9 @@ function createAPI(app, mountPath, middlewareOpts) {
ensureAuthenticated = middlewareOpts.ensureAuthenticated,
gmeConfig = middlewareOpts.gmeConfig,
getUserId = middlewareOpts.getUserId,
webgme = require('../../../index'),
StorageUtil = webgme.requirejs('common/storage/util'),
webgmeUtils = require('../../utils'),
GUID = webgme.requirejs('common/util/guid'),

CONSTANTS = webgme.requirejs('common/Constants'),
STORAGE_CONSTANTS = CONSTANTS.STORAGE,
CORE_CONSTANTS = CONSTANTS.CORE,
versionedAPIPath = mountPath + '/v1',

latestAPIPath = mountPath,
registerEndPoint = typeof gmeConfig.authentication.allowUserRegistration === 'string' ?
require(gmeConfig.authentication.allowUserRegistration)(middlewareOpts) :
Expand Down Expand Up @@ -2178,36 +2176,12 @@ function createAPI(app, mountPath, middlewareOpts) {
});

// AddOns
router.get('/addOns', ensureAuthenticated, function (req, res) {
router.get(['/add-ons', '/addOns'], ensureAuthenticated, function (req, res) {
var result = webgmeUtils.getComponentNames(gmeConfig.addOn.basePaths);
logger.debug('/addOns', {metadata: result});
res.send(result);
});

// FIXME: This might not be the best path
// TODO: Extend on this and collect worker info in general.. (keeping this outside of the doc for now)
router.get('/addOnStatus', ensureAuthenticated, function (req, res, next) {
var userId = getUserId(req);

if (gmeConfig.addOn.enable) {
gmeAuth.getUser(userId)
.then(function (userData) {
if (gmeConfig.authentication.enable && !userData.siteAdmin) {
res.status(403);
throw new Error('site admin role is required for this operation');
}

return middlewareOpts.addOnEventPropagator.getStatus({});
})
.then(function (status) {
res.json(status);
})
.catch(next);
} else {
res.sendStatus(404);
}
});

//router.get('/addOns/:addOnId/queryParams', ensureAuthenticated, function (req, res) {});
//router.get('/addOns/:addOnId/queryParamsStructure', ensureAuthenticated, function (req, res) {});
//router.post('/addOns/:addOnId/query', ensureAuthenticated, function (req, res) {});
Expand Down Expand Up @@ -2281,6 +2255,97 @@ function createAPI(app, mountPath, middlewareOpts) {
res.send(result);
});

router.get('/status', ensureAuthenticated, function (req, res, next) {
var userId = getUserId(req),
result = {
addOns: null,
serverWorkers: null,
webSockets: null
};

gmeAuth.getUser(userId)
.then(function (userData) {
if (gmeConfig.authentication.enable && !userData.siteAdmin) {
res.status(403);
throw new Error('site admin role is required for this operation');
}

result.webSockets = middlewareOpts.webSocket.getStatus();

if (gmeConfig.addOn.enable) {
return middlewareOpts.addOnEventPropagator.getStatus({});
} else {
return null;
}
})
.then(function (addOnStatus) {
result.addOns = addOnStatus;

return Q.ninvoke(middlewareOpts.workerManager, 'getStatus');
})
.then(function (serverWorkersStatus) {
result.serverWorkers = serverWorkersStatus;

res.json(result);
})
.catch(next);
});

router.get(['/status/add-ons', '/addOnStatus'], ensureAuthenticated, function (req, res, next) {
var userId = getUserId(req);

if (gmeConfig.addOn.enable) {
gmeAuth.getUser(userId)
.then(function (userData) {
if (gmeConfig.authentication.enable && !userData.siteAdmin) {
res.status(403);
throw new Error('site admin role is required for this operation');
}

return middlewareOpts.addOnEventPropagator.getStatus({});
})
.then(function (status) {
res.json(status);
})
.catch(next);
} else {
res.sendStatus(404);
}
});

router.get('/status/server-workers', ensureAuthenticated, function (req, res, next) {
var userId = getUserId(req);

gmeAuth.getUser(userId)
.then(function (userData) {
if (gmeConfig.authentication.enable && !userData.siteAdmin) {
res.status(403);
throw new Error('site admin role is required for this operation');
}

return Q.ninvoke(middlewareOpts.workerManager, 'getStatus');
})
.then(function (status) {
res.json(status);
})
.catch(next);
});

router.get('/status/web-sockets', ensureAuthenticated, function (req, res, next) {
var userId = getUserId(req);

gmeAuth.getUser(userId)
.then(function (userData) {
if (gmeConfig.authentication.enable && !userData.siteAdmin) {
res.status(403);
throw new Error('site admin role is required for this operation');
}

res.json(middlewareOpts.webSocket.getStatus());
})
.catch(next);
});

router.use('*', function (req, res, next) {
res.status(404);
next(new Error());
Expand Down
34 changes: 34 additions & 0 deletions src/server/api/webgme-api-status-add-ons-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"guest+ANewProject": {
"initRequested": true,
"closeRequested": false,
"renewingToken": false,
"inStoppedAndStarted": 0,
"branchMonitors": {
"master": {
"startRequested": true,
"stopRequested": false,
"branchIsOpen": true,
"commitHash": "#0a1b4a3c4f1a4e4efcb4914192f227e801f421a3",
"runningAddOns": [],
"lastActivity": 1520290354966
}
}
},
"guest+LogicGates": {
"initRequested": true,
"closeRequested": false,
"renewingToken": false,
"inStoppedAndStarted": 0,
"branchMonitors": {
"master": {
"startRequested": true,
"stopRequested": false,
"branchIsOpen": true,
"commitHash": "#980a3e12e6c77efe51163989222b7f703026bcff",
"runningAddOns": [],
"lastActivity": 1520290366899
}
}
}
}
84 changes: 84 additions & 0 deletions src/server/api/webgme-api-status-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"addOns": {
"guest+ANewProject": {
"initRequested": true,
"closeRequested": false,
"renewingToken": false,
"inStoppedAndStarted": 0,
"branchMonitors": {
"master": {
"startRequested": true,
"stopRequested": false,
"branchIsOpen": true,
"commitHash": "#0a1b4a3c4f1a4e4efcb4914192f227e801f421a3",
"runningAddOns": [],
"lastActivity": 1520290354966
}
}
},
"guest+LogicGates": {
"initRequested": true,
"closeRequested": false,
"renewingToken": false,
"inStoppedAndStarted": 0,
"branchMonitors": {
"master": {
"startRequested": true,
"stopRequested": false,
"branchIsOpen": true,
"commitHash": "#980a3e12e6c77efe51163989222b7f703026bcff",
"runningAddOns": [],
"lastActivity": 1520290366899
}
}
}
},
"serverWorkers": {
"waitingRequests": [],
"workers": [
{
"pid": "3668",
"state": "working",
"request": {
"command": "exportProjectToFile",
"projectId": "guest+ANewProject",
"commitHash": "#0a1b4a3c4f1a4e4efcb4914192f227e801f421a3",
"withAssets": true,
"userId": "hans",
"socketId": "A2Vo2ji9PAxNCqaFAAAG"
}
},
{
"pid": "4692",
"state": "free"
}
]
},
"webSockets": [
{
"socketId": "A2Vo2ji9PAxNCqaFAAAG",
"userId": "hans",
"connectedSince": "Mon Mar 05 2018 16:52:33 GMT-0600 (Central Standard Time)"
},
{
"socketId": "hXhumKK5MuVSbAJHAAAH",
"userId": "olle",
"connectedSince": "Mon Mar 05 2018 16:52:34 GMT-0600 (Central Standard Time)"
},
{
"socketId": "BgKWYImd4d0wSMFYAAAI",
"userId": "guest",
"connectedSince": "Mon Mar 05 2018 16:52:45 GMT-0600 (Central Standard Time)"
},
{
"socketId": "T1PbWEZ_8nxL2hFpAAAJ",
"userId": "guest",
"connectedSince": "Mon Mar 05 2018 16:52:46 GMT-0600 (Central Standard Time)"
},
{
"socketId": "-WydaU_FsQcpCFwqAAAK",
"userId": "ragnar",
"connectedSince": "Mon Mar 05 2018 16:53:50 GMT-0600 (Central Standard Time)"
}
]
}
21 changes: 21 additions & 0 deletions src/server/api/webgme-api-status-server-workers-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"waitingRequests": [],
"workers": [
{
"pid": "3668",
"state": "working",
"request": {
"command": "exportProjectToFile",
"projectId": "guest+ANewProject",
"commitHash": "#0a1b4a3c4f1a4e4efcb4914192f227e801f421a3",
"withAssets": true,
"userId": "hans",
"socketId": "A2Vo2ji9PAxNCqaFAAAG"
}
},
{
"pid": "4692",
"state": "free"
}
]
}
27 changes: 27 additions & 0 deletions src/server/api/webgme-api-status-web-sockets-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"socketId": "A2Vo2ji9PAxNCqaFAAAG",
"userId": "hans",
"connectedSince": "Mon Mar 05 2018 16:52:33 GMT-0600 (Central Standard Time)"
},
{
"socketId": "hXhumKK5MuVSbAJHAAAH",
"userId": "olle",
"connectedSince": "Mon Mar 05 2018 16:52:34 GMT-0600 (Central Standard Time)"
},
{
"socketId": "BgKWYImd4d0wSMFYAAAI",
"userId": "guest",
"connectedSince": "Mon Mar 05 2018 16:52:45 GMT-0600 (Central Standard Time)"
},
{
"socketId": "T1PbWEZ_8nxL2hFpAAAJ",
"userId": "guest",
"connectedSince": "Mon Mar 05 2018 16:52:46 GMT-0600 (Central Standard Time)"
},
{
"socketId": "-WydaU_FsQcpCFwqAAAK",
"userId": "ragnar",
"connectedSince": "Mon Mar 05 2018 16:53:50 GMT-0600 (Central Standard Time)"
}
]
Loading