From 5d1adddd03df1bd11329987167ac13f4a6639519 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Wed, 18 Aug 2021 09:42:06 +0100 Subject: [PATCH] Refactor health routes in to separate file Signed-off-by: James Taylor --- .../rest-api-typescript/src/health.router.ts | 48 +++++++++++++++++++ .../rest-api-typescript/src/server.ts | 43 ++--------------- 2 files changed, 51 insertions(+), 40 deletions(-) create mode 100644 asset-transfer-basic/rest-api-typescript/src/health.router.ts diff --git a/asset-transfer-basic/rest-api-typescript/src/health.router.ts b/asset-transfer-basic/rest-api-typescript/src/health.router.ts new file mode 100644 index 00000000..27b40c3d --- /dev/null +++ b/asset-transfer-basic/rest-api-typescript/src/health.router.ts @@ -0,0 +1,48 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + */ + +import express, { Request, Response } from 'express'; +import { Contract } from 'fabric-network'; +import { getReasonPhrase, StatusCodes } from 'http-status-codes'; +import { getBlockHeight } from './fabric'; +import { logger } from './logger'; +import * as config from './config'; + +const { SERVICE_UNAVAILABLE, OK } = StatusCodes; + +export const healthRouter = express.Router(); + +/* + * Example of possible health endpoints for use in a cloud environment + */ + +healthRouter.get('/ready', (_req, res: Response) => + res.status(OK).json({ + status: getReasonPhrase(OK), + timestamp: new Date().toISOString(), + }) +); + +healthRouter.get('/live', async (req: Request, res: Response) => { + logger.debug(req.body, 'Liveness request received'); + + const qsccOrg1 = req.app.get(config.mspIdOrg1).qsccContract as Contract; + const qsccOrg2 = req.app.get(config.mspIdOrg2).qsccContract as Contract; + + try { + await Promise.all([getBlockHeight(qsccOrg1), getBlockHeight(qsccOrg2)]); + } catch (err) { + logger.error(err, 'Error processing liveness request'); + + res.status(SERVICE_UNAVAILABLE).json({ + status: getReasonPhrase(SERVICE_UNAVAILABLE), + timestamp: new Date().toISOString(), + }); + } + + res.status(OK).json({ + status: getReasonPhrase(OK), + timestamp: new Date().toISOString(), + }); +}); diff --git a/asset-transfer-basic/rest-api-typescript/src/server.ts b/asset-transfer-basic/rest-api-typescript/src/server.ts index 4aeed8dd..14674a1c 100644 --- a/asset-transfer-basic/rest-api-typescript/src/server.ts +++ b/asset-transfer-basic/rest-api-typescript/src/server.ts @@ -9,24 +9,17 @@ import pinoMiddleware from 'pino-http'; import { logger } from './logger'; import { assetsRouter } from './assets.router'; +import { healthRouter } from './health.router'; import { transactionsRouter } from './transactions.router'; import { getContracts, getNetwork, - getBlockHeight, createGateway, createWallet, } from './fabric'; import { redis } from './redis'; -import { Contract } from 'fabric-network'; import * as config from './config'; -const { - BAD_REQUEST, - INTERNAL_SERVER_ERROR, - NOT_FOUND, - OK, - SERVICE_UNAVAILABLE, -} = StatusCodes; +const { BAD_REQUEST, INTERNAL_SERVER_ERROR, NOT_FOUND } = StatusCodes; import { authenticateApiKey, fabricAPIKeyStrategy } from './auth'; import passport from 'passport'; @@ -100,36 +93,7 @@ export const createServer = async (): Promise => { app.set('redis', redis); - // Health routes - app.get('/ready', (_req, res) => - res.status(OK).json({ - status: getReasonPhrase(OK), - timestamp: new Date().toISOString(), - }) - ); - app.get('/live', async (req, res) => { - logger.debug(req.body, 'Liveness request received'); - - const qsccOrg1 = req.app.get(config.mspIdOrg1).qsccContract as Contract; - const qsccOrg2 = req.app.get(config.mspIdOrg2).qsccContract as Contract; - - try { - await Promise.all([getBlockHeight(qsccOrg1), getBlockHeight(qsccOrg2)]); - } catch (err) { - logger.error(err, 'Error processing liveness request'); - - res.status(SERVICE_UNAVAILABLE).json({ - status: getReasonPhrase(SERVICE_UNAVAILABLE), - timestamp: new Date().toISOString(), - }); - } - - res.status(OK).json({ - status: getReasonPhrase(OK), - timestamp: new Date().toISOString(), - }); - }); - + app.use('/', healthRouter); app.use('/api/assets', authenticateApiKey, assetsRouter); app.use('/api/transactions', authenticateApiKey, transactionsRouter); @@ -142,7 +106,6 @@ export const createServer = async (): Promise => { ); // Print API errors - // TBC in addition to pinoMiddleware errors? app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => { logger.error(err); return res.status(INTERNAL_SERVER_ERROR).json({