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 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 2c3231a..1b47ef2 100644 --- a/bin/server.js +++ b/bin/server.js @@ -9,6 +9,8 @@ const mount = require('koa-mount') const serve = require('koa-static') const cors = require('kcors') +const IRedMail = require('./iredmail') + const config = require('../config') const errorMiddleware = require('../src/middleware') @@ -52,10 +54,40 @@ async function startServer () { await app.listen(config.port) console.log(`Server started on ${config.port}`) + 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 + } catch (err) { + console.log(`Could not connect to iRedMail instance.`) + } + return app } // startServer() +/* +async function runTest () { + try { + + 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/config/env/common.js b/config/env/common.js deleted file mode 100644 index 4a03e2d..0000000 --- a/config/env/common.js +++ /dev/null @@ -1,7 +0,0 @@ -// export default { -// port: process.env.PORT || 5000 -// } - -module.exports = { - port: process.env.PORT || 5000 -} 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 db7fcb9..d8e1d3a 100644 --- a/config/index.js +++ b/config/index.js @@ -1,6 +1,16 @@ -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) { + common = require('./env/min-config') +} +// 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/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", 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