diff options
author | Mark Powers <mark@marks.kitchen> | 2022-07-30 16:55:06 -0500 |
---|---|---|
committer | Mark Powers <mark@marks.kitchen> | 2022-07-30 16:55:06 -0500 |
commit | eadb979910da546fd64b937a0d71d2a6c4f3f934 (patch) | |
tree | 7e72c26b8625b0202dc833a36d2fa8b04d0b56bb /src/index.js |
Diffstat (limited to 'src/index.js')
-rw-r--r-- | src/index.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..dbef725 --- /dev/null +++ b/src/index.js @@ -0,0 +1,61 @@ +const server = require('./server'); +const Sequelize = require('sequelize'); +const fs = require('fs'); +const path = require('path'); +const jwt = require('jsonwebtoken'); + +const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'))); + +const dbCreds = config.database; +const secret = config.jwt_secret; + +const jwtFunctions = { + sign: function(message) { + return jwt.sign({ value: message }, secret); + }, + verify: function(token) { + return jwt.verify(token, secret).value; + } +} + +const database = new Sequelize(dbCreds.database, dbCreds.user, dbCreds.password, { + logging(str) { + console.debug(`DB:${str}`); + }, + dialectOptions: { + charset: 'utf8mb4', + multipleStatements: true, + }, +// host: dbCreds.host, + dialect: 'mysql', + pool: { + max: 5, + min: 0, + idle: 10000, + }, +}); + +database.authenticate().then(() => { + console.debug(`database connection successful: ${dbCreds.database}`); +}, (e) => console.log(e)); + +async function sync(alter, force, callback) { + await database.sync({ alter, force, logging: console.log }); +} + +function setUpModels(){ + const models = { + "emails": database.define('email', { + address: Sequelize.STRING, + name: Sequelize.STRING, + code: Sequelize.STRING, + }), + } + return models; +} + +const models = setUpModels(); +sync(); +server.setUpRoutes(models, jwtFunctions, database); +server.listen(config.port); + |