mirror of
https://github.com/mmumshad/ansible-playable.git
synced 2025-03-09 23:38:54 +00:00
Feature - Integrate System API to view logs in UI
This commit is contained in:
parent
1690470269
commit
bf6c8743c5
15 changed files with 353 additions and 26 deletions
11
server/api/system/index.js
Normal file
11
server/api/system/index.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
var express = require('express');
|
||||
var controller = require('./system.controller');
|
||||
|
||||
var router = express.Router();
|
||||
|
||||
router.get('/logs/server', controller.serverLogs);
|
||||
router.get('/logs/api', controller.apiLogs);
|
||||
|
||||
module.exports = router;
|
49
server/api/system/index.spec.js
Normal file
49
server/api/system/index.spec.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
'use strict';
|
||||
|
||||
/* globals sinon, describe, expect, it */
|
||||
|
||||
var proxyquire = require('proxyquire').noPreserveCache();
|
||||
|
||||
var systemCtrlStub = {
|
||||
serverLogs: 'systemCtrl.serverLogs',
|
||||
apiLogs: 'systemCtrl.apiLogs'
|
||||
};
|
||||
|
||||
var routerStub = {
|
||||
get: sinon.spy(),
|
||||
serverLogs: sinon.spy(),
|
||||
apiLogs: sinon.spy()
|
||||
};
|
||||
|
||||
// require the index with our stubbed out modules
|
||||
var systemIndex = proxyquire('./index.js', {
|
||||
express: {
|
||||
Router() {
|
||||
return routerStub;
|
||||
}
|
||||
},
|
||||
'./system.controller': systemCtrlStub
|
||||
});
|
||||
|
||||
describe('System API Router:', function() {
|
||||
it('should return an express router instance', function() {
|
||||
expect(systemIndex).to.equal(routerStub);
|
||||
});
|
||||
|
||||
describe('GET /api/system/logs/server', function() {
|
||||
it('should route to system.controller.serverLogs', function() {
|
||||
expect(routerStub.get
|
||||
.withArgs('/logs/server', 'systemCtrl.serverLogs')
|
||||
).to.have.been.calledOnce;
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/system/logs/api', function() {
|
||||
it('should route to system.controller.apiLogs', function() {
|
||||
expect(routerStub.get
|
||||
.withArgs('/logs/api', 'systemCtrl.apiLogs')
|
||||
).to.have.been.calledOnce;
|
||||
});
|
||||
});
|
||||
|
||||
});
|
90
server/api/system/system.controller.js
Normal file
90
server/api/system/system.controller.js
Normal file
|
@ -0,0 +1,90 @@
|
|||
/**
|
||||
* Using Rails-like standard naming convention for endpoints.
|
||||
* GET /api/system -> index
|
||||
* POST /api/system -> create
|
||||
* GET /api/system/:id -> show
|
||||
* PUT /api/system/:id -> upsert
|
||||
* PATCH /api/system/:id -> patch
|
||||
* DELETE /api/system/:id -> destroy
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import jsonpatch from 'fast-json-patch';
|
||||
import config from '../../config/environment';
|
||||
|
||||
function respondWithResult(res, statusCode) {
|
||||
statusCode = statusCode || 200;
|
||||
return function(entity) {
|
||||
if(entity) {
|
||||
return res.status(statusCode).json(entity);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
function patchUpdates(patches) {
|
||||
return function(entity) {
|
||||
try {
|
||||
// eslint-disable-next-line prefer-reflect
|
||||
jsonpatch.apply(entity, patches, /*validate*/ true);
|
||||
} catch(err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
|
||||
return entity.save();
|
||||
};
|
||||
}
|
||||
|
||||
function removeEntity(res) {
|
||||
return function(entity) {
|
||||
if(entity) {
|
||||
return entity.remove()
|
||||
.then(() => {
|
||||
res.status(204).end();
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function handleEntityNotFound(res) {
|
||||
return function(entity) {
|
||||
if(!entity) {
|
||||
res.status(404).end();
|
||||
return null;
|
||||
}
|
||||
return entity;
|
||||
};
|
||||
}
|
||||
|
||||
function handleError(res, statusCode) {
|
||||
statusCode = statusCode || 500;
|
||||
return function(err) {
|
||||
res.status(statusCode).send(err);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
exports.serverLogs = function(req,res){
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
console.log("Server logs")
|
||||
|
||||
fs.readFile(config.paths.local_server_logfile, function(err, data){
|
||||
if(err)res.status(500).send(err);
|
||||
else res.send(data);
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
exports.apiLogs = function(req,res){
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
fs.readFile(config.paths.local_express_server_logfile, function(err, data){
|
||||
if(err)res.status(500).send(err);
|
||||
else res.send(data);
|
||||
})
|
||||
|
||||
};
|
35
server/api/system/system.events.js
Normal file
35
server/api/system/system.events.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
/**
|
||||
* System model events
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import {EventEmitter} from 'events';
|
||||
var SystemEvents = new EventEmitter();
|
||||
|
||||
// Set max event listeners (0 == unlimited)
|
||||
SystemEvents.setMaxListeners(0);
|
||||
|
||||
// Model events
|
||||
var events = {
|
||||
save: 'save',
|
||||
remove: 'remove'
|
||||
};
|
||||
|
||||
// Register the event emitter to the model events
|
||||
function registerEvents(System) {
|
||||
for(var e in events) {
|
||||
let event = events[e];
|
||||
System.post(e, emitEvent(event));
|
||||
}
|
||||
}
|
||||
|
||||
function emitEvent(event) {
|
||||
return function(doc) {
|
||||
SystemEvents.emit(event + ':' + doc._id, doc);
|
||||
SystemEvents.emit(event, doc);
|
||||
};
|
||||
}
|
||||
|
||||
export {registerEvents};
|
||||
export default SystemEvents;
|
57
server/api/system/system.integration.js
Normal file
57
server/api/system/system.integration.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
'use strict';
|
||||
|
||||
/* globals describe, expect, it, beforeEach, afterEach */
|
||||
|
||||
var app = require('../..');
|
||||
import request from 'supertest';
|
||||
|
||||
var newSystem;
|
||||
|
||||
describe('System API:', function() {
|
||||
describe('GET /api/system/logs/server', function() {
|
||||
var systems;
|
||||
|
||||
beforeEach(function(done) {
|
||||
request(app)
|
||||
.get('/api/system/logs/server')
|
||||
.expect(200)
|
||||
//.expect('Content-Type', /json/)
|
||||
.end((err, res) => {
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
systems = res.text;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should respond with String containing "Express server listening"', function() {
|
||||
expect(systems).to.contain('Express server listening');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('GET /api/system/logs/api', function() {
|
||||
var systems;
|
||||
|
||||
beforeEach(function(done) {
|
||||
request(app)
|
||||
.get('/api/system/logs/api')
|
||||
.expect(200)
|
||||
//.expect('Content-Type', /json/)
|
||||
.end((err, res) => {
|
||||
if(err) {
|
||||
return done(err);
|
||||
}
|
||||
console.log(JSON.stringify(res));
|
||||
systems = res.text;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should respond with String', function() {
|
||||
expect(systems).to.contain('/api/system/logs');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue