import fastify from 'fastify' ; import fastifyBlipp from 'fastify-blipp' ; import metricsPlugin from 'fastify-metrics' ; import openapiGlue from 'fastify-openapi-glue' ; import StatusCodes from 'http-status-codes' ; import ApiError from './api-error' ; import { Services } from './services/services' ; import * as Controllers from './controllers/controllers' ; import { IncomingMessage , Server , ServerResponse } from 'http' ; import { CardanoCli } from './utils/cardano/cli/cardanonode-cli' ; import { CardanoNode } from './utils/cardano/cli/cardano-node' ; import { ErrorFactory } from './utils/errors' ; interface ExtraParams { networkId : string ; pageSize : number ; mock ? : boolean ; disableSearchApi ? : boolean ; } const getBodyLimit = ( ) : number | undefined => { const bodyLimit = parseInt ( process . env . BODY_LIMIT , 10 ) ; return ! Number . isNaN ( bodyLimit ) ? bodyLimit : undefined ; } ; param services param logger const buildServer = ( services , cardanoCli , cardanoNode , logLevel , extraParameters ) : fastify . FastifyInstance < Server , IncomingMessage , ServerResponse > => { const server = fastify ( { logger : { level : logLevel } , bodyLimit : getBodyLimit ( ) } ) ; const { networkId , pageSize , mock , disableSearchApi } = extraParameters ; if ( ! mock ) server . register ( metricsPlugin , { endpoint : '/metrics' } ) ; server . register ( fastifyBlipp ) ; server . register ( openapiGlue , { specification : `${ __dirname }/openApi.json` , service : Controllers . configure ( services , cardanoCli , cardanoNode , pageSize , disableSearchApi ) , noAdditional : true } ) ; server . setErrorHandler ( ( error , request , reply ) => { let toSend = error ; request . log . error ( error , '[errorHandler] An error ocurred and will be sent as response' ) ; if ( error instanceof ApiError === false ) { toSend = ErrorFactory . unspecifiedError ( `An error occurred for request ${ request . id }: ${ error . message }` ) ; } reply . status ( StatusCodes . INTERNAL_SERVER_ERROR ) . send ( { ... toSend , message : toSend . message } ) ; } ) ; return server ; } ; export default buildServer ;