<commit_before>'use strict';

let config;

import express from 'express';
import * as routes from './routes';
import session from 'express-session';
//favicon = require('serve-favicon')
import path from 'path';
import passport from 'passport';
import FacebookAuth from './FacebookAuth';
import usersRouter from './routes/users';
import notepadsRouter from './routes/notepads';
import categoriesRouter from './routes/categories';
import HttpStatus from 'http-status';
import User from './models/user';

let app = express();

let envs = ['development', 'production', 'test'];

if (envs.indexOf(app.get('env')) !== -1) {
    console.info(app.get('env') + ' environment detected');
} else {
    console.warn('unrecognized environment detected', app.get('env'));
}

try {
    config = require('../config/app.conf.json');
} catch (err) {
    if (app.get('env') !== 'test') {
        //prod / dev env - cannot work without real config values
        console.error('No configuration file!');
        process.stderr.write(err);
        process.exit();
    } else {
        //test env
        console.error(err);
        config = require('../config/testing.json');
    }
}

app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//app.use(favicon(__dirname + '/public/favicon.ico'));
if (config) {
    app.use(session({
        //TODO: check these properties for their default values
        resave: true,
        saveUninitialized: true,
        //TODO: use the app.conf.js config file for this
        secret: config.sessionSecret
    }));
    FacebookAuth.setAppConfig(config);
    FacebookAuth.call(null, passport);
    app.use(passport.initialize());
    app.use(passport.session());
}
//get all data sent to the API inside req.body
app.use(require('body-parser').json());
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', routes.index);
app.get('/auth/facebook',
    passport.authenticate('facebook', {}),
    () => {}
);
app.get('/auth/facebook/callback',
    passport.authenticate('facebook', {
        failureRedirect: '/'
    }),
    FacebookAuth.login
);
app.get('/logout', FacebookAuth.logout);

//API URLs
//allow all locations access to the api urls
if (config) {
    app.use(config.apiBase, (req, res, next) => {
        // Website you wish to allow to connect
        res.setHeader('Access-Control-Allow-Origin', '*');
        // Request methods you wish to allow
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
        // Request headers you wish to allow
        res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
        // Pass to next layer of middleware
        next();
    });
}

//authorize if valid access token is given
//populate req.user from a valid access token
let parseAccessToken = (req, res, next) => {
    let blockUser = err => {
        console.error(err);
        //this may return UNAUTHORIZED instead:
        return res.status(HttpStatus.FORBIDDEN).json({});
    };

    let accessToken = req.query.token;

    if (accessToken) {
        //this require is used often, may be put on top:
        User.getByAccessToken(accessToken).then(user => {
            if (user) {
                req.user = user;
                req.user.id = user._id;
                next();
            } else {
                return blockUser('Invalid access token!');
            }
        }).catch(err => {
            return blockUser(err);
        });
    } else {
        return next();
    }
};

app.use(parseAccessToken);

//API routes
if (config) {
    usersRouter.setAppConfig(config);
    app.use(config.apiBase + '/users', usersRouter);
    app.use(config.apiBase + '/notepads', notepadsRouter);
    app.use(config.apiBase + '/categories', categoriesRouter);
}

//dev env
if ('development' === app.get('env')) {
    //app.use(express.logger('dev')); TODO: check exp4 way
    let errorHandler = require('errorhandler');
    app.use(errorHandler());
}

let createConnection = () => {
    let connection = require('./db');
    connection.setAppConfig(config);
    connection().on('connected', err => {
        if (err) {
            process.stderr.write(err);
            process.exit();
        }
    });
};

let startServer = () => {
    app.listen(app.get('port'), () => {
        console.info('Express server listening on port ' + app.get('port'));
    });
};

module.exports = exports = app;
module.exports.createConnection = exports.createConnection = createConnection;
module.exports.startServer = exports.startServer = startServer;
module.exports.parseAccessToken = exports.parseAccessToken = parseAccessToken;
<commit_msg>added morgan logger for the dev environment<commit_after>'use strict';

let config;

