<commit_before>// New Relic Server monitoring support
if ( process.env.NEW_RELIC_ENABLED ) {
  require( "newrelic" );
}

/**
 * Module dependencies.
 */
var ajax = require('request'),
    async = require('async'),
    bleach = require( "./lib/bleach"),
    db = require('./lib/database'),
    express = require('express'),
    fs = require('fs'),
    habitat = require('habitat'),
    helmet = require("helmet"),
    lessMiddleWare = require("less-middleware"),
    makeAPI = require('./lib/makeapi'),
    nunjucks = require('nunjucks'),
    path = require('path'),
    routes = require('./routes'),
    utils = require('./lib/utils'),
    version = require('./package').version;


habitat.load();

var appName = "thimble",
    app = express(),
    env = new habitat(),
    node_env = env.get('NODE_ENV'),
    WWW_ROOT = path.resolve(__dirname, 'public'),
    /**
      We're using two databases here: the first is our normal database, the second is
      a legacy database with old the original thimble.webmaker.org data from 2012/2013
      prior to the webmaker.org reboot. This database is a read-only database, with
      remixes/edits being published to the new database instead. This is intended as
      a short-term solution until all the active "old thimble" projects have been
      migrated by their owners/remixers.
    **/
    databaseOptions =  env.get('CLEARDB_DATABASE_URL') || env.get('DB'),
    databaseAPI = db('thimbleproject', databaseOptions),
    legacyDatabaseAPI = db('legacyproject', databaseOptions, env.get('LEGACY_DB')),

    middleware = require('./lib/middleware')(env),
    make = makeAPI(env.get('make')),
    nunjucksEnv = new nunjucks.Environment(new nunjucks.FileSystemLoader('views'), {
      autoescape: true
    }),
    parameters = require('./lib/parameters');

nunjucksEnv.express(app);
app.locals({
  GA_ACCOUNT: env.get("GA_ACCOUNT"),
  GA_DOMAIN: env.get("GA_DOMAIN")
});

// Express settings
app.use(express.favicon());
app.use(express.logger("dev"));
if (!!env.get("FORCE_SSL") ) {
  app.use(helmet.hsts());
  app.enable("trust proxy");
}
app.use(express.compress());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.cookieSession({
  key: "thimble.sid",
  secret: env.get("SESSION_SECRET"),
  cookie: {
    maxAge: 2678400000, // 31 days. Persona saves session data for 1 month
    secure: !!env.get("FORCE_SSL")
  },
  proxy: true
}));
app.use(express.csrf());
app.use(helmet.xframe());
app.use(app.router);

var optimize = (node_env !== "development"),
    tmpDir = path.join( require("os").tmpDir(), "mozilla.webmaker.org");

app.use(lessMiddleWare({
  once: true,
  debug: !optimize,
  dest: tmpDir,
  src: WWW_ROOT,
  compress: true,
  yuicompress: optimize,
  optimization: optimize ? 0 : 2
}));

app.use( express.static(tmpDir));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'learning_projects')));
app.use(express.static(path.join(__dirname, 'templates')));

app.use( function(err, req, res, next) {
  res.send( 500, err );
});

// what do we do when a project request comes in by id (:id route)?
app.param('id', parameters.id(databaseAPI));

// what do we do when a project request comes in by id (:oldid route)?
app.param('oldid', parameters.oldid(legacyDatabaseAPI));

// what do we do when a project request comes in by name (:name route)?
app.param('name', parameters.name);

// Main page
app.get('/',
        middleware.setNewPageOperation,
        routes.index(utils, env, appName));

// Legacy route for the main page
app.get('/en-US/?',
        middleware.setNewPageOperation,
        routes.index(utils, env, appName));

