Set up config requirement and use it in scripts

This commit is contained in:
Robbie Antenesse 2019-09-06 18:23:55 -06:00
parent 3d19474c34
commit f405dbad69
3 changed files with 17 additions and 2 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
node_modules/ node_modules/
public/ public/
**/*.log **/*.log
config.json

4
config.example.json Normal file
View File

@ -0,0 +1,4 @@
{
"port": 3000,
"siteName": "book-tracker"
}

View File

@ -3,6 +3,14 @@
require('make-promises-safe'); // installs an 'unhandledRejection' handler require('make-promises-safe'); // installs an 'unhandledRejection' handler
const path = require('path'); 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')({ const fastify = require('fastify')({
logger: process.env.NODE_ENV !== 'production', logger: process.env.NODE_ENV !== 'production',
}); });
@ -34,13 +42,15 @@ fastify.register(require('point-of-view'), {
}, },
}); });
fastify.decorate('siteConfig', siteConfig);
// Routes // Routes
fastify.register(require('./routes/resources')); fastify.register(require('./routes/resources'));
fastify.register(require('./routes/home')); fastify.register(require('./routes/home'));
fastify.register(require('./routes/search')); fastify.register(require('./routes/search'));
// Start the server // Start the server
fastify.listen(3000, function (err, address) { fastify.listen(fastify.siteConfig.port, function (err, address) {
if (err) { if (err) {
fastify.log.error(err); fastify.log.error(err);
process.exit(1); process.exit(1);