<commit_before>/** @author <NAME> */

var co = require('co');
var fs = require('fs');
var path = require('path');
var _ = require('underscore');
var app = require('./app');
var verboseLog = require('./app/util').verboseLog;
var Users = require('./model/admin/user');
var Course = require('./model/course/course');
var routes = require('./routes');
var mongoose = require('mongoose');
var restify = require('express-restify-mongoose');

/* Mount login REST endpoints */
app.use('/api/', routes.login);
app.use('/api/', routes["user"]);

mountModel();

/**
 * This is a hack so that if connects from an external site to path managed by
 * the client side history API the page still works.
 */
app.use('/', function(req, res, next){
   var sub = req.url.toString().substr(0, 5);
   if (sub !== '/api/' && req.accepts(['html', 'application/json']) === 'html')
      res.sendFile(__dirname + '/public/index.html');
   else{
      next();
   }
});

//console.log(mkCourse());

co(function *() {
   try {
      var admin = yield Users.findOne({username: 'admin'}).exec();
      if (!admin) {
         console.warn('Creating admin user');
         yield Users.create({username: 'admin', password: '<PASSWORD>'});
      }
      admin = yield Users.findOne({username: 'admin'}).exec();
      yield Course.remove().exec();
      var num = yield Course.count().exec();
      if (num < 1) {
         _.map(_.range(25), function() {
            return Course.makeRandomCourse(admin);
         }).forEach(function(c){
            console.dir(c);
            Course.create(c, function(err, c) {
               console.dir(err)
            })
         });
      }
   } catch (err) {
      console.err(err);
      console.err(err.stack);
   }
   app.ready();
});


function mountModel(){
   function scanModelFiles(dirs){
      var dirs = _.isArray(dirs)? dirs: [dirs];
      var files = [];

      function scanDir(dirname){
         if(!dirname ||   dirname.indexOf("node_modules")>=0) return;
         verboseLog("Scanning directory for modules: ", dirname);
         if(fs.existsSync(path.resolve(dirname, 'index.js'))){
            files.push(path.resolve(dirname, 'index.js') );
            return;
         }
         fs.readdirSync( dirname  ).forEach(function(file) {
            var fullpath = path.resolve(dirname,file);
            if(! fs.statSync(fullpath).isFile()) scanDir( fullpath );
            else if (file.match(/.+\.js/g) !== null) {
               files.push(fullpath);
            }
         });
      }
      dirs.forEach(function(dirname){
         scanDir(dirname);
      });
      return files;
   }

   var files = scanModelFiles(path.resolve('./model'));
   files.forEach(function(file) {
      try{
         if(typeof(file === 'string')){
            verboseLog("Loading module file", file);
            require(file);
         }
      }
      catch(ex){
         console.warn("Skipping file ", file, " due to error: ", ex);
      }
   });
   verboseLog("Adding all mongoose models");
   _.each(mongoose.modelNames(), function(modelName){
      var model = mongoose.models[modelName];
      var options = _.extend({
         version: ''
      }, model.getRestOptions ? model.getRestOptions(): {});
      if (modelName === "User") {

      }
      restify.serve(app, model, options);
   });
};<commit_msg>made an ugly line look better<commit_after>/** @author <NAME> */

var co = require('co');
var fs = require('fs');
var path = require('path');
var _ = require('underscore');
var app = require('./app');
var verboseLog = require('./app/util').verboseLog;
var Users = require('./model/admin/user');
var Course = require('./model/course/course');
var routes = require('./routes');
var mongoose = require('mongoose');
var restify = require('express-restify-mongoose');

/* Mount login REST endpoints */
app.use('/api/', routes.login);
app.use('/api/', routes["user"]);

mountModel();

/**
 * This is a hack so that if connects from an external site to path managed by
 * the client side history API the page still works.
 */
app.use('/', function(req, res, next){
   var sub = req.url.toString().substr(0, 5);
   if (sub !== '/api/' && req.accepts(['html', 'application/json']) === 'html')
      res.sendFile(__dirname + '/public/index.html');
   else{
      next();
   }
});

//console.log(mkCourse());

co(function *() {
   try {
      var admin = yield Users.findOne({username: 'admin'}).exec();
      if (!admin) {
         console.warn('Creating admin user');
         yield Users.create({username: 'admin', password: '<PASSWORD>'});
      }
      admin = yield Users.findOne({username: 'admin'}).exec();
      yield Course.remove().exec();
      var num = yield Course.count().exec();
      if (num < 1) {
         _.map(_.range(25), function() {
            return Course.makeRandomCourse(admin);
         }).forEach(function(c){
            Course.create(c, function(err, c) { if (err) throw err; });
         });
      }
   } catch (err) {
      console.err(err);
      console.err(err.stack);
   }
   app.ready();
});


function mountModel(){
   function scanModelFiles(dirs){
      var dirs = _.isArray(dirs)? dirs: [dirs];
      var files = [];

      function scanDir(dirname){
         if(!dirname ||   dirname.indexOf("node_modules")>=0) return;
         verboseLog("Scanning directory for modules: ", dirname);
         if(fs.existsSync(path.resolve(dirname, 'index.js'))){
            files.push(path.resolve(dirname, 'index.js') );
            return;
         }
         fs.readdirSync( dirname  ).forEach(function(file) {
            var fullpath = path.resolve(dirname,file);
            if(! fs.statSync(fullpath).isFile()) scanDir( fullpath );
            else if (file.match(/.+\.js/g) !== null) {
               files.push(fullpath);
            }
         });
      }
      dirs.forEach(function(dirname){
         scanDir(dirname);
      });
      return files;
   }

   var files = scanModelFiles(path.resolve('./model'));
   files.forEach(function(file) {
      try{
         if(typeof(file === 'string')){
            verboseLog("Loading module file", file);
            require(file);
         }
      }
      catch(ex){
         console.warn("Skipping file ", file, " due to error: ", ex);
      }
   });
   verboseLog("Adding all mongoose models");
   _.each(mongoose.modelNames(), function(modelName){
      var model = mongoose.models[modelName];
      var options = _.extend({
         version: ''
      }, model.getRestOptions ? model.getRestOptions(): {});
      if (modelName === "User") {

      }
      restify.serve(app, model, options);
   });
};
{'fraction_non_alphanumeric': 0.17860930162096653, 'fraction_numerical': 0.0018179063778215423, 'mean_word_length': 2.4986751457339693, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3062472', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1962, 'n_words': 445}