Skip to content

Commit

Permalink
refactor: Migrate to Fastify 4
Browse files Browse the repository at this point in the history
Implements #510
  • Loading branch information
mountaindude committed Jun 21, 2022
1 parent 25583c5 commit 2e4b00e
Show file tree
Hide file tree
Showing 5 changed files with 706 additions and 544 deletions.
69 changes: 46 additions & 23 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ async function build(opts = {}) {
}

// Register rate limited for API
restServer.register(FastifyRateLimit, {
await restServer.register(FastifyRateLimit, {
max: 100,
timeWindow: '1 minute',
});

// This loads all plugins defined in plugins.
// Those should be support plugins that are reused through your application
restServer.register(require('./plugins/sensible'), { options: Object.assign({}, opts) });
restServer.register(require('./plugins/support'), { options: Object.assign({}, opts) });
await restServer.register(require('./plugins/sensible'), { options: Object.assign({}, opts) });
await restServer.register(require('./plugins/support'), { options: Object.assign({}, opts) });

// let apiDocPath = '';
// if (process.pkg) {
Expand All @@ -155,9 +155,9 @@ async function build(opts = {}) {
// exposeRoute: true,
// });

restServer.register(FastifySwagger, {
await restServer.register(FastifySwagger, {
routePrefix: '/documentation',
swagger: {
openapi: {
mode: 'dynamic',
info: {
title: 'Butler API documentation',
Expand All @@ -169,11 +169,34 @@ async function build(opts = {}) {
url: 'https://github.com/ptarmiganlabs',
description: 'Butler family of tools on GitHub',
},
host: `${globals.config.get('Butler.restServerConfig.serverHost')}:${globals.config.get('Butler.restServerConfig.serverPort')}`,
schemes: ['http'],
servers: [
{
url: `http://${globals.config.get('Butler.restServerConfig.serverHost')}:${globals.config.get(
'Butler.restServerConfig.serverPort'
)}`,
},
],
// consumes: ['application/json'],
produces: ['application/json'],
},

// swagger: {
// mode: 'dynamic',
// info: {
// title: 'Butler API documentation',
// description:
// 'Butler is a microservice that provides add-on features to Qlik Sense Enterprise on Windows.\nButler offers both a REST API and things like failed reload notifications etc.\n\nThis page contains the API documentation. Full documentation is available at https://butler.ptarmiganlabs.com',
// version: globals.appVersion,
// },
// externalDocs: {
// url: 'https://github.com/ptarmiganlabs',
// description: 'Butler family of tools on GitHub',
// },
// host: `${globals.config.get('Butler.restServerConfig.serverHost')}:${globals.config.get('Butler.restServerConfig.serverPort')}`,
// schemes: ['http'],
// // consumes: ['application/json'],
// produces: ['application/json'],
// },
uiConfig: {
deepLinking: true,
operationsSorter: 'alpha', // can also be 'alpha' or a function
Expand All @@ -183,24 +206,24 @@ async function build(opts = {}) {
});

// Loads all plugins defined in routes
restServer.register(require('./routes/api'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/base_conversion'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/butler_ping'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/disk_utils'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/key_value_store'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/mqtt_publish_message'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/newrelic_event'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/newrelic_metric'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/scheduler'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/sense_app'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/sense_app_dump'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/sense_list_apps'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/sense_start_task'), { options: Object.assign({}, opts) });
restServer.register(require('./routes/slack_post_message'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/api'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/base_conversion'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/butler_ping'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/disk_utils'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/key_value_store'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/mqtt_publish_message'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/newrelic_event'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/newrelic_metric'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/scheduler'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/sense_app'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/sense_app_dump'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/sense_list_apps'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/sense_start_task'), { options: Object.assign({}, opts) });
await restServer.register(require('./routes/slack_post_message'), { options: Object.assign({}, opts) });

// ---------------------------------------------------
// Configure X-HTTP-Method-Override handling
proxyRestServer.register(FastifyReplyFrom, {
await proxyRestServer.register(FastifyReplyFrom, {
// base: `http://localhost:${globals.config.get('Butler.restServerConfig.backgroundServerPort')}`,
base: `http://${globals.config.get('Butler.restServerConfig.serverHost')}:${globals.config.get(
'Butler.restServerConfig.backgroundServerPort'
Expand Down Expand Up @@ -273,7 +296,7 @@ async function build(opts = {}) {
}
}

dockerHealthCheckServer.register(FastifyHealthcheck);
await dockerHealthCheckServer.register(FastifyHealthcheck);

return { restServer, proxyRestServer, dockerHealthCheckServer };
}
Expand Down
18 changes: 13 additions & 5 deletions src/butler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ const start = async () => {

// restServer.listen(globals.config.get('Butler.restServerConfig.backgroundServerPort'), 'localhost', (err, address) => {
restServer.listen(
globals.config.get('Butler.restServerConfig.backgroundServerPort'),
globals.config.get('Butler.restServerConfig.serverHost'),
{
port: globals.config.get('Butler.restServerConfig.backgroundServerPort'),
host: globals.config.get('Butler.restServerConfig.serverHost'),
},

(err, address) => {
if (err) {
globals.logger.error(`MAIN: Background REST server could not listen on ${address}`);
Expand All @@ -42,11 +45,14 @@ const start = async () => {
);

proxyRestServer.listen(
globals.config.get('Butler.restServerConfig.serverPort'),
globals.config.get('Butler.restServerConfig.serverHost'),
{
port: globals.config.get('Butler.restServerConfig.serverPort'),
host: globals.config.get('Butler.restServerConfig.serverHost'),
},
(err, address) => {
if (err) {
globals.logger.error(`MAIN: REST server could not listen on ${address}`);
globals.logger.error(`MAIN: ${err.stack}`);
proxyRestServer.log.error(err);
process.exit(1);
}
Expand All @@ -67,7 +73,9 @@ const start = async () => {
try {
globals.logger.verbose('MAIN: Starting Docker healthcheck server...');

await dockerHealthCheckServer.listen(globals.config.get('Butler.dockerHealthCheck.port'));
await dockerHealthCheckServer.listen({
port: globals.config.get('Butler.dockerHealthCheck.port'),
});

globals.logger.info(`MAIN: Started Docker healthcheck server on port ${globals.config.get('Butler.dockerHealthCheck.port')}.`);
} catch (err) {
Expand Down
Loading

0 comments on commit 2e4b00e

Please sign in to comment.