Skip to content

Commit

Permalink
#325: Extract config and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
groenroos committed Oct 28, 2021
1 parent fc371b1 commit 4b6173e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 35 deletions.
57 changes: 33 additions & 24 deletions core/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import SaplingError from '../lib/SaplingError.js';


/**
* Load the configuration data. Should exist in a file
* called "config.json" and must be valid JSON.
* Digest config files and apply default config
*
* @param {function} next Chain callback
* @returns {object} Config
*/
export default async function loadConfig(next) {
export function digest() {
let config = {};

const argv = yargs(hideBin(process.argv)).argv;

/* Default configuration values */
Expand Down Expand Up @@ -67,8 +68,7 @@ export default async function loadConfig(next) {
url: '',
};

this.config = {};
Object.assign(this.config, defaultConfig);
Object.assign(config, defaultConfig);

/* Location of the configuration */
const configPath = path.join(this.dir, this.configFile || 'config.json');
Expand All @@ -81,60 +81,69 @@ export default async function loadConfig(next) {
/* Parse and merge the config, or throw an error if it's malformed */
try {
const c = JSON.parse(file.toString());
_.extend(this.config, c);
_.extend(config, c);
} catch (error) {
throw new SaplingError('Error loading config', error);
}
} else {
/* If not, let's add a fallback */
_.extend(this.config, { name: 'untitled' });
_.extend(config, { name: 'untitled' });
}

/* Detect production environment */
if (this.config.production === 'auto') {
this.config.production = process.env.NODE_ENV === 'production';
if (config.production === 'auto') {
config.production = process.env.NODE_ENV === 'production';
}

/* Figure out automatic CORS */
if (!('cors' in this.config)) {
this.config.cors = !this.config.production;
if (!('cors' in config)) {
config.cors = !config.production;
}

/* Figure out automatic compression */
if (!('compression' in this.config)) {
this.config.compression = this.config.production;
if (!('compression' in config)) {
config.compression = config.production;
}

console.log('Production mode is', this.config.production);
console.log('Compression is', this.config.compression);
console.log('CORS is', this.config.cors);

/* Set other config based on production */
if (this.config.production === true || this.config.production === 'on') {
if (config.production === true || config.production === 'on') {
/* Check if there's a separate production config */
const prodConfigPath = path.join(this.dir, (this.configFile && this.configFile.replace('.json', `.${process.env.NODE_ENV}.json`)) || `config.${process.env.NODE_ENV}.json`);

if (fs.existsSync(prodConfigPath)) {
/* If we have a config file, let's load it */
const file = fs.readFileSync(prodConfigPath);

this.config = {};
Object.assign(this.config, defaultConfig);
config = {};
Object.assign(config, defaultConfig);

/* Parse and merge the config, or throw an error if it's malformed */
try {
const pc = JSON.parse(file.toString());
_.extend(this.config, pc);
_.extend(config, pc);
} catch (error) {
throw new SaplingError('Error loading production config', error);
}
}

/* Set immutable production vars */
this.config.strict = true;
this.config.showError = false;
config.strict = true;
config.showError = false;
}

return config;
}


/**
* Load the configuration data. Should exist in a file
* called "config.json" and must be valid JSON.
*
* @param {function} next Chain callback
*/
export default async function loadConfig(next) {
/* Digest config */
this.config = digest.call(this);
console.log('CONFIG', this.config);

/* Set the app name */
Expand Down
34 changes: 23 additions & 11 deletions core/loadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ import Templating from '../lib/Templating.js';


/**
* Load the controller JSON file into routes.
* Digest controller.json and/or views directory for the controller
*
* @param {function} next Chain callback
* @returns {object} Controller
*/
export default async function loadController(next) {
/* Load templating engine */
this.templating = new Templating(this);
await this.templating.importDriver();

this.controller = {};
export function digest() {
let controller = {};

/* Generate a controller from the available views */
if ((this.config.autoRouting === 'on' || this.config.autoRouting === true) && this.config.viewsDir !== null) {
Expand Down Expand Up @@ -59,7 +55,7 @@ export default async function loadController(next) {
}

/* Create an automatic GET route for a given view */
this.controller[route] = view.replace(/^\/+/g, '');
controller[route] = view.replace(/^\/+/g, '');
}
}
}
Expand All @@ -82,15 +78,31 @@ export default async function loadController(next) {

/* Merge routes if autorouting, replace routes if not */
if (this.config.autoRouting === 'on' || this.config.autoRouting === true) {
Object.assign(this.controller, routes);
Object.assign(controller, routes);
} else {
this.controller = routes;
controller = routes;
}
} catch (error) {
console.error(`Controller at path: \`${controllerPath}\` could not be loaded.`, error);
}
}

return controller;
}


/**
* Load the controller JSON file into routes.
*
* @param {function} next Chain callback
*/
export default async function loadController(next) {
/* Load templating engine */
this.templating = new Templating(this);
await this.templating.importDriver();

/* Digest controller */
this.controller = digest.call(this, this.config);
console.log('CONTROLLER', this.controller);

if (next) {
Expand Down

0 comments on commit 4b6173e

Please sign in to comment.