From f405dbad69a49353f260bd9512885e7d9d65e3fc Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Fri, 6 Sep 2019 18:23:55 -0600 Subject: [PATCH] Set up config requirement and use it in scripts --- .gitignore | 3 ++- config.example.json | 4 ++++ server.js | 12 +++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 config.example.json diff --git a/.gitignore b/.gitignore index 84f97f6..14b1f3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules/ public/ -**/*.log \ No newline at end of file +**/*.log +config.json \ No newline at end of file diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..af8c047 --- /dev/null +++ b/config.example.json @@ -0,0 +1,4 @@ +{ + "port": 3000, + "siteName": "book-tracker" +} \ No newline at end of file diff --git a/server.js b/server.js index c8ffd68..5549bb0 100644 --- a/server.js +++ b/server.js @@ -3,6 +3,14 @@ require('make-promises-safe'); // installs an 'unhandledRejection' handler const path = require('path'); +let siteConfig; +try { + siteConfig = require('./config.json'); +} catch (ex) { + console.error('Please copy `config.example.json` to `config.json` and fill it with your server\'s data.'); + process.exit(1); +} + const fastify = require('fastify')({ logger: process.env.NODE_ENV !== 'production', }); @@ -34,13 +42,15 @@ fastify.register(require('point-of-view'), { }, }); +fastify.decorate('siteConfig', siteConfig); + // Routes fastify.register(require('./routes/resources')); fastify.register(require('./routes/home')); fastify.register(require('./routes/search')); // Start the server -fastify.listen(3000, function (err, address) { +fastify.listen(fastify.siteConfig.port, function (err, address) { if (err) { fastify.log.error(err); process.exit(1);