Compile the whole ./src/www directory
This commit is contained in:
parent
f993aab1c2
commit
b657a1b385
@ -40,7 +40,7 @@
|
||||
"cordova": "^6.1.1"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "babel src/www/webserver.js --out-file webserver.js"
|
||||
"build": "babel src/www --out-file webserver.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"universal-router": "^3.2.0"
|
||||
|
@ -33,7 +33,7 @@ export default class AppServer {
|
||||
}
|
||||
|
||||
onRequest(request) {
|
||||
let request = new Request(
|
||||
let requestObject = new Request(
|
||||
request.requestId,
|
||||
request.method,
|
||||
request.path,
|
||||
@ -42,23 +42,23 @@ export default class AppServer {
|
||||
request.headers
|
||||
);
|
||||
|
||||
let response = new Response(
|
||||
let responseObject = new Response(
|
||||
this.webserver,
|
||||
request.requestId
|
||||
requestObject.requestId
|
||||
);
|
||||
|
||||
this.router.resolve(
|
||||
request.url
|
||||
requestObject.url
|
||||
).then(
|
||||
// callback is a function
|
||||
(callback) => {
|
||||
// run the callback with all information we got for the request and the response
|
||||
callback(request, response);
|
||||
callback(requestObject, responseObject);
|
||||
}
|
||||
).catch(
|
||||
(error) => {
|
||||
// if there is an error, just send a not found 404 bljad
|
||||
response.notFound();
|
||||
responseObject.notFound();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
195
webserver.js
195
webserver.js
@ -1,3 +1,197 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||||
|
||||
var _universalRouter = require("universal-router");
|
||||
|
||||
var _universalRouter2 = _interopRequireDefault(_universalRouter);
|
||||
|
||||
var _Request = require("./Request");
|
||||
|
||||
var _Request2 = _interopRequireDefault(_Request);
|
||||
|
||||
var _Response = require("./Response");
|
||||
|
||||
var _Response2 = _interopRequireDefault(_Response);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var AppServer = function () {
|
||||
function AppServer(port) {
|
||||
_classCallCheck(this, AppServer);
|
||||
|
||||
this.port = port;
|
||||
// Why? because SOLID of this class
|
||||
this.webserver = webserver;
|
||||
this.routes = [];
|
||||
|
||||
this.initWebserver();
|
||||
this.initRouter();
|
||||
}
|
||||
|
||||
_createClass(AppServer, [{
|
||||
key: "initWebserver",
|
||||
value: function initWebserver() {
|
||||
this.webserver.onRequest(this.onRequest);
|
||||
}
|
||||
}, {
|
||||
key: "initRouter",
|
||||
value: function initRouter() {
|
||||
this.router = new _universalRouter2.default(this.routes);
|
||||
}
|
||||
}, {
|
||||
key: "addRoute",
|
||||
value: function addRoute(path, callback) {
|
||||
this.routes.push({
|
||||
path: path,
|
||||
action: function action() {
|
||||
return callback;
|
||||
}
|
||||
});
|
||||
this.initRouter();
|
||||
}
|
||||
}, {
|
||||
key: "onRequest",
|
||||
value: function onRequest(request) {
|
||||
var requestObject = new _Request2.default(request.requestId, request.method, request.path, request.query, request.body, request.headers);
|
||||
|
||||
var responseObject = new _Response2.default(this.webserver, requestObject.requestId);
|
||||
|
||||
this.router.resolve(requestObject.url).then(
|
||||
// callback is a function
|
||||
function (callback) {
|
||||
// run the callback with all information we got for the request and the response
|
||||
callback(requestObject, responseObject);
|
||||
}).catch(function (error) {
|
||||
// if there is an error, just send a not found 404 bljad
|
||||
responseObject.notFound();
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: "start",
|
||||
value: function start() {
|
||||
this.webserver.start();
|
||||
}
|
||||
}, {
|
||||
key: "stop",
|
||||
value: function stop() {
|
||||
this.webserver.stop();
|
||||
}
|
||||
}]);
|
||||
|
||||
return AppServer;
|
||||
}();
|
||||
|
||||
exports.default = AppServer;
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var Request = function () {
|
||||
function Request(requestId, method, body, headers, path, query) {
|
||||
_classCallCheck(this, Request);
|
||||
|
||||
this.requestId = requestId;
|
||||
this.method = method;
|
||||
this.path = path;
|
||||
this.query = query;
|
||||
this.body = body;
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
_createClass(Request, [{
|
||||
key: "json",
|
||||
get: function get() {
|
||||
return JSON.parse(this.body);
|
||||
}
|
||||
}]);
|
||||
|
||||
return Request;
|
||||
}();
|
||||
|
||||
exports.default = Request;
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var Response = function () {
|
||||
function Response(webserver, requestId) {
|
||||
var status = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 200;
|
||||
var body = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
|
||||
var headers = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {
|
||||
'Content-Type': 'text/plain'
|
||||
};
|
||||
|
||||
_classCallCheck(this, Response);
|
||||
|
||||
this.webserver = webserver;
|
||||
this.requestId = requestId;
|
||||
this.status = status;
|
||||
this.body = body;
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
_createClass(Response, [{
|
||||
key: 'send',
|
||||
value: function send() {
|
||||
this.webserver.sendResponse({
|
||||
status: this.status,
|
||||
body: this.body,
|
||||
headers: this.headers
|
||||
});
|
||||
return this;
|
||||
}
|
||||
}, {
|
||||
key: 'status',
|
||||
value: function status(_status) {
|
||||
this.status(_status);
|
||||
return this;
|
||||
}
|
||||
}, {
|
||||
key: 'notFound',
|
||||
value: function notFound() {
|
||||
return this.status(404).send();
|
||||
}
|
||||
}, {
|
||||
key: 'methodNotAllowed',
|
||||
value: function methodNotAllowed() {
|
||||
return this.status(405).send();
|
||||
}
|
||||
}, {
|
||||
key: 'ok',
|
||||
value: function ok() {
|
||||
return this.status(200).send();
|
||||
}
|
||||
}, {
|
||||
key: 'json',
|
||||
value: function json(data) {
|
||||
this.body = JSON.stringify(data);
|
||||
return this;
|
||||
}
|
||||
}]);
|
||||
|
||||
return Response;
|
||||
}();
|
||||
|
||||
exports.default = Response;
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
@ -21,6 +215,7 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
// Export the Appserver
|
||||
var AppServer = exports.AppServer = ImportedAppServer;
|
||||
var WEBSERVER_CLASS = 'Webserver';
|
||||
var START_FUNCTION = 'start';
|
||||
|
Loading…
Reference in New Issue
Block a user