Deleted appserver, it is not a part of the project

This commit is contained in:
Michael Bykovski 2017-09-23 18:12:25 +02:00
parent d79671b0c9
commit 9df48b7fb9
3 changed files with 0 additions and 175 deletions

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}