// Legacy route for the main page
app.get('/en-US/editor',<commit_msg>Fix bug 892999 - Add console logging<commit_after>// New Relic Server monitoring support
if ( process.env.NEW_RELIC_ENABLED ) {
  require( "newrelic" );
}

/**
 * Module dependencies.
 */
var ajax = require('request'),
    async = require('async'),
    bleach = require( "./lib/bleach"),
    db = require('./lib/database'),
    express = require('express'),
    fs = require('fs'),
    habitat = require('habitat'),
    helmet = require("helmet"),
    lessMiddleWare = require("less-middleware"),
    makeAPI = require('./lib/makeapi'),
    nunjucks = require('nunjucks'),
    path = require('path'),
    routes = require('./routes'),
    utils = require('./lib/utils'),
    version = require('./package').version;


habitat.load();

var appName = "thimble",
    app = express(),
    env = new habitat(),
    node_env = env.get('NODE_ENV'),
    WWW_ROOT = path.resolve(__dirname, 'public'),
    /**
      We're using two databases here: the first is our normal database, the second is
      a legacy database with old the original thimble.webmaker.org data from 2012/2013
      prior to the webmaker.org reboot. This database is a read-only database, with
      remixes/edits being published to the new database instead. This is intended as
      a short-term solution until all the active "old thimble" projects have been
      migrated by their owners/remixers.
    **/
    databaseOptions =  env.get('CLEARDB_DATABASE_URL') || env.get('DB'),
    databaseAPI = db('thimbleproject', databaseOptions),
    legacyDatabaseAPI = db('legacyproject', databaseOptions, env.get('LEGACY_DB')),

    middleware = require('./lib/middleware')(env),
    make = makeAPI(env.get('make')),
    nunjucksEnv = new nunjucks.Environment(new nunjucks.FileSystemLoader('views'), {
      autoescape: true
    }),
    parameters = require('./lib/parameters');

nunjucksEnv.express(app);
app.locals({
  GA_ACCOUNT: env.get("GA_ACCOUNT"),
  GA_DOMAIN: env.get("GA_DOMAIN")
});

// Express settings
app.use(express.favicon());
app.use(express.logger("dev"));
if (!!env.get("FORCE_SSL") ) {
  app.use(helmet.hsts());
  app.enable("trust proxy");
}
app.use(express.compress());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.cookieSession({
  key: "thimble.sid",
  secret: env.get("SESSION_SECRET"),
  cookie: {
    maxAge: 2678400000, // 31 days. Persona saves session data for 1 month
    secure: !!env.get("FORCE_SSL")
  },
  proxy: true
}));
app.use(express.csrf());
app.use(helmet.xframe());
app.use(app.router);

var optimize = (node_env !== "development"),
    tmpDir = path.join( require("os").tmpDir(), "mozilla.webmaker.org");

app.use(lessMiddleWare({
  once: true,
  debug: !optimize,
  dest: tmpDir,
  src: WWW_ROOT,
  compress: true,
  yuicompress: optimize,
  optimization: optimize ? 0 : 2
}));

app.use( express.static(tmpDir));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'learning_projects')));
app.use(express.static(path.join(__dirname, 'templates')));

app.use( function(err, req, res, next) {
  console.error(err);
  res.send( 500, err );
});

// what do we do when a project request comes in by id (:id route)?
app.param('id', parameters.id(databaseAPI));

// what do we do when a project request comes in by id (:oldid route)?
app.param('oldid', parameters.oldid(legacyDatabaseAPI));

// what do we do when a project request comes in by name (:name route)?
app.param('name', parameters.name);

// Main page
app.get('/',
        middleware.setNewPageOperation,
        routes.index(utils, env, appName));

// Legacy route for the main page
app.get('/en-US/?',
        middleware.setNewPageOperation,
        routes.index(utils, env, appName));

// Legacy route for the main page
app.get('/en-US/editor',
{'fraction_non_alphanumeric': 0.16288331342094783, 'fraction_numerical': 0.007699455728129564, 'mean_word_length': 3.950065703022339, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5035028', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2426, 'n_words': 607}