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.
Initial draft of homework assignment pirple#2.
Committer: Gregor Okorn <[email protected]> On branch restful-api-lectures Changes to be committed: new file: hwassign2/https/cert.pem new file: hwassign2/https/key.pem new file: hwassign2/index.js new file: hwassign2/lib/cart.js new file: hwassign2/lib/config.js new file: hwassign2/lib/menu.js new file: hwassign2/lib/menuitem.js new file: hwassign2/lib/order.js new file: hwassign2/lib/receipt.js new file: hwassign2/lib/router.js new file: hwassign2/lib/router_token.js new file: hwassign2/lib/router_user.js new file: hwassign2/lib/secureserver.js new file: hwassign2/lib/token.js new file: hwassign2/lib/unsecureserver.js new file: hwassign2/lib/user.js new file: hwassign2/lib/utils.js new file: hwassign2/readme.txt new file: hwassign2/router.js
- Loading branch information
Gregor Okorn
authored and
Gregor Okorn
committed
May 10, 2020
1 parent
11e2988
commit 4460d2a
Showing
19 changed files
with
602 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIEaDCCA1CgAwIBAgIJAMi6qD0EuesXMA0GCSqGSIb3DQEBBQUAMH8xCzAJBgNV | ||
BAYTAnVzMQswCQYDVQQIEwJ2YTEOMAwGA1UEBxMFYnVya2UxDjAMBgNVBAoTBW9r | ||
b3JuMQ4wDAYDVQQLEwVva29ybjESMBAGA1UEAxMJbG9jYWxob3N0MR8wHQYJKoZI | ||
hvcNAQkBFhBncmVnb3JAb2tvcm4uY29tMB4XDTIwMDUwOTA3MDQ1M1oXDTMwMDUw | ||
NzA3MDQ1M1owfzELMAkGA1UEBhMCdXMxCzAJBgNVBAgTAnZhMQ4wDAYDVQQHEwVi | ||
dXJrZTEOMAwG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Vj5ZNTj62LL31muDb | ||
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 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEpQIBAAKCAQEAwCaSoG+BcvDphuDFKB+asiB5Ywi30QI6a1T8FpTMb+b29BTc | ||
F24xWFcG+bgcuPVaDa2ByuqNJQ1WiIYz01oZ4q5vB04+JespWfeyZCpiOj2PL9hW | ||
GsuQoL5O9XMa+GK4/+Rr3nf+EaDlnmW0xc1uFT1Ux/3LqYiO7w2lIiO0gnAvs+g5 | ||
1NcsB+8CtR7h7cDI1Me42D1x1ozLtGRV9U8dJ1UuvWJZ/SttRK4ZDeZ4ScYMS3sy | ||
b98Y03XHxoYlWW1FFfBMd44Wo52K414hgpmHHjNOl+gaYqf1IIZ7o4cy54qxEKRR | ||
ZXNmHPo3mTOgcSWAYGatYRWilASiYXXB4ZpdFwIDAQABAoIBAQCyTBiKnm0zxrAq | ||
466OSKU23ENGzfQjETo8FiPEoFYDEAHfAuwnIazQGBD07w5X4fKp9nIVZAeMdg/x | ||
pvW4DEcwvENyN/wbG1bHkLwLjfiPqkePUeK0Amg1f+GsiB5ilNitObuONzGeEfp/ | ||
PEi9sp2PP9XHrYypx3k6ASNVxmPdE7i6jdzszTN9nHrcMqm+Qcf2l56Szvp/tyo5 | ||
+XirhKi9Ud3iYKXjuRbO9id+MAMkViFM+5FWDn2G1MuPA0cPGRYD8LD07yEAJici | ||
/feuCf6E2O2VwacxmWBMvkss1D0SDzrZzfLVW60aeRxA6fEvBGK2bzvEpWB9ZMwC | ||
4il+FQbBAoGBAOLTmUoeY+Q3WdRcjTP2fook9KE6vqo0Sgu/TBb2v0YE1zDKeYM6 | ||
uqpe4xAx9o/D+RuwJFcxYSuPXmhUEoLliCuliNWB68kACmu1WXdp6ua6VQyozVej | ||
O50/MVlpRqXkYTFQh1q6nUFjVh0J80U2Hnk4E8EIoOkQBTSYkX2ZRXIPAoGBANjd | ||
P+bLNB31ZHbmxCMgUFAQF/mMOpxlt+OZraDssLm1hHYgUCTJ9HJf1iea9Vd0gHOD | ||
5WMPExp0qVJDW8g6m9iSX5jh/+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.
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,24 @@ | ||
"use strict"; | ||
/* | ||
* Define configurations for testing and production | ||
*/ | ||
let environments = {}; | ||
let choices = ['staging','production']; | ||
environments.staging = { | ||
'envName': 'staging', | ||
'httpPort': '3001', | ||
'httpsPort': '3002' | ||
}; | ||
|
||
|
||
environments.production = { | ||
'envName': 'production', | ||
'httpPort': '4001', | ||
'httpsPort': '4002' | ||
}; | ||
|
||
let choice = typeof(process.env.NODE_ENV) == 'string' && process.env.NODE_ENV.length > 0 | ||
? (choices.indexOf(process.env.NODE_ENV) > -1 ? process.env.NODE_ENV : 'staging') | ||
: 'staging'; | ||
|
||
module.exports = environments[choice]; |
Empty file.
Empty file.
Empty file.
Empty file.
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,54 @@ | ||
"use strict"; | ||
/* | ||
* Router class to handle routing http/https requests. | ||
*/ | ||
|
||
// dependencies | ||
const util = require('util'); | ||
const debug = util.debuglog('router'); | ||
const url = require('url'); | ||
|
||
class Router { | ||
constructor() { | ||
debug('Instantiated Router.'); | ||
} | ||
route(inputData, callback) { | ||
debug('inputData:', inputData); | ||
this.dispatch(inputData, callback); | ||
// callback(200, 'Hello\n') | ||
} | ||
|
||
dispatch(data, callback) { | ||
debug("callback inputdata:",data); | ||
let acceptableMethods = ['post','get','put','delete']; | ||
let reqMethod = data.method.toLowerCase(); | ||
if(acceptableMethods.indexOf(reqMethod) != -1) { | ||
this[reqMethod](data, callback); | ||
} else { | ||
callback("Invalid REST method requested: "+data.method); | ||
} | ||
} | ||
|
||
post(data, callback) { | ||
debug('Router post()'); | ||
callback(500, 'Router post'); | ||
} | ||
|
||
get(data, callback) { | ||
debug('Router get()'); | ||
callback(500, 'Router get'); | ||
} | ||
|
||
put(data, callback) { | ||
debug('Router update()'); | ||
callback(500, 'Router update'); | ||
} | ||
|
||
delete(data, callback) { | ||
debug('Router delete()'); | ||
callback(500, 'Router delete'); | ||
} | ||
|
||
} | ||
|
||
module.exports = Router; |
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,40 @@ | ||
"use strict"; | ||
/* | ||
* A subclass of the Router class, that handles Token requests | ||
*/ | ||
// dependencies | ||
const Router = require('./router'); | ||
const util = require('util'); | ||
const debug = util.debuglog('router_token'); | ||
|
||
class TokenRouter extends Router { | ||
constructor() { | ||
super(); | ||
debug('constructor'); | ||
|
||
} | ||
|
||
|
||
post(data, callback) { | ||
debug('post()'); | ||
callback(200, 'TokenRouter post\n'); | ||
} | ||
|
||
get(data, callback) { | ||
debug('get()'); | ||
callback(200, 'TokenRouter get\n'); | ||
} | ||
|
||
put(data, callback) { | ||
debug('update()'); | ||
callback(200, 'TokenRouter update\n'); | ||
} | ||
|
||
delete(data, callback) { | ||
debug('delete()'); | ||
callback(200, 'TokenRouter delete\n'); | ||
} | ||
} | ||
|
||
|
||
module.exports = TokenRouter; |
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,40 @@ | ||
"use strict"; | ||
/* | ||
* A subclass of the Router class, that handles User requests | ||
*/ | ||
// dependencies | ||
const Router = require('./router'); | ||
const util = require('util'); | ||
const debug = util.debuglog('router_user'); | ||
|
||
class UserRouter extends Router { | ||
constructor() { | ||
super(); | ||
debug('constructor'); | ||
|
||
} | ||
|
||
|
||
post(data, callback) { | ||
debug('post()'); | ||
callback(200, 'UserRouter post\n'); | ||
} | ||
|
||
get(data, callback) { | ||
debug('get()'); | ||
callback(200, 'UserRouter get\n'); | ||
} | ||
|
||
put(data, callback) { | ||
debug('update()'); | ||
callback(200, 'UserRouter update\n'); | ||
} | ||
|
||
delete(data, callback) { | ||
debug('delete()'); | ||
callback(200, 'UserRouter delete\n'); | ||
} | ||
} | ||
|
||
|
||
module.exports = UserRouter; |
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,35 @@ | ||
"use strict"; | ||
/* | ||
* Secure server class | ||
*/ | ||
const UnsecureServer = require('./unsecureserver'); | ||
const https = require('https'); | ||
const util = require('util'); | ||
const debug = util.debuglog('secureserver'); | ||
|
||
class SecureServer extends UnsecureServer { | ||
constructor(options, port, envName) { | ||
super(port, envName); | ||
this.options = options; | ||
this.protocol = 'https'; | ||
} | ||
|
||
init() { | ||
// create secure server to handle secure requests | ||
this.server = https.createServer(this.options, | ||
function(req, res) { | ||
this.parse(req, res, this.handle); | ||
}.bind(this) | ||
); | ||
|
||
// start listening for requests | ||
this.server.listen(this.port, | ||
function() { | ||
debug('listening on HTTPS '+ this.envName+' port:'+this.port); | ||
}.bind(this) | ||
); | ||
} | ||
} | ||
|
||
|
||
module.exports = SecureServer; |
Empty file.
Oops, something went wrong.