diff --git a/server.ts b/server.ts
index d8fa26a..30c3f4a 100644
--- a/server.ts
+++ b/server.ts
@@ -38,10 +38,7 @@ export function app(): express.Express {
server.get('/log/:id', (req, res) => {
res.status(200).json(getLogs(req.params['id']));
});
- function getLogs(id){
- const file = readFileSync(properties.logFilesPath + 'actions.log' + (id?"."+id:""), 'utf-8');
- return JSON.parse("[" + file.substring(0,file.length - 2) + "]");
- }
+
server.get('/logFiles', (req, res) => {
fs.readdir(properties.logFilesPath, (err, files) => {
res.status(200).json(files);
@@ -49,20 +46,29 @@ export function app(): express.Express {
});
-
server.get('/prettyLog', (req, res) => {
- const file = readFileSync('/var/log/irish-log/foo.txt', 'utf-8');
- let logs = JSON.parse("[" + file.substring(0,file.length - 2) + "]");
- let prettyLog = "";
- for(let i = logs.length -1; i>=0; i--){
- prettyLog += "On " + (logs[i].date?logs[i].date:"") + " " /*+ logs[i].action + " "*/ + logs[i].message+ "
";
- }
- res.status(200).send( prettyLog);
+ res.status(200).send( getPrettyLogs(null));
+ });
+ server.get('/prettyLog/:id', (req, res) => {
+ res.status(200).send( getPrettyLogs(req.params['id']));
});
server.post('/logAction', jsonParser,(req, res) => {
let log = req.body;
log.date = new Date();
- fs.appendFile('/var/log/irish-log/foo.txt', JSON.stringify(log)+ ",\n", 'utf8', function(err) {
+ fs.appendFile(properties.logFilesPath + 'actions.log', JSON.stringify(log)+ ",\n", 'utf8', function(err) {
+ if (err) {
+ return console.error(err);
+ }
+ });
+ res.status(200).send({
+ code: 200,
+ message: 'action received!'
+ });
+ });
+
+ server.get('/testLogAction', jsonParser,(req, res) => {
+ let log = {message:"log", date: new Date()};
+ fs.appendFile(properties.logFilesPath + 'actions.log', JSON.stringify(log)+ ",\n", 'utf8', function(err) {
if (err) {
return console.error(err);
}
@@ -108,7 +114,43 @@ function run(): void {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
+function getLogs(id){
+ const file = readFileSync(properties.logFilesPath + 'actions.log' + (id?"."+id:""), 'utf-8');
+ return JSON.parse("[" + file.substring(0,file.length - 2) + "]");
+}
+function getPrettyLogs(id){
+ const file = readFileSync(properties.logFilesPath + 'actions.log'+ (id?"."+id:""), 'utf-8');
+ let logs = JSON.parse("[" + file.substring(0,file.length - 2) + "]");
+ let prettyLog = "";
+ for(let i = logs.length -1; i>=0; i--) {
+ prettyLog += "On " + (logs[i].date ? formatDateAndTime(logs[i].date) : "") + " " /*+ logs[i].action + " "*/ + logs[i].message + "
";
+ }
+ return prettyLog + getPrevNextLog(id?id:0);
+}
+function formatDateAndTime(dateStr){
+ let date = new Date(dateStr);
+ const formatter = new Intl.DateTimeFormat('en-GB', {
+ timeStyle: 'short',
+ timeZone: 'GMT',
+ });
+ return date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear() + " "
+ + formatter.format(date) + "";
+
+}
+
+function getPrevNextLog(id= 0){
+ const files = fs.readdirSync(properties.logFilesPath);
+ let id_next:number = +id+1;
+ let id_prev:number = id-1;
+ let prev = (id>1 && files.indexOf("actions.log."+id_prev )!=-1)? 'Prev':"";
+ prev = (id ==1 && files.indexOf("actions.log")!=-1)? 'Prev':prev;
+ let next = (id>=0 && files.indexOf("actions.log."+ id_next )!=-1)? ('Next'):"";
+ return "
" + prev + next;
+
+}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.