UNPKG

redis-smq-rest-api

Version:
138 lines 6.9 kB
"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.RedisSmqRestApi = void 0; const bodyparser_1 = require("@koa/bodyparser"); const cors_1 = __importDefault(require("@koa/cors")); const awilix_1 = require("awilix"); const bluebird_1 = __importDefault(require("bluebird")); const http = __importStar(require("http")); const koa_1 = __importDefault(require("koa")); const koa_mount_1 = __importDefault(require("koa-mount")); const koa_static_1 = __importDefault(require("koa-static")); const koa2_swagger_ui_1 = require("koa2-swagger-ui"); const path_1 = require("path"); const redis_smq_1 = require("redis-smq"); const tmp_1 = __importDefault(require("tmp")); const Container_js_1 = require("./src/app/container/Container.js"); const resource_map_js_1 = require("./src/app/router/resource-map.js"); const constants_js_1 = require("./src/config/constants.js"); const parseConfig_js_1 = require("./src/config/parseConfig.js"); const errorHandlerMiddleware_js_1 = require("./src/lib/errors/middlewares/errorHandlerMiddleware.js"); const builder_js_1 = require("./src/lib/openapi-spec/builder.js"); const index_js_1 = require("./src/lib/router/index.js"); const tmpAsync = bluebird_1.default.promisifyAll(tmp_1.default); class RedisSmqRestApi { constructor(config = {}) { this.bootstrapped = false; this.app = new koa_1.default(); this.config = (0, parseConfig_js_1.parseConfig)(config); redis_smq_1.Configuration.getSetConfig(this.config); Container_js_1.Container.registerServices(); const container = Container_js_1.Container.getInstance(); container.register({ config: (0, awilix_1.asValue)(this.config) }); this.logger = container.resolve('logger'); this.httpServer = http.createServer(this.app.callback()); } initApplicationMiddlewares() { return __awaiter(this, void 0, void 0, function* () { this.app.use((ctx, next) => { ctx.scope = Container_js_1.Container.getInstance().createScope(); return next(); }); this.app.use(errorHandlerMiddleware_js_1.errorHandlerMiddleware); this.app.use((0, bodyparser_1.bodyParser)()); this.app.use((0, cors_1.default)({ origin: '*', })); }); } initOpenApi() { return __awaiter(this, void 0, void 0, function* () { this.logger.info(`Initializing OpenAPI...`); const spec = yield (0, builder_js_1.generateOpenApiDocument)(resource_map_js_1.resourceMap, this.config.apiServer.basePath); const { port, hostname, basePath } = this.config.apiServer; const tmpDir = yield tmpAsync.dirAsync(); yield (0, builder_js_1.saveOpenApiDocument)(spec, tmpDir); this.app.use((0, koa_mount_1.default)((0, path_1.join)(basePath, '/assets'), (0, koa_static_1.default)(tmpDir))); const baseURL = `http://${hostname}:${port}${basePath === '/' ? '' : basePath}`; const specsBasePath = (0, path_1.join)('/assets/', constants_js_1.constants.openApiDocumentFilename); this.app.use((0, koa2_swagger_ui_1.koaSwagger)({ swaggerOptions: { url: specsBasePath }, })); const specsURL = (0, path_1.join)(baseURL, specsBasePath); this.logger.info(`OpenAPI specs are available at ${specsURL}`); const docsURL = (0, path_1.join)(baseURL, '/docs'); this.logger.info(`SWAGGER UI is accessible from ${docsURL}`); }); } initRouting() { return __awaiter(this, void 0, void 0, function* () { this.logger.info(`Registering routes...`); const appRouter = yield (0, index_js_1.registerResources)(resource_map_js_1.resourceMap); this.app.use(appRouter.routes()); this.app.use(appRouter.allowedMethods()); }); } bootstrap() { return __awaiter(this, void 0, void 0, function* () { if (!this.bootstrapped) { yield this.initApplicationMiddlewares(); yield this.initRouting(); yield this.initOpenApi(); this.bootstrapped = true; } }); } run() { return __awaiter(this, void 0, void 0, function* () { yield this.bootstrap(); const { hostname, port } = this.config.apiServer; yield new Promise((resolve) => this.httpServer.listen(port, hostname, () => resolve())); this.logger.info(`RedisSMQ REST API server is running on http://${hostname}:${port}...`); }); } shutdown() { return __awaiter(this, void 0, void 0, function* () { yield new Promise((resolve) => this.httpServer.close(resolve)); yield Container_js_1.Container.getInstance().dispose(); this.logger.info(`RedisSMQ HTTP API has been shutdown.`); }); } } exports.RedisSmqRestApi = RedisSmqRestApi; //# sourceMappingURL=index.js.map