From 4d462072bbe76132de7118a81329918fae5f1bb3 Mon Sep 17 00:00:00 2001 From: Michael Bykovski Date: Fri, 21 Jul 2017 11:13:38 +0200 Subject: [PATCH] Introducing the AppServer -> an abstraction of the webserver --- package-lock.json | 217 +++------------------------------ package.json | 4 +- src/www/appserver/AppServer.js | 73 +++++++++++ src/www/appserver/Request.js | 24 ++++ src/www/appserver/Response.js | 51 ++++++++ 5 files changed, 170 insertions(+), 199 deletions(-) create mode 100644 src/www/appserver/AppServer.js create mode 100644 src/www/appserver/Request.js create mode 100644 src/www/appserver/Response.js diff --git a/package-lock.json b/package-lock.json index be2ee17..95f52bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -155,24 +155,12 @@ "dev": true, "optional": true }, - "asynckit": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", - "dev": true - }, "aws-sign2": { "version": "0.5.0", "resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.5.0.tgz", "integrity": "sha1-xXED96F/wDfwLXwuZLYC6iI/fWM=", "dev": true }, - "aws4": { - "version": "1.6.0", - "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.6.0.tgz", - "integrity": "sha1-g+9cqGCysy5KDe7e6MdxudtXRx4=", - "dev": true - }, "babel-cli": { "version": "6.24.1", "resolved": "https://registry.npmjs.org/babel-cli/-/babel-cli-6.24.1.tgz", @@ -613,13 +601,6 @@ "integrity": "sha512-dwVUVIXsBZXwTuwnXI9RK8sBmgq09NDHzyR9SAph9eqk76gKK2JSQmZARC2zRC81JC2QTtxD0ARU5qTS25gIGw==", "dev": true }, - "bcrypt-pbkdf": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.1.tgz", - "integrity": "sha1-Y7xdy2EzG5K8Bf1SiVPDNGKgb40=", - "dev": true, - "optional": true - }, "big-integer": { "version": "1.6.23", "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.23.tgz", @@ -899,12 +880,6 @@ "integrity": "sha1-pNKT72frt7iNSk1CwMzwDE0eNm0=", "dev": true }, - "co": { - "version": "4.6.0", - "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", - "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=", - "dev": true - }, "code-point-at": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", @@ -1283,20 +1258,6 @@ "integrity": "sha1-gsGMJGH3QRTvFsE1IkrQuRRMoS8=", "dev": true }, - "dashdash": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", - "integrity": "sha1-hTz6D3y+L+1d4gMmuN1YEDX24vA=", - "dev": true, - "dependencies": { - "assert-plus": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", - "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=", - "dev": true - } - } - }, "date-now": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/date-now/-/date-now-0.1.4.tgz", @@ -1423,13 +1384,6 @@ "integrity": "sha1-GqdzAC4VeEV+nZ1KULDMquvL1gQ=", "dev": true }, - "ecc-jsbn": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.1.tgz", - "integrity": "sha1-D8c6ntXw1Tw4GTOYUj735UN3dQU=", - "dev": true, - "optional": true - }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -1550,12 +1504,6 @@ } } }, - "extend": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz", - "integrity": "sha1-p1Xqe8Gt/MWjHOfnYtuq3F5jZEQ=", - "dev": true - }, "extglob": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/extglob/-/extglob-0.3.2.tgz", @@ -1563,12 +1511,6 @@ "dev": true, "optional": true }, - "extsprintf": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.0.2.tgz", - "integrity": "sha1-4QgOBljjALBilJkMxw4VAiNf1VA=", - "dev": true - }, "falafel": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/falafel/-/falafel-1.2.0.tgz", @@ -1693,12 +1635,6 @@ "integrity": "sha1-jNF0XItPiinIyuw5JHaSG6GV9WA=", "dev": true }, - "fs.realpath": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", - "dev": true - }, "fsevents": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.1.2.tgz", @@ -2368,20 +2304,6 @@ "integrity": "sha1-FhdnFMgBeY5Ojyz391KUZ7tKV3E=", "dev": true }, - "getpass": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz", - "integrity": "sha1-Xv+OPmhNVprkyysSgmBOi6YhSfo=", - "dev": true, - "dependencies": { - "assert-plus": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", - "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=", - "dev": true - } - } - }, "glob": { "version": "5.0.15", "resolved": "https://registry.npmjs.org/glob/-/glob-5.0.15.tgz", @@ -2427,26 +2349,6 @@ "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=", "dev": true }, - "har-schema": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-1.0.5.tgz", - "integrity": "sha1-0mMTX0MwfALGAq/I/pWXDAFRNp4=", - "dev": true - }, - "har-validator": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/har-validator/-/har-validator-4.2.1.tgz", - "integrity": "sha1-M0gdDxu/9gDdID11gSpqX7oALio=", - "dev": true, - "dependencies": { - "ajv": { - "version": "4.11.8", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-4.11.8.tgz", - "integrity": "sha1-gv+wKynmYq5TvcIK8VlHcGc5xTY=", - "dev": true - } - } - }, "has": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/has/-/has-1.0.1.tgz", @@ -2723,12 +2625,6 @@ } } }, - "interpret": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.0.3.tgz", - "integrity": "sha1-y8NcYu7uc/Gat7EKgBURQBr8D5A=", - "dev": true - }, "invariant": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/invariant/-/invariant-2.2.2.tgz", @@ -2843,12 +2739,6 @@ "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=", "dev": true }, - "is-typedarray": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", - "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=", - "dev": true - }, "is-url": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/is-url/-/is-url-1.2.2.tgz", @@ -2868,12 +2758,6 @@ "dev": true, "optional": true }, - "isstream": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", - "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=", - "dev": true - }, "jju": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/jju/-/jju-1.3.0.tgz", @@ -2886,13 +2770,6 @@ "integrity": "sha1-mGbfOVECEw449/mWvOtlRDIJwls=", "dev": true }, - "jsbn": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", - "integrity": "sha1-peZUwuWi3rXyAdls77yoDA7y9RM=", - "dev": true, - "optional": true - }, "jsesc": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-1.3.0.tgz", @@ -2905,18 +2782,6 @@ "integrity": "sha1-E/FM4C7tTpgSl7ZOueO5MuLdE9w=", "dev": true }, - "json-schema": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz", - "integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM=", - "dev": true - }, - "json-stable-stringify": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.0.1.tgz", - "integrity": "sha1-mnWdOcXy/1A/1TAGRu1EX4jE+a8=", - "dev": true - }, "json-stringify-safe": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", @@ -2941,20 +2806,6 @@ "integrity": "sha1-P02uSpH6wxX3EGL4UhzCOfE2YoA=", "dev": true }, - "jsprim": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.0.tgz", - "integrity": "sha1-o7h+QCmNjDgFUtjMdiigu5WiKRg=", - "dev": true, - "dependencies": { - "assert-plus": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", - "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=", - "dev": true - } - } - }, "kind-of": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", @@ -3204,8 +3055,7 @@ "dev": true }, "ansistyles": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/ansistyles/-/ansistyles-0.1.3.tgz", + "version": "https://registry.npmjs.org/ansistyles/-/ansistyles-0.1.3.tgz", "integrity": "sha1-XeYEFb2gcbs3EnhUyGT0GyMlRTk=", "dev": true }, @@ -3684,8 +3534,7 @@ } }, "path-is-inside": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/path-is-inside/-/path-is-inside-1.0.1.tgz", + "version": "https://registry.npmjs.org/path-is-inside/-/path-is-inside-1.0.1.tgz", "integrity": "sha1-mNjx0DC/BL167uShulSF1AMY/Yk=", "dev": true }, @@ -4505,12 +4354,6 @@ "integrity": "sha1-z4uvrm7d/0tafvsYUmnqr0YQ3b0=", "dev": true }, - "performance-now": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-0.2.0.tgz", - "integrity": "sha1-M+8wxcd9TqIcWlOGnZG1bY8lVeU=", - "dev": true - }, "pify": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", @@ -4759,12 +4602,6 @@ } } }, - "rechoir": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", - "integrity": "sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q=", - "dev": true - }, "regenerate": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.3.2.tgz", @@ -5075,26 +4912,6 @@ "integrity": "sha1-yd96NCRZSt5r0RkA1ZZpbcBrrFc=", "dev": true }, - "sshpk": { - "version": "1.13.1", - "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.13.1.tgz", - "integrity": "sha1-US322mKHFEMW3EwY/hzx2UBzm+M=", - "dev": true, - "dependencies": { - "asn1": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.3.tgz", - "integrity": "sha1-2sh4dxPJlmhJ/IGAd36+nB3fO4Y=", - "dev": true - }, - "assert-plus": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", - "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=", - "dev": true - } - } - }, "statuses": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz", @@ -5261,13 +5078,6 @@ "integrity": "sha1-Y3PbdpCf5XDgjXNYM2Xtgop07us=", "dev": true }, - "tweetnacl": { - "version": "0.14.5", - "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz", - "integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q=", - "dev": true, - "optional": true - }, "type-is": { "version": "1.6.15", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.15.tgz", @@ -5292,6 +5102,23 @@ "integrity": "sha1-a7rwh3UA02vjTsqlhODbn+8DUgk=", "dev": true }, + "universal-router": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/universal-router/-/universal-router-3.2.0.tgz", + "integrity": "sha1-RZDuQvVeKggYjGMgcNkGdbsvpA8=", + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "path-to-regexp": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.7.0.tgz", + "integrity": "sha1-Wf3g9DW62suhA6hOnTvGTpa5k30=" + } + } + }, "unorm": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/unorm/-/unorm-1.4.1.tgz", @@ -5400,12 +5227,6 @@ "integrity": "sha1-Z1Neu2lMHVIldFeYRmUyP1h+jTc=", "dev": true }, - "verror": { - "version": "1.3.6", - "resolved": "https://registry.npmjs.org/verror/-/verror-1.3.6.tgz", - "integrity": "sha1-z/XfEpRtKX0rqu+qJoniW+AcAFw=", - "dev": true - }, "vm-browserify": { "version": "0.0.4", "resolved": "https://registry.npmjs.org/vm-browserify/-/vm-browserify-0.0.4.tgz", diff --git a/package.json b/package.json index 0341ad1..a1c3e2e 100644 --- a/package.json +++ b/package.json @@ -42,5 +42,7 @@ "scripts": { "build": "babel src/www/webserver.js --out-file webserver.js" }, - "dependencies": {} + "dependencies": { + "universal-router": "^3.2.0" + } } diff --git a/src/www/appserver/AppServer.js b/src/www/appserver/AppServer.js new file mode 100644 index 0000000..9d3623c --- /dev/null +++ b/src/www/appserver/AppServer.js @@ -0,0 +1,73 @@ +import UniversalRouter from 'universal-router'; +import Request from "./Request"; +import Response from "./Response"; + + +export default class AppServer { + constructor(port) { + this.port = port; + // Why? because SOLID of this class + this.webserver = webserver; + this.routes = []; + + this.initWebserver(); + this.initRouter(); + } + + initWebserver() { + this.webserver.onRequest(this.onRequest); + } + + initRouter() { + this.router = new UniversalRouter(this.routes); + } + + addRoute(path, callback) { + this.routes.push( + { + path: path, + action: () => callback + } + ); + this.initRouter(); + } + + onRequest(request) { + let request = new Request( + request.requestId, + request.method, + request.path, + request.query, + request.body, + request.headers + ); + + let response = new Response( + this.webserver, + request.requestId + ); + + this.router.resolve( + request.url + ).then( + // callback is a function + (callback) => { + // run the callback with all information we got for the request and the response + callback(request, response); + } + ).catch( + (error) => { + // if there is an error, just send a not found 404 bljad + response.notFound(); + } + ); + } + + start() { + this.webserver.start(); + } + + stop() { + this.webserver.stop(); + } +} \ No newline at end of file diff --git a/src/www/appserver/Request.js b/src/www/appserver/Request.js new file mode 100644 index 0000000..0bf28c3 --- /dev/null +++ b/src/www/appserver/Request.js @@ -0,0 +1,24 @@ + + + +export default class Request { + constructor( + requestId, + method, + body, + headers, + path, + query + ) { + this.requestId = requestId; + this.method = method; + this.path = path; + this.query = query; + this.body = body; + this.headers = headers; + } + + get json() { + return JSON.parse(this.body); + } +} \ No newline at end of file diff --git a/src/www/appserver/Response.js b/src/www/appserver/Response.js new file mode 100644 index 0000000..226d09f --- /dev/null +++ b/src/www/appserver/Response.js @@ -0,0 +1,51 @@ + +export default class Response { + constructor( + webserver, + requestId, + status=200, + body='', + headers={ + 'Content-Type': 'text/plain' + } + ) { + this.webserver = webserver; + this.requestId = requestId; + this.status = status; + this.body = body; + this.headers = headers; + } + + send() { + this.webserver.sendResponse( + { + status: this.status, + body: this.body, + headers: this.headers + } + ); + return this; + } + + status(status) { + this.status(status); + return this; + } + + notFound(){ + return this.status(404).send(); + } + + methodNotAllowed() { + return this.status(405).send(); + } + + ok() { + return this.status(200).send(); + } + + json(data) { + this.body = JSON.stringify(data); + return this; + } +} \ No newline at end of file