Merge pull request #1 from Permissionless-Software-Foundation/unstable

structuring project
This commit is contained in:
Chris Troutner
2019-01-21 17:35:35 -08:00
committed by GitHub
8 changed files with 127 additions and 9 deletions
+3
View File
@@ -59,3 +59,6 @@ docs
.nyc_output .nyc_output
coverage coverage
# common config file.
common.js
+45
View File
@@ -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
+32
View File
@@ -9,6 +9,8 @@ const mount = require('koa-mount')
const serve = require('koa-static') const serve = require('koa-static')
const cors = require('kcors') const cors = require('kcors')
const IRedMail = require('./iredmail')
const config = require('../config') const config = require('../config')
const errorMiddleware = require('../src/middleware') const errorMiddleware = require('../src/middleware')
@@ -52,10 +54,40 @@ async function startServer () {
await app.listen(config.port) await app.listen(config.port)
console.log(`Server started on ${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 return app
} }
// startServer() // 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 // export default app
// module.exports = app // module.exports = app
module.exports = { module.exports = {
-7
View File
@@ -1,7 +0,0 @@
// export default {
// port: process.env.PORT || 5000
// }
module.exports = {
port: process.env.PORT || 5000
}
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
port: process.env.PORT || 5000
}
+11 -1
View File
@@ -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' const env = process.env.NODE_ENV || 'development'
// Load the config object for the selected environment.
const config = require(`./env/${env}`) const config = require(`./env/${env}`)
// Merge into a single object and export.
module.exports = Object.assign({}, common, config) module.exports = Object.assign({}, common, config)
+3 -1
View File
@@ -46,8 +46,10 @@
"koa-static": "^5.0.0", "koa-static": "^5.0.0",
"mongoose": "^5.0.8", "mongoose": "^5.0.8",
"passport-local": "^1.0.0", "passport-local": "^1.0.0",
"pg": "^7.8.0",
"request": "^2.85.0", "request": "^2.85.0",
"request-promise": "^4.2.2" "request-promise": "^4.2.2",
"sequelize": "^4.42.0"
}, },
"devDependencies": { "devDependencies": {
"chai": "^4.1.2", "chai": "^4.1.2",
+30
View File
@@ -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