|
@@ -0,0 +1,195 @@
|
|
|
|
+"use strict";
|
|
|
|
+var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
|
|
+ if (k2 === undefined) k2 = k;
|
|
|
|
+ var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
|
|
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
|
|
+ desc = { enumerable: true, get: function() { return m[k]; } };
|
|
|
|
+ }
|
|
|
|
+ Object.defineProperty(o, k2, desc);
|
|
|
|
+}) : (function(o, m, k, k2) {
|
|
|
|
+ if (k2 === undefined) k2 = k;
|
|
|
|
+ o[k2] = m[k];
|
|
|
|
+}));
|
|
|
|
+var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
|
|
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
|
|
+}) : function(o, v) {
|
|
|
|
+ o["default"] = v;
|
|
|
|
+});
|
|
|
|
+var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
+ if (mod && mod.__esModule) return mod;
|
|
|
|
+ var result = {};
|
|
|
|
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
|
|
+ __setModuleDefault(result, mod);
|
|
|
|
+ return result;
|
|
|
|
+};
|
|
|
|
+var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
|
+ return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
|
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
+ });
|
|
|
|
+};
|
|
|
|
+var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
+ return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
+};
|
|
|
|
+Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
+exports.Server = void 0;
|
|
|
|
+const express_1 = __importDefault(require("express"));
|
|
|
|
+const http_1 = require("../base/http");
|
|
|
|
+const exceptions_1 = require("../base/exceptions");
|
|
|
|
+const exceptions_2 = require("../base/exceptions");
|
|
|
|
+const logger_1 = require("../base/logger");
|
|
|
|
+const i18n_1 = require("../base/i18n");
|
|
|
|
+const path_1 = __importDefault(require("path"));
|
|
|
|
+const fs_1 = __importDefault(require("fs"));
|
|
|
|
+const exceptions_3 = require("../base/exceptions");
|
|
|
|
+const exceptions_4 = require("../base/exceptions");
|
|
|
|
+const exceptions_5 = require("../base/exceptions");
|
|
|
|
+/** @sealed */
|
|
|
|
+class Server {
|
|
|
|
+ constructor(properties) {
|
|
|
|
+ this.instance = (0, express_1.default)();
|
|
|
|
+ this.port = properties.port;
|
|
|
|
+ this.i18n = i18n_1.i18nLoader.getInstance().setLocale(properties.locale);
|
|
|
|
+ this.logger = new logger_1.Logger();
|
|
|
|
+ this.httpHandlers = {};
|
|
|
|
+ this.i18nPath = properties.i18nPath;
|
|
|
|
+ this.middlewaresPath = properties.middlewaresPath;
|
|
|
|
+ this.routesPath = properties.routesPath;
|
|
|
|
+ this.initialized = false;
|
|
|
|
+ }
|
|
|
|
+ init() {
|
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
+ this.i18n.load(path_1.default.resolve(__dirname, Server.i18nDefaultPath));
|
|
|
|
+ yield this.registerMiddlewares(path_1.default.resolve(__dirname, Server.defaultMiddlewaresPath));
|
|
|
|
+ yield this.registerRoutes(path_1.default.resolve(__dirname, Server.defaultRoutesPath));
|
|
|
|
+ yield this.postInit();
|
|
|
|
+ this.initialized = true;
|
|
|
|
+ return this;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ postInit() {
|
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
+ if (this.i18nPath)
|
|
|
|
+ this.i18n.load(this.i18nPath);
|
|
|
|
+ if (this.middlewaresPath)
|
|
|
|
+ yield this.registerMiddlewares(this.middlewaresPath);
|
|
|
|
+ if (this.routesPath)
|
|
|
|
+ yield this.registerRoutes(this.routesPath);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ processHttpHandlers() {
|
|
|
|
+ const handlers = [];
|
|
|
|
+ for (const key in this.httpHandlers)
|
|
|
|
+ handlers.push(this.httpHandlers[key]);
|
|
|
|
+ handlers.sort((a, b) => a.getOrder() - b.getOrder());
|
|
|
|
+ for (const handler of handlers) {
|
|
|
|
+ if (handler instanceof http_1.Middleware)
|
|
|
|
+ this.addMiddleware(handler);
|
|
|
|
+ else if (handler instanceof http_1.Route)
|
|
|
|
+ this.addRoute(handler);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ addMiddleware(middleware) {
|
|
|
|
+ if (middleware.getRoute() != null)
|
|
|
|
+ this.instance.use(middleware.getRoute(), middleware.getAction());
|
|
|
|
+ else
|
|
|
|
+ this.instance.use(middleware.getAction());
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+ addRoute(route) {
|
|
|
|
+ if (route.getRoute() == null)
|
|
|
|
+ throw new exceptions_1.RouteNotSetException();
|
|
|
|
+ switch (route.getMethod()) {
|
|
|
|
+ case http_1.HttpMethod.GET:
|
|
|
|
+ return this.get(route);
|
|
|
|
+ case http_1.HttpMethod.POST:
|
|
|
|
+ return this.post(route);
|
|
|
|
+ default:
|
|
|
|
+ throw new exceptions_2.IncorrectMethodException();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ logInfo(message) {
|
|
|
|
+ this.logger.getLogger().info(message);
|
|
|
|
+ }
|
|
|
|
+ logError(message) {
|
|
|
|
+ this.logger.getLogger().error(message);
|
|
|
|
+ }
|
|
|
|
+ logWarn(message) {
|
|
|
|
+ this.logger.getLogger().warn(message);
|
|
|
|
+ }
|
|
|
|
+ log(message) {
|
|
|
|
+ switch (message.type) {
|
|
|
|
+ case logger_1.MessageTypes.WARNING:
|
|
|
|
+ return this.logWarn(message.text);
|
|
|
|
+ case logger_1.MessageTypes.ERROR:
|
|
|
|
+ return this.logError(message.text);
|
|
|
|
+ default:
|
|
|
|
+ return this.logInfo(message.text);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ get(route) {
|
|
|
|
+ this.instance.get(route.getRoute(), route.getAction());
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+ post(route) {
|
|
|
|
+ this.instance.post(route.getRoute(), route.getAction());
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+ registerRoutes(dir) {
|
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
+ const files = fs_1.default.readdirSync(dir);
|
|
|
|
+ for (const file of files) {
|
|
|
|
+ if (/\.(ts|js)$/.test(file)) {
|
|
|
|
+ const { default: RouteClass } = yield Promise.resolve(`${path_1.default.join(dir, file)}`).then(s => __importStar(require(s)));
|
|
|
|
+ if (RouteClass.prototype instanceof http_1.Route) {
|
|
|
|
+ this.httpHandlers[RouteClass.name] = new RouteClass(this);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ throw new exceptions_4.InvalidRouteException(file);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return this;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ registerMiddlewares(dir) {
|
|
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
+ const files = fs_1.default.readdirSync(dir);
|
|
|
|
+ for (const file of files) {
|
|
|
|
+ if (/\.(ts|js)$/.test(file)) {
|
|
|
|
+ const { default: MiddlewareClass } = yield Promise.resolve(`${path_1.default.join(dir, file)}`).then(s => __importStar(require(s)));
|
|
|
|
+ if (MiddlewareClass.prototype instanceof http_1.Middleware) {
|
|
|
|
+ this.httpHandlers[MiddlewareClass.name] = new MiddlewareClass(this);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ throw new exceptions_3.InvalidMiddlewareException(file);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return this;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ getLogger() {
|
|
|
|
+ return this.logger;
|
|
|
|
+ }
|
|
|
|
+ i18nLoad(path) {
|
|
|
|
+ this.i18n.load(path);
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+ start(callback) {
|
|
|
|
+ if (!this.initialized)
|
|
|
|
+ throw new exceptions_5.ServerNotInitializedException();
|
|
|
|
+ this.processHttpHandlers();
|
|
|
|
+ const cb = () => {
|
|
|
|
+ this.logInfo((0, i18n_1.$$)('org.crazydoctor.expressts.start', { 'port': this.port }));
|
|
|
|
+ if (callback)
|
|
|
|
+ callback();
|
|
|
|
+ };
|
|
|
|
+ this.instance.listen(this.port, cb);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+exports.Server = Server;
|
|
|
|
+Server.i18nDefaultPath = '../resources/i18n.json';
|
|
|
|
+Server.defaultMiddlewaresPath = '../middlewares';
|
|
|
|
+Server.defaultRoutesPath = '../routes';
|