UNPKG

@piggly/fastify-chassis

Version:

An ESM/CommonJS toolkit to help you to do common operations in your back-end applications with Fastify and NodeJS.

186 lines 5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpServer = void 0; const ddd_toolkit_1 = require("@piggly/ddd-toolkit"); /** * @file The HTTP server. * @copyright Piggly Lab 2023 */ class HttpServer { /** * The API server. * * @protected * @memberof HttpServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ _api; /** * The running state. * * @type {boolean} * @protected * @memberof HttpServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ _running; /** * Create a new HTTP server. * * @param api The API server. * @public * @constructor * @memberof HttpServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ constructor(api) { this._api = api; this._running = false; } /** * Get the API server. * * @public * @memberof HttpServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ getApi() { return this._api; } /** * Check if the server is running. * * @returns {boolean} * @public * @memberof HttpServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ isRunning() { return this._running; } /** * Restart the server. * * @returns {Promise<boolean>} * @public * @async * @memberof HttpServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ async restart() { await this.stop(); await this.start(); return this._running; } /** * Start the server. * It will auto add current server to cleanup service if it is registered. * * @returns {Promise<boolean>} * @public * @async * @memberof HttpServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ async start() { this._running = await this.listen(); if (this._running) { const service = ddd_toolkit_1.ServiceProvider.get('CleanUpService'); if (service) { const { name } = this._api.getEnv().api.rest; service.register(`fastify:server:${name}`, async () => { await this.stop(); return true; }); } } return this._running; } /** * Stop the server. * * @returns {Promise<boolean>} * @public * @async * @memberof HttpServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ async stop() { const logger = this.getLogger(); logger.info('⚠️ HttpServer: Stopping server'); const response = await new Promise((res, rej) => { if (!this.isRunning()) { res(true); return; } this._api .getApp() .close() .then(() => { logger.info('⚡️ HttpServer: Server was closed with success'); res(true); }, (err) => { logger.error('⛔ HttpServer: An unexpected error happened while closing server', err); rej(err); }) .catch((err) => { logger.error('⛔ HttpServer: An unexpected error happened while closing server', err); rej(err); }); }); this._running = !response; return this._running; } /** * Get the logger. * * @returns {LoggerService} * @protected * @memberof HttpServer * @since 5.4.0 * @author Caique Araujo <caique@piggly.com.br> */ getLogger() { return ddd_toolkit_1.LoggerService.softResolve(); } /** * Listen to the server. * * @returns {Promise<boolean>} * @protected * @async * @memberof HttpServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ async listen() { const logger = this.getLogger(); if (this.isRunning()) { logger.warn('⚠️ HttpServer: Server is already running'); return new Promise(res => { res(false); }); } return new Promise((res, rej) => { const { host, port } = this._api.getEnv().api.rest; this._api.getApp().listen({ host, port }, (err, address) => { if (err) { logger.error('⛔ HttpServer: Error while starting to listen on host', err); return rej(err); } logger.info(`⚡️ HttpServer: Server is running at ${host}:${port} - ${address}`); res(true); }); }); } } exports.HttpServer = HttpServer; //# sourceMappingURL=HttpServer.js.map