Skip to content

Commit

Permalink
Merge branch 'restful-api-lectures' into master.
Browse files Browse the repository at this point in the history
This commit is actually for the first draft of the homework assignment pirple#2 but I forgot to create a separate branch for it, so I'm committing to the last branch I was working on.
  • Loading branch information
Gregor Okorn authored and Gregor Okorn committed May 10, 2020
2 parents 65003fd + 4460d2a commit b5d9f35
Show file tree
Hide file tree
Showing 22 changed files with 820 additions and 7 deletions.
8 changes: 4 additions & 4 deletions app/lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const utils = require('./utils')

let lib = {};

lib.baseDir = path.join(__dirname,'../.data/');
lib.baseDir = path.join(__dirname,'/../.data/');

// write file
lib.create = function(dir, fileName, data, callback) {
Expand Down Expand Up @@ -40,8 +40,8 @@ lib.create = function(dir, fileName, data, callback) {
lib.read = function(dir, fileName, callback) {
// open file
fs.readFile(lib.baseDir+dir+"/"+fileName+".json", 'utf8', function(err, data) {
console.log('_data.read err:',err);
console.log('_data.read data:',data);
//console.log('_data.read err:',err);
//console.log('_data.read data:',data);
if(!err && data) {
callback(false, utils.parseJsonToObj(data));
} else {
Expand All @@ -53,7 +53,7 @@ lib.read = function(dir, fileName, callback) {

lib.update = function(dir, fileName, data, callback) {
// try to open the file
console.log("lib.update data:",data);
//console.log("lib.update data:",data);
fs.open(lib.baseDir+dir+"/"+fileName+".json",'r+',function(err, fileDescriptor) {
if(!err && fileDescriptor) {
let stringifiedData = JSON.stringify(data);
Expand Down
142 changes: 142 additions & 0 deletions app/lib/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Library for storing and rotating log files
*/

// dependencies
let fs = require('fs');
let path = require('path');
let zlib = require('zlib');

// container for the module
let lib = {};

lib.baseDir = path.join(__dirname, '/../.logs/');

// append a string to a file. Create file if it doesn't exist yet.
lib.append = function(fileName, dataString, callback) {
// open file for appending
fs.open(lib.baseDir+fileName+'.log','a', function(err, fileDescriptor) {
if(!err && fileDescriptor) {
// append to the file and close it
fs.appendFile(fileDescriptor, dataString+'\n', function(err) {
if(!err) {
fs.close(fileDescriptor, function(err){
if(!err) {
callback(false);
} else {
callback('Error closing file that was being appended',err);
}
});
} else {
callback(500,{'Error': 'Failed to write data to log file,'+fileName});
}
});
} else {
callback('Could not open file,'+fileName+', to append to.');
}
});

};


lib.list = function(includeCompressedLogs, callback) {
fs.readdir(lib.baseDir, function(err, data) {
console.log('logs.list data',data);
if(!err && data) {
var trimmedFileNames = [];
data.forEach(function(fileName) {
if(fileName.indexOf('.log') > -1) {
trimmedFileNames.push(fileName.replace('.log',''));
}
// Add on the .gz files
if(fileName.indexOf('.gz.b64') > -1 && includeCompressedLogs) {
trimmedFileNames.push(fileName.replace('.gz.g64',''));
}
callback(false, trimmedFileNames);
});
} else {
callback(err, data);
}
});
};

// compress a .log file into a .gz.b64 file in same folder
lib.compress = function(logId, newFileId, callback) {
let sourceFile = logId+'.log';
let destFile = newFileId+'.gz.b64';

// read the source file
fs.readFile(lib.baseDir+sourceFile, 'utf8', function(err, inputString) {
if(!err && inputString) {
// compress the data
zlib.gzip(inputString, function(err, buffer) {
if(!err && buffer) {
// send to file for writing
fs.open(lib.baseDir+destFile, 'wx', function(err, fileDescriptor) {
if(!err && fileDescriptor) {
fs.writeFile(fileDescriptor, buffer.toString('base64'), function(err) {
if(!err) {
fs.close(fileDescriptor, function(err) {
if(!err) {
callback(false);
} else {
callback(err);
}
});
} else {
callback(err);
}
});
} else {
callback(err);
}
});
} else {
callback(err);
}
});
} else {
callback(err);
}
});

};


// decompress contents of a .gz.b64 file into a string variable
lib.decompress = function(fileId, callback) {
let fileName = fileId+'.gz.b64';
fs.readFile(lib.baseDir+filename, 'utf8', function(err, str) {
if(!err && str) {
// decompress the data
let inputBuffer = Buffer.from(str, 'base64');
zlib.unzip(inputBuffer, function(err, outputBuffer) {
if(!err && outputBuffer) {
let str = outputBuffer.toString();
callback(false, str);
} else {
callback(err);
}
});
} else {
callback(err);
}
});
};


// truncate a log file
lib.truncate = function(logId, callback) {
fs.truncate(lib.baseDir+logId+'.log', 0, function(err) {
if(!err) {
callback(false);
} else {
callback(err);
}
});

};



module.exports = lib;
75 changes: 72 additions & 3 deletions app/lib/workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ const fs = require('fs');
const utils = require('./utils')
const path = require('path');
const _data = require('./data');
const _logs = require('./logs');

let workers = {};



// look up all checks, get their data, send to a validator
workers.gatherAllChecks = function() {
// get all the checks
Expand Down Expand Up @@ -147,11 +146,14 @@ workers.processCheckOutcome = function(origCheckData, checkOutcome) {

// decide if an alert is warranted? is current state different than origCheck's state?
let alertWarranted = origCheckData.lastChecked && origCheckData.state !== state ? true : false;
let timeOfCheck = Date.now();

workers.log(origCheckData, checkOutcome, state, alertWarranted, timeOfCheck);

// update the check state data
let newCheckData = origCheckData;
newCheckData.state = state;
newCheckData.lastChecked = Date.now();
newCheckData.lastChecked = timeOfCheck;

// save the data
_data.update('checks', newCheckData.id, newCheckData, function(err, ){
Expand Down Expand Up @@ -185,16 +187,83 @@ workers.loop = function() {
setInterval(workers.gatherAllChecks,1000 * 60);
};

// rotate (aka compress) the log files
workers.rotateLogs = function() {
console.log('workers.rotateLogs start');
// list all the non-compressed logs in the .logs folder
_logs.list(false, function(err, logs) {
console.log('workers.rotateLogs logs',logs);
if(!err && logs && logs.length) {
logs.forEach(function(logName) {
// compress data to a different file
let logId = logName.replace('.log', ''); // strip extension
let newFileId = logName+'-'+Date.now();
_logs.compress(logId, newFileId, function(err) {
if(!err) {
// truncate the log
_logs.truncate(logId, function(err) {
if(!err) {
console.log('Success truncating log file');
} else {
console.log('Error truncating the log file',err);
}
});
} else {
console.log('Error: compressing log file: ',err);
}
});
});
} else {
console.log('Error: could not find any logs to rotate.')
}
});
};

workers.logRotationLoop = function() {
setInterval(workers.rotateLogs,
1000 * 60 * 60 * 24); // once per day
};

workers.init = function() {
// get list of checks and execute immediately
workers.gatherAllChecks();

// start loop and check on schedule automatically
console.log('workers calling loop()');
workers.loop();
console.log('workers calling rotateLogs()');
// compress all the logs immediately
workers.rotateLogs();

//
workers.logRotationLoop();
};


workers.log = function(origCheckData, checkOutcome, state, alertWarranted, timeOfCheck) {
// form the log data
let logData = {
'check' : origCheckData,
'outcome' : checkOutcome,
'state' : state,
'alert' : alertWarranted,
'time' : timeOfCheck
};

let logString = JSON.stringify(logData);

// determine file to write to, one per check
let logFileName = origCheckData.id;

// append log data to log file
_logs.append(logFileName, logString, function(err) {
if(!err) {
console.log('Logging to file, '+logFileName+', succeeded.');
} else {
console.log('Logging to file, '+logFileName+', failed.');
}
})
};



Expand Down
26 changes: 26 additions & 0 deletions hwassign2/https/cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-----BEGIN CERTIFICATE-----
MIIEaDCCA1CgAwIBAgIJAMi6qD0EuesXMA0GCSqGSIb3DQEBBQUAMH8xCzAJBgNV
BAYTAnVzMQswCQYDVQQIEwJ2YTEOMAwGA1UEBxMFYnVya2UxDjAMBgNVBAoTBW9r
b3JuMQ4wDAYDVQQLEwVva29ybjESMBAGA1UEAxMJbG9jYWxob3N0MR8wHQYJKoZI
hvcNAQkBFhBncmVnb3JAb2tvcm4uY29tMB4XDTIwMDUwOTA3MDQ1M1oXDTMwMDUw
NzA3MDQ1M1owfzELMAkGA1UEBhMCdXMxCzAJBgNVBAgTAnZhMQ4wDAYDVQQHEwVi
dXJrZTEOMAwGA1UEChMFb2tvcm4xDjAMBgNVBAsTBW9rb3JuMRIwEAYDVQQDEwls
b2NhbGhvc3QxHzAdBgkqhkiG9w0BCQEWEGdyZWdvckBva29ybi5jb20wggEiMA0G
CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDAJpKgb4Fy8OmG4MUoH5qyIHljCLfR
AjprVPwWlMxv5vb0FNwXbjFYVwb5uBy49VoNrYHK6o0lDVaIhjPTWhnirm8HTj4l
6ylZ97JkKmI6PY8v2FYay5Cgvk71cxr4Yrj/5Gved/4RoOWeZbTFzW4VPVTH/cup
iI7vDaUiI7SCcC+z6DnU1ywH7wK1HuHtwMjUx7jYPXHWjMu0ZFX1Tx0nVS69Yln9
K21ErhkN5nhJxgxLezJv3xjTdcfGhiVZbUUV8Ex3jhajnYrjXiGCmYceM06X6Bpi
p/UghnujhzLnirEQpFFlc2Yc+jeZM6BxJYBgZq1hFaKUBKJhdcHhml0XAgMBAAGj
geYwgeMwHQYDVR0OBBYEFIXcX4MqJdK8WX6rzOOXe+UjSDouMIGzBgNVHSMEgasw
gaiAFIXcX4MqJdK8WX6rzOOXe+UjSDouoYGEpIGBMH8xCzAJBgNVBAYTAnVzMQsw
CQYDVQQIEwJ2YTEOMAwGA1UEBxMFYnVya2UxDjAMBgNVBAoTBW9rb3JuMQ4wDAYD
VQQLEwVva29ybjESMBAGA1UEAxMJbG9jYWxob3N0MR8wHQYJKoZIhvcNAQkBFhBn
cmVnb3JAb2tvcm4uY29tggkAyLqoPQS56xcwDAYDVR0TBAUwAwEB/zANBgkqhkiG
9w0BAQUFAAOCAQEAvwmX6FFDZ+ZLpp0ppzJqd0WjSdDY/ADEENriDv9n6FxUSeuF
VYaPDI+N55v1ivx/FIWJo5ctipmwHv5XsYjOIS/YWCV6LUbYLrCkiLenk7DCgsCH
ZtLg4QyaZ3vmDzgqQtZBnKsk6NFYwNQILNelSFkwTKnh2dPVj5ZNTj62LL31muDb
fG4KXKHHyInaSbueRrqtzenBmW9lAT6bkBxMl9jvzv4SPF/X6EMDoCGL7mtdt/ur
X8XhpyQKQATxVk0yQDpiZBSk5hTM0eQ1WEMxKxmDhg+wZEGPTmtzCVBsM6e6SS4N
0GQ1xAb/z0EOd4ws/6poC/GbtgRBb3pUzOqLlw==
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions hwassign2/https/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAwCaSoG+BcvDphuDFKB+asiB5Ywi30QI6a1T8FpTMb+b29BTc
F24xWFcG+bgcuP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9C8Pfv2bXZQsSTfYN1n
lU9xPDQTFDj+M8XWim3DcOW4mLuNZTS/IFdpzeqVNW9Dpe7Ur+yyOKNZkXFtImsP
Rh3B0OGFbqOi6R5K6Ds5piL1AoGAXy5cSZOOQEff3D/UfgZEuZ8WprRhVRtf5kkg
56x6tEdy59Wu1za8Tya4+5ELdWLwrcKJ/zwyij4BwtVFh1AR7q/vvU4T3ub7ldqS
ey46FR1+/eVz1cxqD5eENL8RZk0LNRYW2rrv3w2XCPq59tBEC4JmG0ZgcVqI7uue
eoK6+yECgYEAjNW7DyL4KtnWcJVZNETZYuGNY8sAdkOMLpsp9fXPd1y+7OqEQRLn
xxB1JqKdxGjxUjbzdPss/BPQn4iLxLTIo6PbKSEjESNF1sbutkNf+fTmCGc3/spA
lmMcYmRSMJI+Ok31QIXsqnD2IiTeIMbqq+ZjIhg6wDSf6YXtSHoPQ9I=
-----END RSA PRIVATE KEY-----
52 changes: 52 additions & 0 deletions hwassign2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use strict";
/*
* Launch pizza delivery APIs
*/

// dependencies
const http = require('http');
const config = require('./lib/config');
const UnsecureServer = require('./lib/unsecureserver');
const SecureServer = require('./lib/secureserver');
const fs = require('fs');
const path = require('path');
const Router = require('./lib/router');
const UserRouter = require('./lib/router_user');
const TokenRouter = require('./lib/router_token');

let app = {};

app.init = function() {

const options = {
key: fs.readFileSync(path.join(__dirname, '/https/key.pem')),
cert: fs.readFileSync(path.join(__dirname, '/https/cert.pem'))
};

console.log('env = ', config);

// initialize all routers
let router = new Router();
let userRouter = new UserRouter();
let tokenRouter = new TokenRouter();

// initialize servers, http and https
let serverHttp = new UnsecureServer(config.httpPort, config.envName);
serverHttp.router = router;
serverHttp.userRouter = userRouter;
serverHttp.tokenRouter = tokenRouter;
let serverHttps = new SecureServer(options, config.httpsPort, config.envName);
serverHttps.router = router;
serverHttps.userRouter = userRouter;
serverHttps.tokenRouter = tokenRouter;

serverHttp.init();
serverHttps.init();

// initialize router for REST requests


};

app.init();

Empty file added hwassign2/lib/cart.js
Empty file.
Loading

0 comments on commit b5d9f35

Please sign in to comment.