diff --git a/package.json b/package.json index 62b3d0d..7256048 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "cordova": "^6.1.1" }, "scripts": { - "build": "./node_modules/babel-cli/bin/babel.js src/www --out-file webserver.js" + "build": "./node_modules/babel-cli/bin/babel.js src/www/webserver.js --out-file webserver.js" }, "browser": { "cordova": false diff --git a/webserver.js b/webserver.js index dfd6dc9..9ac93d6 100644 --- a/webserver.js +++ b/webserver.js @@ -1,197 +1,3 @@ -"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", {