text: const Koa = require('koa');
  const logger = require('logger');
  const koaLogger = require('koa-logger');
  const config = require('config');
  const loader = require('loader');
  const convert = require('koa-convert');
  const { RWAPIMicroservice } = require('rw-api-microservice-node');
  const ErrorSerializer = require('serializers/error.serializer');
  const koaSimpleHealthCheck = require('koa-simple-healthcheck');
  
  const koaBody = require('koa-body')({
      multipart: true,
      jsonLimit: '50mb',
      formLimit: '50mb',
      textLimit: '50mb'
  });
  
  const app = new Koa();
  
  app.use(convert(koaBody));
  
  app.use(async (ctx, next) => {
      try {
          await next();
      } catch (err) {
          let error = err;
          try {
              error = JSON.parse(err);
          } catch (e) {
              logger.debug('Could not parse error message - is it JSON?: ', err);
              error = err;
          }
          ctx.status = error.status || ctx.status || 500;
          if (ctx.status >= 500) {
              logger.error(error);
          } else {
              logger.info(error);
          }
  
          ctx.body = ErrorSerializer.serializeError(ctx.status, error.message);
          if (process.env.NODE_ENV === 'prod' && this.status === 500) {
              ctx.body = 'Unexpected error';
          }
          ctx.response.type = 'application/vnd.api+json';
      }
  
  });
  
  app.use(koaLogger());
  app.use(koaSimpleHealthCheck());
  
  app.use(RWAPIMicroservice.bootstrap({
      name: config.get('service.name'),
      info: require('../microservice/register.json'),
      swagger: require('../microservice/public-swagger.json'),
      logger,
      baseURL: process.env.CT_URL,
      url: process.env.LOCAL_URL,
      token: process.env.CT_TOKEN,
      fastlyEnabled: process.env.FASTLY_ENABLED,
      fastlyServiceId: process.env.FASTLY_SERVICEID,
      fastlyAPIKey: process.env.FASTLY_APIKEY
  }));
  
  loader.loadRoutes(app);
  
  const server = app.listen(process.env.PORT, () => {
      if (process.env.CT_REGISTER_MODE === 'auto') {
          RWAPIMicroservice.register().then(() => {
              logger.info('CT registration process started');
          }, (error) => {
              logger.error(error);
              process.exit(1);
          });
      }
  });
  
  logger.info('Server started in ', process.env.PORT);
  
  module.exports = server;
  