From 9df48b7fb9e6ded0fad7e11e52e0f96fe9690a1f Mon Sep 17 00:00:00 2001 From: Michael Bykovski Date: Sat, 23 Sep 2017 18:12:25 +0200 Subject: [PATCH] Deleted appserver, it is not a part of the project --- src/www/appserver/AppServer.js | 91 ---------------------------------- src/www/appserver/Request.js | 24 --------- src/www/appserver/Response.js | 60 ---------------------- 3 files changed, 175 deletions(-) delete mode 100644 src/www/appserver/AppServer.js delete mode 100644 src/www/appserver/Request.js delete mode 100644 src/www/appserver/Response.js diff --git a/src/www/appserver/AppServer.js b/src/www/appserver/AppServer.js deleted file mode 100644 index 5950116..0000000 --- a/src/www/appserver/AppServer.js +++ /dev/null @@ -1,91 +0,0 @@ -/*global webserver*/ - -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.notFoundCallback = null; - - this.onRequest = this.onRequest.bind(this); - 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: (context) => { - return { - callback: callback, - context: context - }; - } - } - ); - this.initRouter(); - } - - addNotFound(callback) { - this.notFoundCallback = callback; - } - - onRequest(request) { - let requestObject = new Request( - request.requestId, - request.method, - request.body, - request.headers, - request.path, - request.query - ); - - let responseObject = new Response( - this.webserver, - requestObject.requestId - ); - - this.router.resolve( - requestObject.path - ).then( - // callback is a function - (callbackContext) => { - // run the callback with all information we got for the request and the response - requestObject.params = callbackContext.context.params; - callbackContext.callback(requestObject, responseObject); - } - ).catch( - (error) => { - if (!this.notFoundCallback) { - // if there is an error, just send a not found 404 bljad - responseObject.notFound(); - } else { - this.notFoundCallback(); - } - } - ); - } - - start() { - this.webserver.start(); - } - - stop() { - this.webserver.stop(); - } -} diff --git a/src/www/appserver/Request.js b/src/www/appserver/Request.js deleted file mode 100644 index 0bf28c3..0000000 --- a/src/www/appserver/Request.js +++ /dev/null @@ -1,24 +0,0 @@ - - - -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 deleted file mode 100644 index b1a6ac1..0000000 --- a/src/www/appserver/Response.js +++ /dev/null @@ -1,60 +0,0 @@ -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; - - this.send = this.send.bind(this); - } - - send() { - this.webserver.sendResponse( - this.requestId, - { - status: this.status, - body: this.body, - headers: this.headers - } - ); - return this; - } - - status(status) { - this.status = status; - return this; - } - - notFound(){ - this.status(404).send(); - } - - methodNotAllowed() { - this.status(405).send(); - } - - ok() { - this.status(200).send(); - } - - setHeader(key, value) { - this.headers[key] = value; - return this; - } - - json(data) { - this.setHeader('Content-Type', 'application/json'); - this.body = JSON.stringify(data); - return this; - } -}