From 63901432973918aa585f16c7eb1433902aa9a22e Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 20 Jan 2019 21:41:42 -0800 Subject: [PATCH 1/7] ported test code into boilerplate --- bin/server.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 4 +++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/bin/server.js b/bin/server.js index 2c3231a..51c535c 100644 --- a/bin/server.js +++ b/bin/server.js @@ -8,6 +8,7 @@ const passport = require('koa-passport') const mount = require('koa-mount') const serve = require('koa-static') const cors = require('kcors') +const Sequelize = require('sequelize') const config = require('../config') const errorMiddleware = require('../src/middleware') @@ -52,10 +53,56 @@ async function startServer () { await app.listen(config.port) console.log(`Server started on ${config.port}`) + runTest() + return app } // startServer() +async function runTest () { + try { + const sequelize = new Sequelize('vmail', 'postgres', 'postgres', { + host: 'box.bchtest.net', + dialect: 'postgres', + + pool: { + max: 5, + min: 0, + acquire: 30000, + idle: 10000 + }, + + // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators + operatorsAliases: false + }) + + const UsedQuota = sequelize.define( + 'used_quota', + { + username: { type: Sequelize.STRING, primaryKey: true }, + bytes: Sequelize.BIGINT, + messages: Sequelize.BIGINT, + domain: Sequelize.STRING + }, + { + timestamps: false + } + ) + + await sequelize.authenticate() + + console.log('Connection has been established successfully.') + + await UsedQuota.sync() + + const data = await UsedQuota.findAll() + + console.log(`data: ${JSON.stringify(data, null, 2)}`) + } catch (err) { + console.log(`Error: `, err) + } +} + // export default app // module.exports = app module.exports = { diff --git a/package.json b/package.json index 8b3b2f6..5222802 100644 --- a/package.json +++ b/package.json @@ -46,8 +46,10 @@ "koa-static": "^5.0.0", "mongoose": "^5.0.8", "passport-local": "^1.0.0", + "pg": "^7.8.0", "request": "^2.85.0", - "request-promise": "^4.2.2" + "request-promise": "^4.2.2", + "sequelize": "^4.42.0" }, "devDependencies": { "chai": "^4.1.2", From 62df57de0688cf653ddfd0848e2f5c5c7f1f1897 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 21 Jan 2019 16:15:26 -0800 Subject: [PATCH 2/7] Updating config --- bin/server.js | 4 ++-- config/env/common.js | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/bin/server.js b/bin/server.js index 51c535c..53c6175 100644 --- a/bin/server.js +++ b/bin/server.js @@ -61,8 +61,8 @@ async function startServer () { async function runTest () { try { - const sequelize = new Sequelize('vmail', 'postgres', 'postgres', { - host: 'box.bchtest.net', + const sequelize = new Sequelize('vmail', config.postgresUser, config.postgresPass, { + host: config.postgresHost, dialect: 'postgres', pool: { diff --git a/config/env/common.js b/config/env/common.js index 4a03e2d..864d927 100644 --- a/config/env/common.js +++ b/config/env/common.js @@ -1,7 +1,6 @@ -// export default { -// port: process.env.PORT || 5000 -// } - module.exports = { - port: process.env.PORT || 5000 + port: process.env.PORT || 5000, + postgresHost: 'http://127.0.0.1', + postgresUser: 'postgres', + postgresPass: 'postgres' } From 2e5cfdb106fe42eacb3e67753509b20f10e6023c Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 21 Jan 2019 16:16:16 -0800 Subject: [PATCH 3/7] Adding common.js config file to .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index c8cf125..e15c669 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,6 @@ docs .nyc_output coverage + +# common config file. +common.js From bb7dc9ad5e976d43ade142787a4aeb9a859972a9 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 21 Jan 2019 16:18:09 -0800 Subject: [PATCH 4/7] deleting common.js config file --- .gitignore | 1 - config/env/common.js | 6 ------ 2 files changed, 7 deletions(-) delete mode 100644 config/env/common.js diff --git a/.gitignore b/.gitignore index e15c669..c4a91dd 100644 --- a/.gitignore +++ b/.gitignore @@ -61,4 +61,3 @@ docs coverage # common config file. -common.js diff --git a/config/env/common.js b/config/env/common.js deleted file mode 100644 index 864d927..0000000 --- a/config/env/common.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - port: process.env.PORT || 5000, - postgresHost: 'http://127.0.0.1', - postgresUser: 'postgres', - postgresPass: 'postgres' -} From 04ea33cd969e91ea133b4086784e40bf499a6a02 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 21 Jan 2019 16:18:50 -0800 Subject: [PATCH 5/7] Removing config/env/common.js config file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c4a91dd..e15c669 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,4 @@ docs coverage # common config file. +common.js From c0346f2e32411e6167201378ebf326ff5e03e880 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 21 Jan 2019 17:17:26 -0800 Subject: [PATCH 6/7] Added iredmail class and used-quota model --- bin/iredmail.js | 45 +++++++++++++++++++++++++++++++++++ bin/server.js | 39 ++++++++---------------------- config/index.js | 10 +++++++- postgres/models/used-quota.js | 30 +++++++++++++++++++++++ 4 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 bin/iredmail.js create mode 100644 postgres/models/used-quota.js diff --git a/bin/iredmail.js b/bin/iredmail.js new file mode 100644 index 0000000..ccca338 --- /dev/null +++ b/bin/iredmail.js @@ -0,0 +1,45 @@ +/* + A class for connecting to and controlling the postgres database of an iredmail + email server. +*/ + +'use strict' + +const Sequelize = require('sequelize') +const config = require('../config') + +// Models +const UsedQuota = require('../postgres/models/used-quota') + +class IRedMail { + constructor () { + // Instantiate and initialize sequelize. + this.sequelize = this.initSequelize() + + // Load the models + this.models = { + usedQuota: new UsedQuota(this.sequelize) + } + } + + // Returns an instance of the sequelize class, configured for the Postgres + // database defined in the config object. + initSequelize () { + return new Sequelize('vmail', config.postgresUser, config.postgresPass, { + host: config.postgresHost, + dialect: 'postgres', + + pool: { + max: 5, + min: 0, + acquire: 30000, + idle: 10000 + }, + + // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators + operatorsAliases: false + }) + } +} + +module.exports = IRedMail diff --git a/bin/server.js b/bin/server.js index 53c6175..ddaa3f9 100644 --- a/bin/server.js +++ b/bin/server.js @@ -8,7 +8,8 @@ const passport = require('koa-passport') const mount = require('koa-mount') const serve = require('koa-static') const cors = require('kcors') -const Sequelize = require('sequelize') + +const IRedMail = require('./iredmail') const config = require('../config') const errorMiddleware = require('../src/middleware') @@ -53,41 +54,20 @@ async function startServer () { await app.listen(config.port) console.log(`Server started on ${config.port}`) - runTest() + const iRedMail = new IRedMail() + await iRedMail.sequelize.authenticate() + console.log('Connection to the iRedMail postgres database has been established successfully.') + + // Attach to iRedMail instantiation to the context object. + app.context.iRedMail = iRedMail return app } // startServer() +/* async function runTest () { try { - const sequelize = new Sequelize('vmail', config.postgresUser, config.postgresPass, { - host: config.postgresHost, - dialect: 'postgres', - - pool: { - max: 5, - min: 0, - acquire: 30000, - idle: 10000 - }, - - // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators - operatorsAliases: false - }) - - const UsedQuota = sequelize.define( - 'used_quota', - { - username: { type: Sequelize.STRING, primaryKey: true }, - bytes: Sequelize.BIGINT, - messages: Sequelize.BIGINT, - domain: Sequelize.STRING - }, - { - timestamps: false - } - ) await sequelize.authenticate() @@ -102,6 +82,7 @@ async function runTest () { console.log(`Error: `, err) } } +*/ // export default app // module.exports = app diff --git a/config/index.js b/config/index.js index db7fcb9..395a90d 100644 --- a/config/index.js +++ b/config/index.js @@ -1,6 +1,14 @@ -const common = require('./env/common') +// Attempt to include a common.js config file, if it exists. +let common +try { + common = require('./env/common') +} catch (err) {} +// Default to development environment. const env = process.env.NODE_ENV || 'development' + +// Load the config object for the selected environment. const config = require(`./env/${env}`) +// Merge into a single object and export. module.exports = Object.assign({}, common, config) diff --git a/postgres/models/used-quota.js b/postgres/models/used-quota.js new file mode 100644 index 0000000..b8185b7 --- /dev/null +++ b/postgres/models/used-quota.js @@ -0,0 +1,30 @@ +/* + UsedQuota table in the iRedMail postgres database. + + The constructor cxpects an instantiation of the sequalize class. Returns a + Sequelize model. +*/ + +'use strict' + +const Sequelize = require('sequelize') + +class UsedQuota { + // Expects an instantiation of the sequalize class. Returns a Sequelize model. + constructor (sequelize) { + return sequelize.define( + 'used_quota', + { + username: { type: Sequelize.STRING, primaryKey: true }, + bytes: Sequelize.BIGINT, + messages: Sequelize.BIGINT, + domain: Sequelize.STRING + }, + { + timestamps: false + } + ) + } +} + +module.exports = UsedQuota From 94bd7811a158a6f85456f0fcc669dffe56989a65 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 21 Jan 2019 17:26:27 -0800 Subject: [PATCH 7/7] Allowing server to start without iRedMail postgres connection --- bin/server.js | 14 +++++++++----- config/env/min-config.js | 3 +++ config/index.js | 4 +++- 3 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 config/env/min-config.js diff --git a/bin/server.js b/bin/server.js index ddaa3f9..1b47ef2 100644 --- a/bin/server.js +++ b/bin/server.js @@ -54,12 +54,16 @@ async function startServer () { await app.listen(config.port) console.log(`Server started on ${config.port}`) - const iRedMail = new IRedMail() - await iRedMail.sequelize.authenticate() - console.log('Connection to the iRedMail postgres database has been established successfully.') + try { + const iRedMail = new IRedMail() + await iRedMail.sequelize.authenticate() + console.log('Connection to the iRedMail postgres database has been established successfully.') - // Attach to iRedMail instantiation to the context object. - app.context.iRedMail = iRedMail + // Attach to iRedMail instantiation to the context object. + app.context.iRedMail = iRedMail + } catch (err) { + console.log(`Could not connect to iRedMail instance.`) + } return app } diff --git a/config/env/min-config.js b/config/env/min-config.js new file mode 100644 index 0000000..1e08603 --- /dev/null +++ b/config/env/min-config.js @@ -0,0 +1,3 @@ +module.exports = { + port: process.env.PORT || 5000 +} diff --git a/config/index.js b/config/index.js index 395a90d..d8e1d3a 100644 --- a/config/index.js +++ b/config/index.js @@ -2,7 +2,9 @@ let common try { common = require('./env/common') -} catch (err) {} +} catch (err) { + common = require('./env/min-config') +} // Default to development environment. const env = process.env.NODE_ENV || 'development'