Server.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  26. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  27. return new (P || (P = Promise))(function (resolve, reject) {
  28. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  29. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  30. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  31. step((generator = generator.apply(thisArg, _arguments || [])).next());
  32. });
  33. };
  34. var __importDefault = (this && this.__importDefault) || function (mod) {
  35. return (mod && mod.__esModule) ? mod : { "default": mod };
  36. };
  37. Object.defineProperty(exports, "__esModule", { value: true });
  38. exports.Server = void 0;
  39. const express_1 = __importDefault(require("express"));
  40. const http_1 = require("../base/http");
  41. const exceptions_1 = require("../base/exceptions");
  42. const exceptions_2 = require("../base/exceptions");
  43. const logger_1 = require("../base/logger");
  44. const i18n_1 = require("../base/i18n");
  45. const path_1 = __importDefault(require("path"));
  46. const fs_1 = __importDefault(require("fs"));
  47. const http_2 = __importDefault(require("http"));
  48. const ws_1 = require("ws");
  49. const exceptions_3 = require("../base/exceptions");
  50. const exceptions_4 = require("../base/exceptions");
  51. const exceptions_5 = require("../base/exceptions");
  52. const websocket_1 = require("../base/websocket");
  53. const swagger_jsdoc_1 = __importDefault(require("swagger-jsdoc"));
  54. const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
  55. /** @sealed */
  56. class Server {
  57. constructor(properties) {
  58. var _a, _b, _c, _d, _e;
  59. this.instance = (0, express_1.default)();
  60. this.httpServer = http_2.default.createServer(this.instance);
  61. this.port = properties.port;
  62. this.host = properties.host || 'http://localhost:3000';
  63. this.i18n = i18n_1.i18nLoader.getInstance();
  64. if (properties.locale)
  65. this.i18n.setLocale(properties.locale);
  66. this.logger = new logger_1.Logger();
  67. this.httpHandlers = {};
  68. this.wsHandlers = properties.wsHandlers || {};
  69. this.wsServers = {};
  70. this.i18nPath = properties.i18nPath;
  71. this.middlewaresPath = properties.middlewaresPath;
  72. this.routesPath = properties.routesPath;
  73. this.viewEngine = properties.viewEngine;
  74. this.viewsPath = properties.viewsPath;
  75. this.options = properties.options;
  76. if (properties.swagger) {
  77. this.swaggerDocsPath = (_a = properties.swagger) === null || _a === void 0 ? void 0 : _a.docsPath;
  78. this.swaggerTitle = ((_b = properties.swagger) === null || _b === void 0 ? void 0 : _b.title) || 'API Documentation';
  79. this.swaggerDescription = ((_c = properties.swagger) === null || _c === void 0 ? void 0 : _c.description) || 'API Documentation';
  80. this.swaggerApiVersion = ((_d = properties.swagger) === null || _d === void 0 ? void 0 : _d.version) || '1.0.0';
  81. this.swaggerRoute = ((_e = properties.swagger) === null || _e === void 0 ? void 0 : _e.route) || '/api-docs';
  82. }
  83. this.initialized = false;
  84. }
  85. init() {
  86. return __awaiter(this, void 0, void 0, function* () {
  87. if (this.viewEngine)
  88. this.instance.set('view engine', this.viewEngine);
  89. if (this.viewsPath)
  90. this.instance.set('views', this.viewsPath);
  91. this.i18n.load(path_1.default.resolve(__dirname, Server.i18nDefaultPath));
  92. yield this.registerMiddlewares(path_1.default.resolve(__dirname, Server.defaultMiddlewaresPath));
  93. yield this.registerRoutes(path_1.default.resolve(__dirname, Server.defaultRoutesPath));
  94. yield this.postInit();
  95. this.initialized = true;
  96. return this;
  97. });
  98. }
  99. postInit() {
  100. return __awaiter(this, void 0, void 0, function* () {
  101. if (this.i18nPath)
  102. this.i18n.load(this.i18nPath);
  103. if (this.middlewaresPath)
  104. yield this.registerMiddlewares(this.middlewaresPath);
  105. if (this.routesPath)
  106. yield this.registerRoutes(this.routesPath);
  107. if (this.swaggerDocsPath)
  108. fs_1.default.writeFileSync(this.swaggerDocsPath, '');
  109. if (Object.keys(this.wsHandlers).length > 0) {
  110. this.registerWsServers();
  111. this.applyWsHandlers();
  112. }
  113. });
  114. }
  115. processHttpHandlers() {
  116. const handlers = [];
  117. for (const key in this.httpHandlers)
  118. handlers.push(this.httpHandlers[key]);
  119. handlers.sort((a, b) => a.getOrder() - b.getOrder());
  120. for (const handler of handlers) {
  121. if (handler instanceof http_1.Middleware)
  122. this.addMiddleware(handler);
  123. else if (handler instanceof http_1.Route)
  124. this.addRoute(handler);
  125. }
  126. }
  127. addMiddleware(middleware) {
  128. if (middleware.getRoute() != null)
  129. this.instance.use(middleware.getRoute(), middleware.getAction());
  130. else
  131. this.instance.use(middleware.getAction());
  132. return this;
  133. }
  134. addRoute(route) {
  135. if (route.getRoute() == null)
  136. throw new exceptions_1.RouteNotSetException();
  137. switch (route.getMethod()) {
  138. case http_1.HttpMethod.GET:
  139. return this.get(route);
  140. case http_1.HttpMethod.POST:
  141. return this.post(route);
  142. default:
  143. throw new exceptions_2.IncorrectMethodException();
  144. }
  145. }
  146. registerRoutesDocumentation() {
  147. for (const routeName in this.httpHandlers) {
  148. const route = this.httpHandlers[routeName];
  149. if (!(route instanceof http_1.Route))
  150. continue;
  151. if (route.getRoute() == null)
  152. throw new exceptions_1.RouteNotSetException();
  153. if (![http_1.HttpMethod.GET, http_1.HttpMethod.POST].includes(route.getMethod()))
  154. throw new exceptions_2.IncorrectMethodException();
  155. const docs = route.getDocumentation();
  156. if (this.swaggerDocsPath && docs.length > 0) {
  157. fs_1.default.appendFileSync(this.swaggerDocsPath, `${docs}\n`);
  158. this.logInfo(`Swagger documentation for route '${route.getRoute()}' generated!`);
  159. }
  160. }
  161. return this;
  162. }
  163. registerWsServers() {
  164. for (const url of Object.keys(this.wsHandlers)) {
  165. const wsServer = this.wsServers[url] = new ws_1.Server({ noServer: true });
  166. const wsHandler = this.wsHandlers[url];
  167. wsServer.on(websocket_1.WebSocketHandler.Event.CONNECTION, (ws) => {
  168. wsHandler.onConnect(ws);
  169. ws.on(websocket_1.WebSocketHandler.Event.MESSAGE, wsHandler.onMessage);
  170. ws.on(websocket_1.WebSocketHandler.Event.ERROR, wsHandler.onError);
  171. ws.on(websocket_1.WebSocketHandler.Event.CLOSE, wsHandler.onClose);
  172. });
  173. }
  174. return this;
  175. }
  176. applyWsHandlers() {
  177. this.httpServer.on('upgrade', (request, socket, head) => {
  178. var _a;
  179. const url = (_a = request.url) === null || _a === void 0 ? void 0 : _a.split('?')[0];
  180. if (url && this.wsHandlers[url] && this.wsServers[url]) {
  181. const wsServer = this.wsServers[url];
  182. wsServer.handleUpgrade(request, socket, head, (ws) => {
  183. wsServer.emit(websocket_1.WebSocketHandler.Event.CONNECTION, ws, request);
  184. });
  185. }
  186. else {
  187. socket.destroy();
  188. }
  189. });
  190. return this;
  191. }
  192. getWsConnections(url) {
  193. const wsServer = this.wsServers[url];
  194. return wsServer ? wsServer.clients : null;
  195. }
  196. logInfo(message) {
  197. this.logger.getLogger().info(message);
  198. }
  199. logError(message) {
  200. this.logger.getLogger().error(message);
  201. }
  202. logWarn(message) {
  203. this.logger.getLogger().warn(message);
  204. }
  205. log(message) {
  206. switch (message.type) {
  207. case logger_1.MessageTypes.WARNING:
  208. return this.logWarn(message.text);
  209. case logger_1.MessageTypes.ERROR:
  210. return this.logError(message.text);
  211. default:
  212. return this.logInfo(message.text);
  213. }
  214. }
  215. get(route) {
  216. this.instance.get(route.getRoute(), route.getAction());
  217. return this;
  218. }
  219. post(route) {
  220. this.instance.post(route.getRoute(), route.getAction());
  221. return this;
  222. }
  223. registerSwaggerMiddleware() {
  224. if (!this.swaggerDocsPath)
  225. return this;
  226. const swaggerOptions = {
  227. swaggerDefinition: {
  228. openapi: '3.0.0',
  229. info: {
  230. title: this.swaggerTitle,
  231. version: this.swaggerApiVersion,
  232. description: this.swaggerDescription,
  233. },
  234. servers: [
  235. { url: this.host }
  236. ],
  237. },
  238. apis: [this.swaggerDocsPath],
  239. };
  240. const swaggerDocs = (0, swagger_jsdoc_1.default)(swaggerOptions);
  241. this.instance.use(this.swaggerRoute, swagger_ui_express_1.default.serve, swagger_ui_express_1.default.setup(swaggerDocs));
  242. return this;
  243. }
  244. registerRoutes(dir) {
  245. return __awaiter(this, void 0, void 0, function* () {
  246. const files = fs_1.default.readdirSync(dir, { recursive: true, encoding: 'utf8' });
  247. for (const file of files) {
  248. if (/\.js$/.test(file)) {
  249. const { default: RouteClass } = yield Promise.resolve(`${path_1.default.join(dir, file)}`).then(s => __importStar(require(s)));
  250. if (RouteClass.prototype instanceof http_1.Route) {
  251. this.httpHandlers[RouteClass.name] = new RouteClass(this);
  252. }
  253. else
  254. throw new exceptions_4.InvalidRouteException(file);
  255. }
  256. }
  257. return this;
  258. });
  259. }
  260. registerMiddlewares(dir) {
  261. return __awaiter(this, void 0, void 0, function* () {
  262. const files = fs_1.default.readdirSync(dir, { recursive: true, encoding: 'utf8' });
  263. for (const file of files) {
  264. if (/\.js$/.test(file)) {
  265. const { default: MiddlewareClass } = yield Promise.resolve(`${path_1.default.join(dir, file)}`).then(s => __importStar(require(s)));
  266. if (MiddlewareClass.prototype instanceof http_1.Middleware) {
  267. this.httpHandlers[MiddlewareClass.name] = new MiddlewareClass(this);
  268. }
  269. else
  270. throw new exceptions_3.InvalidMiddlewareException(file);
  271. }
  272. }
  273. return this;
  274. });
  275. }
  276. getLogger() {
  277. return this.logger;
  278. }
  279. i18nLoad(path) {
  280. this.i18n.load(path);
  281. return this;
  282. }
  283. getOption(key) {
  284. return this.options ? this.options[key] || null : null;
  285. }
  286. start(callback) {
  287. if (!this.initialized)
  288. throw new exceptions_5.ServerNotInitializedException();
  289. this.registerRoutesDocumentation();
  290. this.registerSwaggerMiddleware();
  291. this.processHttpHandlers();
  292. const cb = () => {
  293. this.logInfo((0, i18n_1.$$)('org.crazydoctor.expressts.start', { 'port': this.port }));
  294. if (callback)
  295. callback();
  296. };
  297. this.httpServer.listen(this.port, cb);
  298. }
  299. }
  300. exports.Server = Server;
  301. Server.i18nDefaultPath = '../resources/i18n.json';
  302. Server.defaultMiddlewaresPath = '../middlewares';
  303. Server.defaultRoutesPath = '../routes';
  304. //# sourceMappingURL=Server.js.map