From d1da553ec23522b63c9c0c5fa5249e5e8f37c555 Mon Sep 17 00:00:00 2001 From: nioc Date: Fri, 18 Oct 2019 22:42:04 +0200 Subject: [PATCH] Structure app by self-contained components --- .gitignore | 2 +- README.md | 4 ++-- docs/xmpp-bot.service | 4 ++-- config.json.dist => lib/config/config.json.dist | 0 config.js => lib/config/index.js | 2 +- error.js => lib/error/index.js | 0 logger.js => lib/logger/index.js | 0 outgoing.js => lib/outgoing/index.js | 0 server.js => lib/server.js | 0 webhook.js => lib/webhook/index.js | 0 xmpp.js => lib/xmpp/index.js | 4 ++-- package.json | 6 +++--- test/tests.js | 2 +- 13 files changed, 12 insertions(+), 12 deletions(-) rename config.json.dist => lib/config/config.json.dist (100%) rename config.js => lib/config/index.js (94%) rename error.js => lib/error/index.js (100%) rename logger.js => lib/logger/index.js (100%) rename outgoing.js => lib/outgoing/index.js (100%) rename server.js => lib/server.js (100%) rename webhook.js => lib/webhook/index.js (100%) rename xmpp.js => lib/xmpp/index.js (91%) diff --git a/.gitignore b/.gitignore index a69d3ef..1169385 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /node_modules/ -/config.json +/lib/config/config.json /*.log /.nyc_output/ /coverage/ diff --git a/README.md b/README.md index 68f685c..6b2b7d6 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,10 @@ User ⇄ XMPP client ⇄ XMPP Server ⇄ **XMPP Bot** ⇄ REST A - Create run user (optionnal): ``` useradd -r -s /bin/false xmpp-bot - chown xmpp-bot:xmpp-bot /usr/local/bin/xmpp-bot -R + chown xmpp-bot:xmpp-bot /usr/local/bin/xmpp-bot/lib -R ``` -- Set [configuration](#configuration) in `config.json` (you can copy `config.json.dist`) +- Set [configuration](#configuration) in `/lib/config/config.json` (you can copy `config.json.dist`) - Add systemd service from [model](/docs/xmpp-bot.service): ```shell diff --git a/docs/xmpp-bot.service b/docs/xmpp-bot.service index 1592724..99f675d 100644 --- a/docs/xmpp-bot.service +++ b/docs/xmpp-bot.service @@ -5,8 +5,8 @@ After=network.target [Service] User=xmpp-bot -WorkingDirectory=/usr/local/bin/xmpp-bot/ -ExecStart=/usr/bin/node /usr/local/bin/xmpp-bot/server.js +WorkingDirectory=/usr/local/bin/xmpp-bot/lib/ +ExecStart=/usr/bin/node /usr/local/bin/xmpp-bot/lib/server.js Restart=on-failure RestartSec=1000ms Environment=NODE_ENV=production diff --git a/config.json.dist b/lib/config/config.json.dist similarity index 100% rename from config.json.dist rename to lib/config/config.json.dist diff --git a/config.js b/lib/config/index.js similarity index 94% rename from config.js rename to lib/config/index.js index ac1dfb0..6107262 100644 --- a/config.js +++ b/lib/config/index.js @@ -12,7 +12,7 @@ module.exports = function Configuration (logger) { let config try { - let data = require('fs').readFileSync('./config.json') + let data = require('fs').readFileSync('./lib/config/config.json') config = JSON.parse(data) } catch (error) { logger.fatal(`Invalid configuration file: ${error.message}`) diff --git a/error.js b/lib/error/index.js similarity index 100% rename from error.js rename to lib/error/index.js diff --git a/logger.js b/lib/logger/index.js similarity index 100% rename from logger.js rename to lib/logger/index.js diff --git a/outgoing.js b/lib/outgoing/index.js similarity index 100% rename from outgoing.js rename to lib/outgoing/index.js diff --git a/server.js b/lib/server.js similarity index 100% rename from server.js rename to lib/server.js diff --git a/webhook.js b/lib/webhook/index.js similarity index 100% rename from webhook.js rename to lib/webhook/index.js diff --git a/xmpp.js b/lib/xmpp/index.js similarity index 91% rename from xmpp.js rename to lib/xmpp/index.js index 1d412e1..154b0ce 100644 --- a/xmpp.js +++ b/lib/xmpp/index.js @@ -38,7 +38,7 @@ module.exports = (logger, config) => { switch (xmppHook.action) { case 'outgoing_webhook': logger.debug(`Call outgoing webhook: ${xmppHook.args[0]}`) - require('./outgoing')(logger, config, xmpp, from, from, message, false, xmppHook.args[0]) + require('../outgoing')(logger, config, xmpp, from, from, message, false, xmppHook.args[0]) break default: break @@ -62,7 +62,7 @@ module.exports = (logger, config) => { switch (xmppHook.action) { case 'outgoing_webhook': logger.debug(`Call outgoing webhook: ${xmppHook.args[0]}`) - require('./outgoing')(logger, config, xmpp, from, conference, message, true, xmppHook.args[0]) + require('../outgoing')(logger, config, xmpp, from, conference, message, true, xmppHook.args[0]) break default: break diff --git a/package.json b/package.json index f0ee088..eda1e32 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,10 @@ "name": "xmpp-bot", "version": "1.0.0", "description": "XMPP bot", - "main": "server.js", + "main": "./lib/server.js", "scripts": { - "dev": "nodemon server.js", - "start": "NODE_ENV=production node server.js", + "dev": "nodemon lib/server.js", + "start": "NODE_ENV=production node lib/server.js", "lint": "eslint .", "test": "mocha", "cover": "nyc --reporter=html --reporter=text mocha" diff --git a/test/tests.js b/test/tests.js index 1005c66..07310cb 100644 --- a/test/tests.js +++ b/test/tests.js @@ -4,7 +4,7 @@ process.env.NODE_ENV = 'production' // eslint-disable-next-line no-unused-vars const should = require('chai').should() let request = require('request') -let server = require('../server') +let server = require('../lib/server') const baseUrl = 'http://localhost:8000/' after((done) => {