api: More configurable options

This commit is contained in:
Paul Bienkowski 2021-02-27 13:20:41 +01:00
parent ccd3d80bae
commit 470dfe339d
6 changed files with 46 additions and 26 deletions

View file

@ -1,5 +1,11 @@
{ {
"cookieSecret": "CHANGEME!!!!!!!!!!@##@!!$$$$$$$$$$$$$!!", "cookieSecret": "CHANGEME!!!!!!!!!!@##@!!$$$$$$$$$$$$$!!",
"jwtSecret": "CHANGEME??????????????////3212321;312kjbkasjd", "jwtSecret": "CHANGEME??????????????////3212321;312kjbkasjd",
"mail": false "baseUrl": "http://localhost:3000/",
"mainFrontendUrl": "http://localhost:3001/",
"mail": false,
"mongodb": {
"url": "mongodb://mongo/obsTest",
"debug": true
}
} }

View file

@ -1,6 +1,8 @@
{ {
"cookieSecret": "CHANGEME!!!!!!!!!!!!!!!!!!!!!11", "cookieSecret": "CHANGEME!!!!!!!!!!!!!!!!!!!!!11",
"jwtSecret": "CHANGEME???????????????????////", "jwtSecret": "CHANGEME???????????????????////",
"baseUrl": "https://openbikesensor.example.com/",
"mainFrontendUrl": "https://openbikesensor.example.com/api/",
"mail": { "mail": {
"from": "Sender Name <sender@example.com>", "from": "Sender Name <sender@example.com>",
"smtp" : { "smtp" : {
@ -9,5 +11,9 @@
"username": "sender@example.com", "username": "sender@example.com",
"password": "hunter2" "password": "hunter2"
} }
},
"mongodb": {
"url": "mongodb://user:pass@host/obs",
"debug": false
} }
} }

View file

@ -1,25 +1,31 @@
const fs = require('fs'); const fs = require('fs');
const Joi = require('joi'); const Joi = require('joi');
const configSchema = Joi.object() const configSchema = Joi.object({
.required() jwtSecret: Joi.string().min(16).max(128).required(),
.keys({ cookieSecret: Joi.string().min(16).max(128).required(),
jwtSecret: Joi.string().min(16).max(128).required(),
cookieSecret: Joi.string().min(16).max(128).required(),
mail: Joi.alternatives().try( baseUrl: Joi.string().required(),
Joi.object({ mainFrontendUrl: Joi.string(), // optional
from: Joi.string().required(),
smtp: Joi.object().required().keys({ mail: Joi.alternatives().try(
host: Joi.string().required(), Joi.object({
port: Joi.number().default(587), from: Joi.string().required(),
username: Joi.string().required(), smtp: Joi.object({
password: Joi.string().required(), host: Joi.string().required(),
}), port: Joi.number().default(587),
}), username: Joi.string().required(),
Joi.boolean().valid(false), password: Joi.string().required(),
), }).required(),
}); }),
Joi.boolean().valid(false),
),
mongodb: Joi.object({
url: Joi.string().required(),
debug: Joi.boolean().default(process.env.NODE_ENV !== 'production'),
}).required(),
}).required();
const configFiles = [ const configFiles = [
process.env.CONFIG_FILE, process.env.CONFIG_FILE,

View file

@ -1,10 +1,9 @@
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const isProduction = process.env.NODE_ENV === 'production'; const config = require('./config')
const mongodbUrl =
process.env.MONGODB_URL || (isProduction ? 'mongodb://localhost/obs' : 'mongodb://localhost/obsTest'); mongoose.connect(config.mongodb.url);
mongoose.connect(mongodbUrl); mongoose.set('debug', config.mongodb.debug);
mongoose.set('debug', !isProduction);
require('./models/TrackData'); require('./models/TrackData');
require('./models/User'); require('./models/User');

View file

@ -6,6 +6,7 @@ const { createChallenge } = require('pkce');
const { AuthorizationCode, AccessToken, RefreshToken, Client } = require('../models'); const { AuthorizationCode, AccessToken, RefreshToken, Client } = require('../models');
const auth = require('../passport'); const auth = require('../passport');
const wrapRoute = require('../_helpers/wrapRoute'); const wrapRoute = require('../_helpers/wrapRoute');
const config = require('../config')
// Check whether the "bigScope" fully includes the "smallScope". // Check whether the "bigScope" fully includes the "smallScope".
function scopeIncludes(smallScope, bigScope) { function scopeIncludes(smallScope, bigScope) {
@ -48,6 +49,7 @@ function isValidScope(scope) {
router.use((req, res, next) => { router.use((req, res, next) => {
res.locals.user = req.user; res.locals.user = req.user;
res.locals.mainFrontendUrl = config.mainFrontendUrl
next(); next();
}); });
@ -397,7 +399,7 @@ router.get(
router.get( router.get(
'/.well-known/oauth-authorization-server', '/.well-known/oauth-authorization-server',
wrapRoute(async (req, res) => { wrapRoute(async (req, res) => {
const baseUrl = 'http://localhost:3000'; const baseUrl = config.baseUrl.replace(/\/+$/, '')
return res.json({ return res.json({
issuer: baseUrl, issuer: baseUrl,

View file

@ -130,7 +130,8 @@ html
nav nav
header OpenBikeSensor Account Pages header OpenBikeSensor Account Pages
ul ul
li: a(href="/") Back to Portal if mainFrontendUrl
li: a(href=mainFrontendUrl) Back to Portal
if !user if !user
li: a(href="/login") Login li: a(href="/login") Login
li: a(href="/register") Register li: a(href="/register") Register