From 21406af028d484f4c1b0fa4e225f2b820a307283 Mon Sep 17 00:00:00 2001 From: mattia richetto Date: Wed, 7 Sep 2016 15:16:08 +0100 Subject: [PATCH] codebase reorganization --- license.txt => LICENSE | 2 +- index.js | 32 +------------------ package.json | 6 ++-- {helpers => src/helpers}/default-config.js | 0 {helpers => src/helpers}/gather-os-metrics.js | 0 .../helpers}/on-headers-listener.js | 0 {helpers => src/helpers}/send-metrics.js | 0 {helpers => src/helpers}/socket-io-init.js | 0 {helpers => src/helpers}/validate.js | 0 src/middleware-wrapper.js | 31 ++++++++++++++++++ index.html => src/public/index.html | 0 app.js => src/public/javascripts/app.js | 0 style.css => src/public/stylesheets/style.css | 0 test/helpers/gather-os-metrics.spec.js | 2 +- test/helpers/on-headers-listener.spec.js | 4 +-- test/helpers/send-metrics.spec.js | 2 +- test/helpers/socket-io-init.spec.js | 4 +-- test/helpers/validate.spec.js | 4 +-- ...dex.spec.js => middleware-wrapper.spec.js} | 4 +-- 19 files changed, 46 insertions(+), 45 deletions(-) rename license.txt => LICENSE (97%) rename {helpers => src/helpers}/default-config.js (100%) rename {helpers => src/helpers}/gather-os-metrics.js (100%) rename {helpers => src/helpers}/on-headers-listener.js (100%) rename {helpers => src/helpers}/send-metrics.js (100%) rename {helpers => src/helpers}/socket-io-init.js (100%) rename {helpers => src/helpers}/validate.js (100%) create mode 100644 src/middleware-wrapper.js rename index.html => src/public/index.html (100%) rename app.js => src/public/javascripts/app.js (100%) rename style.css => src/public/stylesheets/style.css (100%) rename test/{index.spec.js => middleware-wrapper.spec.js} (88%) diff --git a/license.txt b/LICENSE similarity index 97% rename from license.txt rename to LICENSE index 3d687ec..913aa83 100644 --- a/license.txt +++ b/LICENSE @@ -1,4 +1,4 @@ -MIT License +The MIT License (MIT) Copyright (c) 2016 Rafal Wilinski diff --git a/index.js b/index.js index 7df8dd8..3fcf65e 100644 --- a/index.js +++ b/index.js @@ -1,31 +1 @@ -const fs = require('fs'); -const path = require('path'); -const onHeaders = require('on-headers'); -const validate = require('./helpers/validate'); -const onHeadersListener = require('./helpers/on-headers-listener'); -const socketIoInit = require('./helpers/socket-io-init'); - -const middlewareWrapper = (config) => { - config = validate(config); - - const renderedHtml = - fs.readFileSync(path.join(__dirname, '/index.html')) - .toString() - .replace(/{{title}}/g, config.title) - .replace(/{{script}}/g, fs.readFileSync(path.join(__dirname, '/app.js'))) - .replace(/{{style}}/g, fs.readFileSync(path.join(__dirname, '/style.css'))); - - return (req, res, next) => { - socketIoInit(req.socket.server, config.spans); - - const startTime = process.hrtime(); - if (req.path === config.path) { - res.send(renderedHtml); - } else { - onHeaders(res, () => { onHeadersListener(res.statusCode, startTime, config.spans) }); - next(); - } - }; -}; - -module.exports = middlewareWrapper; +module.exports = require('./src/middleware-wrapper'); \ No newline at end of file diff --git a/package.json b/package.json index a9226de..f25be63 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ "express", "charts" ], - "engines" : { - "node" : ">=4" + "engines": { + "node": ">=4" }, "author": "Rafal Wilinski (http://rwilinski.me)", "contributors": [ @@ -56,4 +56,4 @@ "mocha": "^3.0.2", "sinon": "^1.17.5" } -} +} \ No newline at end of file diff --git a/helpers/default-config.js b/src/helpers/default-config.js similarity index 100% rename from helpers/default-config.js rename to src/helpers/default-config.js diff --git a/helpers/gather-os-metrics.js b/src/helpers/gather-os-metrics.js similarity index 100% rename from helpers/gather-os-metrics.js rename to src/helpers/gather-os-metrics.js diff --git a/helpers/on-headers-listener.js b/src/helpers/on-headers-listener.js similarity index 100% rename from helpers/on-headers-listener.js rename to src/helpers/on-headers-listener.js diff --git a/helpers/send-metrics.js b/src/helpers/send-metrics.js similarity index 100% rename from helpers/send-metrics.js rename to src/helpers/send-metrics.js diff --git a/helpers/socket-io-init.js b/src/helpers/socket-io-init.js similarity index 100% rename from helpers/socket-io-init.js rename to src/helpers/socket-io-init.js diff --git a/helpers/validate.js b/src/helpers/validate.js similarity index 100% rename from helpers/validate.js rename to src/helpers/validate.js diff --git a/src/middleware-wrapper.js b/src/middleware-wrapper.js new file mode 100644 index 0000000..ffc3394 --- /dev/null +++ b/src/middleware-wrapper.js @@ -0,0 +1,31 @@ +const fs = require('fs'); +const path = require('path'); +const onHeaders = require('on-headers'); +const validate = require('./helpers/validate'); +const onHeadersListener = require('./helpers/on-headers-listener'); +const socketIoInit = require('./helpers/socket-io-init'); + +const middlewareWrapper = (config) => { + config = validate(config); + + const renderedHtml = + fs.readFileSync(path.join(__dirname, '/public/index.html')) + .toString() + .replace(/{{title}}/g, config.title) + .replace(/{{script}}/g, fs.readFileSync(path.join(__dirname, '/public/javascripts/app.js'))) + .replace(/{{style}}/g, fs.readFileSync(path.join(__dirname, '/public/stylesheets/style.css'))); + + return (req, res, next) => { + socketIoInit(req.socket.server, config.spans); + + const startTime = process.hrtime(); + if (req.path === config.path) { + res.send(renderedHtml); + } else { + onHeaders(res, () => { onHeadersListener(res.statusCode, startTime, config.spans) }); + next(); + } + }; +}; + +module.exports = middlewareWrapper; diff --git a/index.html b/src/public/index.html similarity index 100% rename from index.html rename to src/public/index.html diff --git a/app.js b/src/public/javascripts/app.js similarity index 100% rename from app.js rename to src/public/javascripts/app.js diff --git a/style.css b/src/public/stylesheets/style.css similarity index 100% rename from style.css rename to src/public/stylesheets/style.css diff --git a/test/helpers/gather-os-metrics.spec.js b/test/helpers/gather-os-metrics.spec.js index 3de2b00..7d099a1 100644 --- a/test/helpers/gather-os-metrics.spec.js +++ b/test/helpers/gather-os-metrics.spec.js @@ -2,7 +2,7 @@ const chai = require('chai'); chai.should(); -const gatherOsMetrics = require('../../helpers/gather-os-metrics'); +const gatherOsMetrics = require('../../src/helpers/gather-os-metrics'); describe('helpers', () => { describe('gather-os-metrics', () => { diff --git a/test/helpers/on-headers-listener.spec.js b/test/helpers/on-headers-listener.spec.js index aeaca83..0abf95c 100644 --- a/test/helpers/on-headers-listener.spec.js +++ b/test/helpers/on-headers-listener.spec.js @@ -3,8 +3,8 @@ const sinon = require('sinon'); chai.should(); -const onHeadersListener = require('../../helpers/on-headers-listener'); -const defaultConfig = require('../../helpers/default-config'); +const onHeadersListener = require('../../src/helpers/on-headers-listener'); +const defaultConfig = require('../../src/helpers/default-config'); describe('helpers', () => { describe('on-headers-listener', () => { diff --git a/test/helpers/send-metrics.spec.js b/test/helpers/send-metrics.spec.js index cfdcae1..f099abe 100644 --- a/test/helpers/send-metrics.spec.js +++ b/test/helpers/send-metrics.spec.js @@ -3,7 +3,7 @@ const sinon = require('sinon'); chai.should(); -const sendMetrics = require('../../helpers/send-metrics'); +const sendMetrics = require('../../src/helpers/send-metrics'); describe('helpers', () => { describe('send-metrics', () => { diff --git a/test/helpers/socket-io-init.spec.js b/test/helpers/socket-io-init.spec.js index a88fefe..a54fde4 100644 --- a/test/helpers/socket-io-init.spec.js +++ b/test/helpers/socket-io-init.spec.js @@ -2,8 +2,8 @@ const chai = require('chai'); chai.should(); -const socketIoInit = require('../../helpers/socket-io-init'); -const defaultConfig = require('../../helpers/default-config'); +const socketIoInit = require('../../src/helpers/socket-io-init'); +const defaultConfig = require('../../src/helpers/default-config'); describe('helpers', () => { describe('socket-io-init', () => { diff --git a/test/helpers/validate.spec.js b/test/helpers/validate.spec.js index eab95d0..2c8b6a6 100644 --- a/test/helpers/validate.spec.js +++ b/test/helpers/validate.spec.js @@ -2,8 +2,8 @@ const chai = require('chai'); chai.should(); -const defaultConfig = require('../../helpers/default-config'); -const validate = require('../../helpers/validate'); +const defaultConfig = require('../../src/helpers/default-config'); +const validate = require('../../src/helpers/validate'); describe('helpers', () => { describe('validate', () => { diff --git a/test/index.spec.js b/test/middleware-wrapper.spec.js similarity index 88% rename from test/index.spec.js rename to test/middleware-wrapper.spec.js index 653ca94..ad1ef48 100644 --- a/test/index.spec.js +++ b/test/middleware-wrapper.spec.js @@ -3,8 +3,8 @@ const sinon = require('sinon'); chai.should(); -const expresStatusMonitor = require('../index'); -const defaultConfig = require('../helpers/default-config'); +const expresStatusMonitor = require('../src/middleware-wrapper'); +const defaultConfig = require('../src/helpers/default-config'); describe('express-status-monitor', () => { describe('when initialised', () => {