-
Notifications
You must be signed in to change notification settings - Fork 0
feat: support on templating env for route inputs #8
base: main
Are you sure you want to change the base?
Changes from 3 commits
ba3cb01
f73b8d9
095b123
53eafe9
0feaa8b
296260f
5cc29e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,18 +1,48 @@ | ||||||
'use strict'; | ||||||
|
||||||
const fs = require('fs'); | ||||||
const path = require('path'); | ||||||
const Logger = require('./lib/logger.js'); | ||||||
const SetupApp = require('./lib/setupApp'); | ||||||
const { version } = require('./package.json'); | ||||||
|
||||||
const serverPromise = SetupApp(); | ||||||
|
||||||
const cwd = process.cwd(); | ||||||
const NODE_ENV = process.env.NODE_ENV || 'development'; | ||||||
const CONFIG_BASE_PATH = process.env.CONFIG_BASE_PATH || './config'; | ||||||
const configFileName = `${NODE_ENV}.json`; | ||||||
|
||||||
let configPath; | ||||||
if (path.isAbsolute(CONFIG_BASE_PATH)) { | ||||||
configPath = path.normalize(CONFIG_BASE_PATH) + path.sep + configFileName; | ||||||
} else { | ||||||
configPath = CONFIG_BASE_PATH.replace(/\/$/, '/') + '/' + configFileName; | ||||||
} | ||||||
|
||||||
module.exports = serverPromise; | ||||||
module.exports.start = start; | ||||||
|
||||||
if (require.main === module) { | ||||||
validateConfig(); | ||||||
start(); | ||||||
} | ||||||
|
||||||
function validateConfig() { | ||||||
try { | ||||||
fs.statSync(path.relative(cwd, configPath)); | ||||||
require(configPath); | ||||||
} catch (err) { | ||||||
if (err.code === 'ENOENT') { | ||||||
const msg = `Could not find configuration file for environment ${NODE_ENV}`; | ||||||
console.log('FATAL', msg); | ||||||
console.log('FATAL', 'Set NODE_ENV to production or development or add', configPath); | ||||||
throw new Error(msg); | ||||||
} | ||||||
throw new Error(err.message); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To remove block. We won't be using config files instead as per discussion. |
||||||
} | ||||||
} | ||||||
|
||||||
function start() { | ||||||
serverPromise.then((server) => { | ||||||
process.once('SIGTERM', terminate); | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"ldap": { | ||
"databaseUser": "OVERRIDE_IN_ENV", | ||
"databasePassword": "OVERRIDE_IN_ENV" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"ldap": { | ||
"databaseUser": "OVERRIDE_IN_ENV", | ||
"databasePassword": "OVERRIDE_IN_ENV" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"ldap": { | ||
"databaseUser": "cn=read-only-admin,dc=example,dc=com", | ||
"databasePassword": "password", | ||
"url": "ldap%3A%2F%2Fldap.forumsys.com" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,8 @@ const LDAP = require('ldapjs'); | |||||
const Logger = require('../lib/logger.js'); | ||||||
const Utils = require('../lib/utils.js'); | ||||||
const Qs = require('qs'); | ||||||
const config = require('exp-config'); | ||||||
const helpers = require("../lib/helpers.js") | ||||||
|
||||||
/** | ||||||
* | ||||||
|
@@ -14,6 +16,8 @@ exports.plugin = { | |||||
name: 'ldap', | ||||||
register: async function (server) { | ||||||
server.ext('onRequest', (request, h) => { | ||||||
helpers.setQueryParameterTemplateValues(request.query, config.ldap); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. request.query is already an object at this point |
||||||
|
||||||
let { tlsOptions, attributes } = request.query; | ||||||
|
||||||
if (tlsOptions) { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
const Handlebars = require("handlebars"); | ||
|
||
Handlebars.registerHelper('ENV', (value) => { | ||
return new Handlebars.SafeString(value); | ||
}); | ||
|
||
exports.setQueryParameterTemplateValues = (query, context) => { | ||
for (const key of Object.keys(query)) { | ||
const template = Handlebars.compile(query[key]); | ||
query[key] = template(context); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"ignore": ["tmp/", "node_modules/", "logs/", "test/"], | ||
"ext": "js json env", | ||
"watch": ["*", ".env"], | ||
"env": { | ||
"ENV_PREFIX": "ONIFY_", | ||
"INTERPRET_CHAR_AS_DOT": "_" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.