From c4c1bcb9ce288f0a366b2548d159a9c04601dae0 Mon Sep 17 00:00:00 2001 From: Maksim Sinik Date: Sat, 28 Sep 2019 11:39:12 +0200 Subject: [PATCH] fix(fastify): fixes fastify app loading --- app.ts | 30 ---------------- azure-pipeline.yml | 12 +++---- {build => azure}/azure-pipelines-npm.yml | 0 {build => azure}/azure-pipelines-yarn.yml | 0 package.json | 44 +++++++++++------------ src/app.ts | 20 ++++++++++- src/index.ts | 29 +++------------ 7 files changed, 52 insertions(+), 83 deletions(-) delete mode 100644 app.ts rename {build => azure}/azure-pipelines-npm.yml (100%) rename {build => azure}/azure-pipelines-yarn.yml (100%) diff --git a/app.ts b/app.ts deleted file mode 100644 index e9f6815a..00000000 --- a/app.ts +++ /dev/null @@ -1,30 +0,0 @@ -'use strict' - -const path = require('path') -const AutoLoad = require('fastify-autoload') - -module.exports = function(fastify, opts, next) { - // Place here your custom code! - - // Do not touch the following lines - - // This loads all plugins defined in plugins - // those should be support plugins that are reused - // through your application - fastify.register(AutoLoad, { - dir: path.join(__dirname, 'plugins'), - includeTypeScript: true, - options: Object.assign({}, opts), - }) - - // This loads all plugins defined in services - // define your routes in one of these - fastify.register(AutoLoad, { - dir: path.join(__dirname, 'services'), - includeTypeScript: true, - options: Object.assign({}, opts), - }) - - // Make sure to call next when done - next() -} diff --git a/azure-pipeline.yml b/azure-pipeline.yml index cc8238b0..ab22b931 100644 --- a/azure-pipeline.yml +++ b/azure-pipeline.yml @@ -4,27 +4,27 @@ trigger: - master jobs: - - template: build/azure-pipelines-npm.yml + - template: azure/azure-pipelines-npm.yml parameters: name: Linux_npm vmImage: ubuntu-16.04 - - template: build/azure-pipelines-npm.yml + - template: azure/azure-pipelines-npm.yml parameters: name: Windows_npm vmImage: vs2017-win2016 - - template: build/azure-pipelines-npm.yml + - template: azure/azure-pipelines-npm.yml parameters: name: macOs_npm vmImage: macOS-10.14 - - template: build/azure-pipelines-yarn.yml + - template: azure/azure-pipelines-yarn.yml parameters: name: Linux_yarn vmImage: ubuntu-16.04 - - template: build/azure-pipelines-yarn.yml + - template: azure/azure-pipelines-yarn.yml parameters: name: Windows_yarn vmImage: vs2017-win2016 - - template: build/azure-pipelines-yarn.yml + - template: azure/azure-pipelines-yarn.yml parameters: name: macOS_yarn vmImage: macOS-10.14 diff --git a/build/azure-pipelines-npm.yml b/azure/azure-pipelines-npm.yml similarity index 100% rename from build/azure-pipelines-npm.yml rename to azure/azure-pipelines-npm.yml diff --git a/build/azure-pipelines-yarn.yml b/azure/azure-pipelines-yarn.yml similarity index 100% rename from build/azure-pipelines-yarn.yml rename to azure/azure-pipelines-yarn.yml diff --git a/package.json b/package.json index d52cc8df..979eeb66 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "predev": "npm run lint", "dev": "cross-env NODE_ENV=development nodemon -e ts -w \"./src\" -x \"npm run debug\"", "debug": "node -r ts-node/register -r dotenv/config --inspect ./src/index.ts | pino-colada", - "update": "npx npm-check -u", + "update": "yarn upgrade-interactive --latest", "prelint": "npm run prettier", "lint": "eslint --fix \"./src/**/*.ts\"", "prettier": "prettier --loglevel warn --write \"./**/*.{ts,tsx}\"", @@ -28,15 +28,15 @@ "author": "Maksim Sinik ", "license": "MIT", "dependencies": { - "cross-env": "~5.2.1", + "cross-env": "^6.0.0", "fastify": "^2.0.0", "fastify-autoload": "^1.0.0", - "fastify-blipp": "~2.1.0", + "fastify-blipp": "^2.1.0", "fastify-cli": "^1.2.0", - "fastify-cors": "~2.1.3", - "fastify-helmet": "~3.0.1", - "fastify-no-icon": "~3.0.0", - "qs": "~6.8.0" + "fastify-cors": "^2.1.3", + "fastify-helmet": "^3.0.1", + "fastify-no-icon": "^3.0.0", + "qs": "^6.9.0" }, "devDependencies": { "@commitlint/cli": "^8.2.0", @@ -48,27 +48,27 @@ "@semantic-release/github": "^5.4.3", "@semantic-release/release-notes-generator": "^7.3.0", "@types/node": "^12.7.4", - "@types/qs": "~6.5.3", - "@typescript-eslint/eslint-plugin": "2.2.0", - "@typescript-eslint/parser": "2.2.0", + "@types/qs": "^6.5.3", + "@typescript-eslint/eslint-plugin": "2.3.1", + "@typescript-eslint/parser": "2.3.1", "commitizen": "^4.0.3", "commitlint-config-cz": "^0.12.1", "cz-conventional-changelog": "^3.0.2", - "dotenv": "~8.1.0", - "eslint": "~6.3.0", - "eslint-config-prettier": "~6.2.0", - "eslint-plugin-prettier": "~3.1.0", - "fastify-plugin": "~1.6.0", + "dotenv": "^8.1.0", + "eslint": "^6.4.0", + "eslint-config-prettier": "^6.3.0", + "eslint-plugin-prettier": "^3.1.0", + "fastify-plugin": "^1.6.0", "husky": "^3.0.5", - "nodemon": "~1.19.2", - "pino-colada": "~1.4.5", - "prettier": "~1.18.2", - "rimraf": "~3.0.0", + "nodemon": "^1.19.2", + "pino-colada": "^1.4.5", + "prettier": "^1.18.2", + "rimraf": "^3.0.0", "semantic-release": "^15.13.24", "tap": "^14.6.1", - "tap-mocha-reporter": "~4.0.1", + "tap-mocha-reporter": "^4.0.1", "ts-node": "^8.3.0", - "typescript": "^3.6.2" + "typescript": "~3.6.2" }, "config": { "commitizen": { @@ -78,7 +78,7 @@ "husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS", - "pre-commit": "npm test" + "pre-commit": "npm run lint && npm test" } } } diff --git a/src/app.ts b/src/app.ts index bd71cdf9..11b618b2 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,8 +2,18 @@ import { join } from 'path' import AutoLoad from 'fastify-autoload' import { FastifyInstance } from 'fastify' import { nextCallback } from 'fastify-plugin' +import noIcon from 'fastify-no-icon' +import helmet from 'fastify-helmet' +import qs from 'qs' +import cors from 'fastify-cors' + +function App(fastify: FastifyInstance, opts: {}, next: nextCallback) { + fastify.register(cors, { + allowedHeaders: ['Content-Type', 'Authorization'], + }) + fastify.register(helmet) + fastify.register(noIcon) -export = function(fastify: FastifyInstance, opts: {}, next: nextCallback) { // This loads all application wide plugins defined in plugins folder fastify.register(AutoLoad, { dir: join(__dirname, 'plugins'), @@ -20,3 +30,11 @@ export = function(fastify: FastifyInstance, opts: {}, next: nextCallback) { next() } + +App.options = { + querystringParser: (str: string) => qs.parse(str), + logger: true, + ignoreTrailingSlash: true, +} + +export = App diff --git a/src/index.ts b/src/index.ts index ee5f6541..ceced3d1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,33 +1,15 @@ // we need this file because of this issue: https://github.com/fastify/fastify-cli/issues/131 import 'make-promises-safe' +import blipp from 'fastify-blipp' import Fastify from 'fastify' -import cors from 'fastify-cors' -import noIcon from 'fastify-no-icon' -import helmet from 'fastify-helmet' -import qs from 'qs' import app from './app' console.time('Boot Time') -const fastify = Fastify({ - querystringParser: str => qs.parse(str), - logger: true, - ignoreTrailingSlash: true, -}) - -fastify.register(cors, { - allowedHeaders: ['Content-Type', 'Authorization'], -}) - -if (process.env.NODE_ENV !== 'production') { - const blipp = require('fastify-blipp') // eslint-disable-line - fastify.register(blipp) -} +const fastify = Fastify(app.options) -fastify.register(helmet) - -fastify.register(noIcon) +fastify.register(blipp) fastify.register(app) fastify.listen((process.env.PORT as any) || 3000, '0.0.0.0', err => { @@ -35,9 +17,8 @@ fastify.listen((process.env.PORT as any) || 3000, '0.0.0.0', err => { console.log(err) process.exit(1) } - if (process.env.NODE_ENV !== 'production') { - fastify.blipp() - } + fastify.blipp() + console.timeEnd('Boot Time') console.log(`Server listening on port ${(fastify.server as any).address().port}`) })