import express from 'express';
import * as routes from './routes';
import session from 'express-session';
//favicon = require('serve-favicon')
import path from 'path';
import passport from 'passport';
import FacebookAuth from './FacebookAuth';
import usersRouter from './routes/users';
import notepadsRouter from './routes/notepads';
import categoriesRouter from './routes/categories';
import HttpStatus from 'http-status';
import User from './models/user';

let app = express();

let envs = ['development', 'production', 'test'];

if (envs.indexOf(app.get('env')) !== -1) {
    console.info(app.get('env') + ' environment detected');
} else {
    console.warn('unrecognized environment detected', app.get('env'));
}

try {
    config = require('../config/app.conf.json');
} catch (err) {
    if (app.get('env') !== 'test') {
        //prod / dev env - cannot work without real config values
        console.error('No configuration file!');
        process.stderr.write(err);
        process.exit();
    } else {
        //test env
        console.error(err);
        config = require('../config/testing.json');
    }
}

app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//app.use(favicon(__dirname + '/public/favicon.ico'));
if (config) {
    app.use(session({
        //TODO: check these properties for their default values
        resave: true,
        saveUninitialized: true,
        //TODO: use the app.conf.js config file for this
        secret: config.sessionSecret
    }));
    FacebookAuth.setAppConfig(config);
    FacebookAuth.call(null, passport);
    app.use(passport.initialize());
    app.use(passport.session());
}
//get all data sent to the API inside req.body
app.use(require('body-parser').json());
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', routes.index);
app.get('/auth/facebook',
    passport.authenticate('facebook', {}),
    () => {}
);
app.get('/auth/facebook/callback',
    passport.authenticate('facebook', {
        failureRedirect: '/'
    }),
    FacebookAuth.login
);
app.get('/logout', FacebookAuth.logout);

//API URLs
//allow all locations access to the api urls
if (config) {
    app.use(config.apiBase, (req, res, next) => {
        // Website you wish to allow to connect
        res.setHeader('Access-Control-Allow-Origin', '*');
        // Request methods you wish to allow
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
        // Request headers you wish to allow
        res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
        // Pass to next layer of middleware
        next();
    });
}

//authorize if valid access token is given
//populate req.user from a valid access token
let parseAccessToken = (req, res, next) => {
    let blockUser = err => {
        console.error(err);
        //this may return UNAUTHORIZED instead:
        return res.status(HttpStatus.FORBIDDEN).json({});
    };

    let accessToken = req.query.token;

    if (accessToken) {
        //this require is used often, may be put on top:
        User.getByAccessToken(accessToken).then(user => {
            if (user) {
                req.user = user;
                req.user.id = user._id;
                next();
            } else {
                return blockUser('Invalid access token!');
            }
        }).catch(err => {
            return blockUser(err);
        });
    } else {
        return next();
    }
};

app.use(parseAccessToken);

//API routes
if (config) {
    usersRouter.setAppConfig(config);
    app.use(config.apiBase + '/users', usersRouter);
    app.use(config.apiBase + '/notepads', notepadsRouter);
    app.use(config.apiBase + '/categories', categoriesRouter);
}

//dev env
if ('development' === app.get('env')) {
    app.use(require('morgan')('combined'));
    app.use(require('errorhandler')());
}

let createConnection = () => {
    let connection = require('./db');
    connection.setAppConfig(config);
    connection().on('connected', err => {
        if (err) {
            process.stderr.write(err);
            process.exit();
        }
    });
};

let startServer = () => {
    app.listen(app.get('port'), () => {
        console.info('Express server listening on port ' + app.get('port'));
    });
};

module.exports = exports = app;
module.exports.createConnection = exports.createConnection = createConnection;
module.exports.startServer = exports.startServer = startServer;
module.exports.parseAccessToken = exports.parseAccessToken = parseAccessToken;

{'fraction_non_alphanumeric': 0.1598037123959889, 'fraction_numerical': 0.001173458502240239, 'mean_word_length': 2.8997504159733776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5784598', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2729, 'n_words': 675}