forked from pirple/The-NodeJS-Master-Class
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'restful-api-lectures' into master.
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
Showing
22 changed files
with
820 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ |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DW8g6m9iSX5jh/+bkSimQgOxkAwUog4lrkCxuW+JAnL/XnO/uNqEj | ||
LB2im821yt86X+uV3bcTxbzTtgVLmWnhJa8OZUx5AoGBAJNEesPqk0R6w3HjXTId | ||
me6rK8D6WQw8ws55tzc5oNofDm/5JYeUO9mdnaLILaMNw9C8Pfv2bXZQsSTfYN1n | ||
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----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.