Skip to content

Commit

Permalink
Added new parameters to help in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MKHenson committed Mar 26, 2017
1 parent 67a4e63 commit 1122e95
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/definitions/custom/modepress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@
*/
debug: boolean;

/**
* If false, the server config controllers will not be included
*/
includePluginControllers: boolean;

/**
* Settings related to sending emails
*/
Expand Down
5 changes: 5 additions & 0 deletions src/definitions/generated/modepress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ declare namespace Modepress {
*/
debug: boolean;

/**
* If false, the server config controllers will not be included
*/
includePluginControllers: boolean;

/**
* Settings related to sending emails
*/
Expand Down
1 change: 1 addition & 0 deletions src/dist-src/example-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"usersSocketOrigin": "webinate.net",
"usersSocketApiKey": "my-secret-key",
"debug": true,
"includePluginControllers": true,
"adminUser": {
"username": "root",
"email": "[email protected]",
Expand Down
24 changes: 24 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as winston from 'winston';
import * as yargs from 'yargs';

let showLogs: boolean = false;

function assignMeta( meta?: any ) {
if ( meta )
return Object.assign( meta, { process: process.pid } );
Expand All @@ -16,6 +18,12 @@ export function initializeLogger() {

// Add the console colours
winston.addColors( { debug: 'green', info: 'cyan', silly: 'magenta', warn: 'yellow', error: 'red' } );



if ( args.logging === undefined || args.logging === 'true' )
showLogs = true;

winston.remove( winston.transports.Console );
winston.add( winston.transports.Console, <any>{ level: 'debug', colorize: true } );

Expand All @@ -31,6 +39,9 @@ export function initializeLogger() {
*/
export function warn( message: string, meta?: any ) {
return new Promise( function( resolve, reject ) {
if ( !showLogs )
return resolve();

winston.warn( message, assignMeta( meta ), function( err ) {
if ( err )
reject( err )
Expand All @@ -40,13 +51,23 @@ export function warn( message: string, meta?: any ) {
} );
}

/**
* Returns if logging is enabled
*/
export function enabled() {
return showLogs;
}

/**
* Logs an info message
* @param message The message to log
* @param meta Optional meta information to store with the message
*/
export function info( message: string, meta?: any ) {
return new Promise( function( resolve, reject ) {
if ( !showLogs )
return resolve();

winston.info( message, assignMeta( meta ), function( err ) {
if ( err )
reject( err )
Expand All @@ -63,6 +84,9 @@ export function info( message: string, meta?: any ) {
*/
export function error( message: string, meta?: any ) {
return new Promise( function( resolve, reject ) {
if ( !showLogs )
return resolve();

winston.error( message, assignMeta( meta ), function( err ) {
if ( err )
reject( err )
Expand Down
18 changes: 10 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as mongodb from 'mongodb';
import * as http from 'http';
import * as https from 'https';
import * as fs from 'fs';
import { error, info } from './logger';
import { error, info, enabled as loggingEnabled } from './logger';
import * as compression from 'compression';
import { Controller } from './controllers/controller'
import PageRenderer from './controllers/page-renderer'
Expand All @@ -20,7 +20,6 @@ import { AdminController } from './controllers/admin-controller';
import { ErrorController } from './controllers/error-controller';
import { CommsController } from './socket-api/comms-controller';


export class Server {
private _config: Modepress.IConfig;
private _server: Modepress.IServer;
Expand Down Expand Up @@ -53,7 +52,8 @@ export class Server {
app.set( 'view engine', 'jade' );

// log every request to the console
app.use( morgan( 'dev' ) );
if ( loggingEnabled() )
app.use( morgan( 'dev' ) );

// Create each of your controllers here
const controllerPromises: Array<Promise<any>> = [];
Expand All @@ -76,12 +76,14 @@ export class Server {
controllers.push( new StatsController( app, config! ) );


// Load the controllers
// Load the optional controllers
try {
for ( let i = 0, l: number = server.controllers.length; i < l; i++ ) {
lastAddedController = server.controllers[ i ].path;
const func = require( server.controllers[ i ].path );
controllers.push( new func.default( server, config, app ) );
if ( config.includePluginControllers ) {
for ( let i = 0, l: number = server.controllers.length; i < l; i++ ) {
lastAddedController = server.controllers[ i ].path;
const func = require( server.controllers[ i ].path );
controllers.push( new func.default( server, config, app ) );
}
}
}
catch ( err ) {
Expand Down

0 comments on commit 1122e95

Please sign in to comment.