api: Add config parsing from JSON file
This commit is contained in:
parent
d5a1eed27a
commit
ad448efd7c
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
local
|
local
|
||||||
|
config.json
|
||||||
|
|
4
api/config.dev.json
Normal file
4
api/config.dev.json
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"secret": "CHANGEME!!!!!!!!!!@##@!!$$$$$$$$$$$$$!!",
|
||||||
|
"mail": false
|
||||||
|
}
|
12
api/config.json.production
Normal file
12
api/config.json.production
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"secret": "CHANGEME",
|
||||||
|
"mail": {
|
||||||
|
"from": "Sender Name <sender@example.com>",
|
||||||
|
"smtp" : {
|
||||||
|
"host": "mail.example.com",
|
||||||
|
"port": 587,
|
||||||
|
"username": "sender@example.com",
|
||||||
|
"password": "hunter2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,18 +1,29 @@
|
||||||
const nodemailer = require('nodemailer');
|
const nodemailer = require('nodemailer');
|
||||||
const config = require('../config/email');
|
const config = require('../config');
|
||||||
|
|
||||||
module.exports = sendEmail;
|
module.exports = sendEmail;
|
||||||
|
|
||||||
async function sendEmail({ to, subject, html, from = config.emailFrom }) {
|
async function sendEmail({ to, subject, html }) {
|
||||||
if (config.sendMails) {
|
if (config.mail) {
|
||||||
const transporter = nodemailer.createTransport(config.smtpOptions);
|
const from = config.mail.from;
|
||||||
|
|
||||||
|
const transporter = nodemailer.createTransport({
|
||||||
|
host: config.mail.smtp.host,
|
||||||
|
port: config.mail.smtp.port,
|
||||||
|
secure: true,
|
||||||
|
auth: {
|
||||||
|
user: config.mail.smtp.username,
|
||||||
|
pass: config.mail.smtp.password,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
await transporter.sendMail({ from, to, subject, html });
|
await transporter.sendMail({ from, to, subject, html });
|
||||||
} else {
|
} else {
|
||||||
console.log({
|
console.log(`========== E-Mail disabled, see contents below =========
|
||||||
to,
|
To: ${to}
|
||||||
subject,
|
Subject: ${subject}
|
||||||
html,
|
|
||||||
from,
|
${html}
|
||||||
});
|
`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
33
api/src/config.js
Normal file
33
api/src/config.js
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
const fs = require('fs');
|
||||||
|
const Joi = require('joi');
|
||||||
|
|
||||||
|
const configSchema = Joi.object()
|
||||||
|
.required()
|
||||||
|
.keys({
|
||||||
|
secret: Joi.string().min(16).max(128).required(),
|
||||||
|
|
||||||
|
mail: Joi.alternatives().try(
|
||||||
|
Joi.object({
|
||||||
|
from: Joi.string().required(),
|
||||||
|
smtp: Joi.object().required().keys({
|
||||||
|
host: Joi.string().required(),
|
||||||
|
port: Joi.number().default(587),
|
||||||
|
username: Joi.string().required(),
|
||||||
|
password: Joi.string().required(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
Joi.boolean().valid(false),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
const configFiles = [
|
||||||
|
process.env.CONFIG_FILE,
|
||||||
|
process.env.NODE_ENV === 'production' ? 'config.prod.json' : 'config.dev.json',
|
||||||
|
'config.json',
|
||||||
|
].filter((x) => x && fs.existsSync(x));
|
||||||
|
|
||||||
|
if (!configFiles.length) {
|
||||||
|
throw new Error('No config file found.');
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Joi.attempt(JSON.parse(fs.readFileSync(configFiles[0], 'utf8')), configSchema);
|
|
@ -1,15 +0,0 @@
|
||||||
const isProduction = process.env.NODE_ENV === 'production';
|
|
||||||
const forcedMail = process.argv.findIndex((s) => s === '--devSendMails') !== -1;
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
sendMails: isProduction || forcedMail,
|
|
||||||
emailFrom: process.env.MAILSENDER,
|
|
||||||
smtpOptions: {
|
|
||||||
host: process.env.MAILSERVER,
|
|
||||||
port: 587,
|
|
||||||
auth: {
|
|
||||||
user: process.env.MAILUSER,
|
|
||||||
pass: process.env.MAILPW,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
|
@ -1,3 +0,0 @@
|
||||||
module.exports = {
|
|
||||||
secret: process.env.NODE_ENV === 'production' ? process.env.SECRET : 'secret',
|
|
||||||
};
|
|
|
@ -10,6 +10,6 @@ require('./models/TrackData');
|
||||||
require('./models/User');
|
require('./models/User');
|
||||||
require('./models/Track');
|
require('./models/Track');
|
||||||
require('./models/Comment');
|
require('./models/Comment');
|
||||||
require('./config/passport');
|
require('./passport');
|
||||||
|
|
||||||
module.exports = mongoose;
|
module.exports = mongoose;
|
||||||
|
|
|
@ -6,7 +6,7 @@ const cors = require('cors');
|
||||||
const errorhandler = require('errorhandler');
|
const errorhandler = require('errorhandler');
|
||||||
const passport = require('passport');
|
const passport = require('passport');
|
||||||
|
|
||||||
require('./config/passport');
|
require('./passport');
|
||||||
|
|
||||||
const isProduction = process.env.NODE_ENV === 'production';
|
const isProduction = process.env.NODE_ENV === 'production';
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,9 @@ const { Strategy: BearerStrategy } = require('passport-http-bearer');
|
||||||
const { Strategy: JwtStrategy } = require('passport-jwt');
|
const { Strategy: JwtStrategy } = require('passport-jwt');
|
||||||
const { Strategy: CustomStrategy } = require('passport-custom');
|
const { Strategy: CustomStrategy } = require('passport-custom');
|
||||||
|
|
||||||
const { User, AccessToken, RefreshToken } = require('../models');
|
const { User, AccessToken, RefreshToken } = require('./models');
|
||||||
|
|
||||||
const secret = require('../config').secret;
|
const secret = require('./config').secret;
|
||||||
|
|
||||||
// used to serialize the user for the session
|
// used to serialize the user for the session
|
||||||
passport.serializeUser(function (user, done) {
|
passport.serializeUser(function (user, done) {
|
|
@ -2,7 +2,7 @@ const router = require('express').Router();
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const User = mongoose.model('User');
|
const User = mongoose.model('User');
|
||||||
const wrapRoute = require('../../_helpers/wrapRoute');
|
const wrapRoute = require('../../_helpers/wrapRoute');
|
||||||
const auth = require('../../config/passport');
|
const auth = require('../../passport');
|
||||||
|
|
||||||
// Preload user profile on routes with ':username'
|
// Preload user profile on routes with ':username'
|
||||||
router.param('username', async function (req, res, next, username) {
|
router.param('username', async function (req, res, next, username) {
|
||||||
|
|
|
@ -5,7 +5,7 @@ const Track = mongoose.model('Track');
|
||||||
const Comment = mongoose.model('Comment');
|
const Comment = mongoose.model('Comment');
|
||||||
const User = mongoose.model('User');
|
const User = mongoose.model('User');
|
||||||
const busboy = require('connect-busboy');
|
const busboy = require('connect-busboy');
|
||||||
const auth = require('../../config/passport');
|
const auth = require('../../passport');
|
||||||
const { normalizeUserAgent, buildObsver1 } = require('../../logic/tracks');
|
const { normalizeUserAgent, buildObsver1 } = require('../../logic/tracks');
|
||||||
const wrapRoute = require('../../_helpers/wrapRoute');
|
const wrapRoute = require('../../_helpers/wrapRoute');
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
const router = require('express').Router();
|
const router = require('express').Router();
|
||||||
const passport = require('passport');
|
|
||||||
const wrapRoute = require('../../_helpers/wrapRoute');
|
const wrapRoute = require('../../_helpers/wrapRoute');
|
||||||
const auth = require('../../config/passport');
|
const auth = require('../../passport');
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
'/user',
|
'/user',
|
||||||
|
|
|
@ -4,7 +4,7 @@ const { URL } = require('url');
|
||||||
const { createChallenge } = require('pkce');
|
const { createChallenge } = require('pkce');
|
||||||
|
|
||||||
const { AuthorizationCode, AccessToken, RefreshToken, Client } = require('../models');
|
const { AuthorizationCode, AccessToken, RefreshToken, Client } = require('../models');
|
||||||
const auth = require('../config/passport');
|
const auth = require('../passport');
|
||||||
const wrapRoute = require('../_helpers/wrapRoute');
|
const wrapRoute = require('../_helpers/wrapRoute');
|
||||||
|
|
||||||
// Check whether the "bigScope" fully includes the "smallScope".
|
// Check whether the "bigScope" fully includes the "smallScope".
|
||||||
|
|
|
@ -20,6 +20,7 @@ services:
|
||||||
- ./local/api-data:/data
|
- ./local/api-data:/data
|
||||||
- ./api/.migrations.js:/opt/obs/api/.migrations.js
|
- ./api/.migrations.js:/opt/obs/api/.migrations.js
|
||||||
- ./api/migrations:/opt/obs/api/migrations/
|
- ./api/migrations:/opt/obs/api/migrations/
|
||||||
|
- ./api/config.dev.json:/opt/obs/api/config.json
|
||||||
environment:
|
environment:
|
||||||
- PORT=3000
|
- PORT=3000
|
||||||
- MONGODB_URL=mongodb://mongo/obsTest
|
- MONGODB_URL=mongodb://mongo/obsTest
|
||||||
|
|
Loading…
Reference in a new issue