Server.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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, _f, _g;
  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:${this.port}`;
  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. this.swaggerComponents = (_f = properties.swagger) === null || _f === void 0 ? void 0 : _f.components;
  83. this.swaggerSecurity = (_g = properties.swagger) === null || _g === void 0 ? void 0 : _g.security;
  84. }
  85. this.initialized = false;
  86. }
  87. init() {
  88. return __awaiter(this, void 0, void 0, function* () {
  89. if (this.viewEngine)
  90. this.instance.set('view engine', this.viewEngine);
  91. if (this.viewsPath)
  92. this.instance.set('views', this.viewsPath);
  93. this.i18n.load(path_1.default.resolve(__dirname, Server.i18nDefaultPath));
  94. yield this.registerMiddlewares(path_1.default.resolve(__dirname, Server.defaultMiddlewaresPath));
  95. yield this.registerRoutes(path_1.default.resolve(__dirname, Server.defaultRoutesPath));
  96. yield this.postInit();
  97. this.initialized = true;
  98. return this;
  99. });
  100. }
  101. postInit() {
  102. return __awaiter(this, void 0, void 0, function* () {
  103. if (this.i18nPath)
  104. this.i18n.load(this.i18nPath);
  105. if (this.middlewaresPath)
  106. yield this.registerMiddlewares(this.middlewaresPath);
  107. if (this.routesPath)
  108. yield this.registerRoutes(this.routesPath);
  109. if (this.swaggerDocsPath)
  110. fs_1.default.writeFileSync(this.swaggerDocsPath, '');
  111. if (Object.keys(this.wsHandlers).length > 0) {
  112. this.registerWsServers();
  113. this.applyWsHandlers();
  114. }
  115. });
  116. }
  117. processHttpHandlers() {
  118. const handlers = [];
  119. for (const key in this.httpHandlers)
  120. handlers.push(this.httpHandlers[key]);
  121. handlers.sort((a, b) => a.getOrder() - b.getOrder());
  122. for (const handler of handlers) {
  123. if (handler instanceof http_1.Middleware)
  124. this.addMiddleware(handler);
  125. else if (handler instanceof http_1.Route)
  126. this.addRoute(handler);
  127. }
  128. }
  129. addMiddleware(middleware) {
  130. if (middleware.getRoute() != null)
  131. this.instance.use(middleware.getRoute(), middleware.getAction());
  132. else
  133. this.instance.use(middleware.getAction());
  134. return this;
  135. }
  136. addRoute(route) {
  137. if (route.getRoute() == null)
  138. throw new exceptions_1.RouteNotSetException();
  139. switch (route.getMethod()) {
  140. case http_1.HttpMethod.GET:
  141. return this.get(route);
  142. case http_1.HttpMethod.POST:
  143. return this.post(route);
  144. default:
  145. throw new exceptions_2.IncorrectMethodException();
  146. }
  147. }
  148. registerRoutesDocumentation() {
  149. for (const routeName in this.httpHandlers) {
  150. const route = this.httpHandlers[routeName];
  151. if (!(route instanceof http_1.Route))
  152. continue;
  153. if (route.getRoute() == null)
  154. throw new exceptions_1.RouteNotSetException();
  155. if (![http_1.HttpMethod.GET, http_1.HttpMethod.POST].includes(route.getMethod()))
  156. throw new exceptions_2.IncorrectMethodException();
  157. const docs = route.getDocumentation();
  158. if (this.swaggerDocsPath && docs.length > 0) {
  159. fs_1.default.appendFileSync(this.swaggerDocsPath, `${docs}\n`);
  160. this.logInfo(`Swagger documentation for route '${route.getRoute()}' generated!`);
  161. }
  162. }
  163. return this;
  164. }
  165. registerWsServers() {
  166. for (const url of Object.keys(this.wsHandlers)) {
  167. const wsServer = this.wsServers[url] = new ws_1.Server({ noServer: true });
  168. const wsHandler = this.wsHandlers[url];
  169. wsServer.on(websocket_1.WebSocketHandler.Event.CONNECTION, (ws) => {
  170. wsHandler.onConnect(ws);
  171. ws.on(websocket_1.WebSocketHandler.Event.MESSAGE, wsHandler.onMessage);
  172. ws.on(websocket_1.WebSocketHandler.Event.ERROR, wsHandler.onError);
  173. ws.on(websocket_1.WebSocketHandler.Event.CLOSE, wsHandler.onClose);
  174. });
  175. }
  176. return this;
  177. }
  178. applyWsHandlers() {
  179. this.httpServer.on('upgrade', (request, socket, head) => {
  180. var _a;
  181. const url = (_a = request.url) === null || _a === void 0 ? void 0 : _a.split('?')[0];
  182. if (url && this.wsHandlers[url] && this.wsServers[url]) {
  183. const wsServer = this.wsServers[url];
  184. wsServer.handleUpgrade(request, socket, head, (ws) => {
  185. wsServer.emit(websocket_1.WebSocketHandler.Event.CONNECTION, ws, request);
  186. });
  187. }
  188. else {
  189. socket.destroy();
  190. }
  191. });
  192. return this;
  193. }
  194. getWsConnections(url) {
  195. const wsServer = this.wsServers[url];
  196. return wsServer ? wsServer.clients : null;
  197. }
  198. logInfo(message) {
  199. this.logger.getLogger().info(message);
  200. }
  201. logError(message) {
  202. this.logger.getLogger().error(message);
  203. }
  204. logWarn(message) {
  205. this.logger.getLogger().warn(message);
  206. }
  207. log(message) {
  208. switch (message.type) {
  209. case logger_1.MessageTypes.WARNING:
  210. return this.logWarn(message.text);
  211. case logger_1.MessageTypes.ERROR:
  212. return this.logError(message.text);
  213. default:
  214. return this.logInfo(message.text);
  215. }
  216. }
  217. get(route) {
  218. this.instance.get(route.getRoute(), route.getAction());
  219. return this;
  220. }
  221. post(route) {
  222. this.instance.post(route.getRoute(), route.getAction());
  223. return this;
  224. }
  225. registerSwaggerMiddleware() {
  226. if (!this.swaggerDocsPath)
  227. return this;
  228. const swaggerOptions = {
  229. swaggerDefinition: {
  230. openapi: '3.0.1',
  231. info: {
  232. title: this.swaggerTitle,
  233. version: this.swaggerApiVersion,
  234. description: this.swaggerDescription,
  235. },
  236. servers: [
  237. { url: this.host }
  238. ],
  239. },
  240. apis: [this.swaggerDocsPath],
  241. };
  242. if (this.swaggerComponents)
  243. swaggerOptions.swaggerDefinition.components = this.swaggerComponents;
  244. if (this.swaggerSecurity)
  245. swaggerOptions.swaggerDefinition.security = this.swaggerSecurity;
  246. const swaggerDocs = (0, swagger_jsdoc_1.default)(swaggerOptions);
  247. this.instance.use(this.swaggerRoute, swagger_ui_express_1.default.serve, swagger_ui_express_1.default.setup(swaggerDocs));
  248. return this;
  249. }
  250. registerRoutes(dir) {
  251. return __awaiter(this, void 0, void 0, function* () {
  252. const files = fs_1.default.readdirSync(dir, { recursive: true, encoding: 'utf8' });
  253. for (const file of files) {
  254. if (/\.js$/.test(file)) {
  255. const { default: RouteClass } = yield Promise.resolve(`${path_1.default.join(dir, file)}`).then(s => __importStar(require(s)));
  256. if (RouteClass.prototype instanceof http_1.Route) {
  257. this.httpHandlers[RouteClass.name] = new RouteClass(this);
  258. }
  259. else
  260. throw new exceptions_4.InvalidRouteException(file);
  261. }
  262. }
  263. return this;
  264. });
  265. }
  266. registerMiddlewares(dir) {
  267. return __awaiter(this, void 0, void 0, function* () {
  268. const files = fs_1.default.readdirSync(dir, { recursive: true, encoding: 'utf8' });
  269. for (const file of files) {
  270. if (/\.js$/.test(file)) {
  271. const { default: MiddlewareClass } = yield Promise.resolve(`${path_1.default.join(dir, file)}`).then(s => __importStar(require(s)));
  272. if (MiddlewareClass.prototype instanceof http_1.Middleware) {
  273. this.httpHandlers[MiddlewareClass.name] = new MiddlewareClass(this);
  274. }
  275. else
  276. throw new exceptions_3.InvalidMiddlewareException(file);
  277. }
  278. }
  279. return this;
  280. });
  281. }
  282. getLogger() {
  283. return this.logger;
  284. }
  285. i18nLoad(path) {
  286. this.i18n.load(path);
  287. return this;
  288. }
  289. getHost() {
  290. return this.host;
  291. }
  292. getOption(key) {
  293. return this.options ? this.options[key] || null : null;
  294. }
  295. start(callback) {
  296. if (!this.initialized)
  297. throw new exceptions_5.ServerNotInitializedException();
  298. this.registerRoutesDocumentation();
  299. this.registerSwaggerMiddleware();
  300. this.processHttpHandlers();
  301. const cb = () => {
  302. this.logInfo((0, i18n_1.$$)('org.crazydoctor.expressts.start', { 'port': this.port }));
  303. if (callback)
  304. callback();
  305. };
  306. this.httpServer.listen(this.port, cb);
  307. }
  308. }
  309. exports.Server = Server;
  310. Server.i18nDefaultPath = '../resources/i18n.json';
  311. Server.defaultMiddlewaresPath = '../middlewares';
  312. Server.defaultRoutesPath = '../routes';
  313. //# sourceMappingURL=Server.js.map