<commit_before>var fs = require('fs');
var pem = require('pem');

var http = require('http');
var https = require('https');
var express = require('express');
var mongoose = require('mongoose');

var redis = require('redis');
var async = require('async');

var _ = require('underscore');

var pathToRegex = require('path-to-regexp');

var Maki = function( config ) {
  var self = this;

  self.config = _.extend( require( __dirname + '/../config') , config );
  self.debug = (process.env.NODE_ENV === 'debug') ? true : false;

  self.app = express();
  self.mongoose = mongoose;

  self.app.models      = [];
  self.app.views       = [];
  self.app.controllers = [];
  
  self.routes = {};
  self.clients = {};
  self.resources = {};
  
  self.Database        = require('../lib/Database');
  self.Resource        = require('../lib/Resource');
  self.JSONRPC         = require('../lib/jsonrpc');
  self.Queue           = require('maki-queue');
  self.WebSocketServer = require('maki-service-websockets');

  self.app.engine('jade', require('jade').__express);
  self.app.set('view engine', 'jade');
  self.app.set('views', ['views', 'app/views'] );

  self.app.locals.pretty = true;
  self.app.locals.maki = {
    resources: []
  }
  
  var provide = function() {
    return function( req , res , next ) {
      var v = req.method.toLowerCase();
      
      res.error = function( code , message) {
        if (!message) {
          var message = code;
          var code = 500;
        }
        
        res.format({
          json: function() {
            res.status( code ).send( message );
          },
          html: function() {
            res.render('error', {
              error: message
            })
          }
        });
      }
      
      res.provide = function( name , data , handler ) {
        var resource = self.resources[ name ];
        var method = null;
        
        // METHOD SELECTION
        // of available resources r[], locate matching path p based on regex r
        // select appropriate method m from path p
        // resolves to r_i_m
        if (self.debug) console.log('verbs', resource.verbs);
        Object.keys( resource.verbs[ v ] ).forEach(function( i ) {
          var r = resource.verbs[ v ][ i ];
          var regex = pathToRegex( r.regex );
          
          if (self.debug) console.log('testing ' + req.path , r ,  resource.methods[ r ],  v , resource.verbs[ v ][ r ] );
          
          if (regex.test( req.path )) {
            if (!method) method = r.method;
            return;
          }
        });

        var subject = JSON.parse( JSON.stringify( data ) );

        res.format({
          json: function() {
            
            function clean( obj ) {
              if (!obj) return null;
              
              Object.keys( obj ).forEach(function( k ) {
                if ( resource.options.attributes[ k ] && resource.options.attributes[ k ].restricted ) {
                  delete obj[ k ];
                }
              });
              return obj;
            }
            
            if (data.toObject) {
              var object = clean( data.toObject() );
            } else if (data.map) {
              var object = data.map(function(i) {
                return clean( i );
              });
            } else {
              var object = clean( object );
            }
            
            res.send( object );
          },
          html: function() {
            // exit if there was an override handler
            if (handler) return handler( null , data );
            
            var locals = {};
            locals[ resource.names[ method ] ] = data;
            
            async.map( Object.keys(resource.requires) , function( requirement , done ) {
              if (self.debug) console.log('LE REQUIREMENTS', resource.requires );
              
              if (self.debug) console.log( requirement );
              if (self.debug) console.log( Object.keys( self.resources ) );
              
              //var provider = self.resources[ requirement ].query;
              
              if (self.debug) console.log( provider );
              
              done( null , [] );
            }, function(err, requirements) {
              if (self.debug) console.log('looking for ' + method + ' in ' , resource.templates );
              res.render( resource.templates[ method ] , locals , function( err , body , next ) {
                // internal express succeeded, send it!
                if (body) return res.send(body);

                // it didn't render, let's see if there wasn't a template...
                if (err && err.view && !err.view.path) {
                  
                  if (self.debug) console.log('4. no such view, falling back', err.view , subject );
                  
                  if (subject && !subject.length) {
                    return res.render('resource', {
                      resource: resource,
                      item: subject
                    });
                  } else {
                    return res.render('resource', {
                      resource: resource,
                      collection: subject
                    });
                  }

                }
                
              } );
            } );
          }
        });
      }

      next();
    }
  }
  
  var jademin = require('jade-browser');
  self.app.use( jademin('/js/templates.js', ['views/**', 'app/views/**'], {
    beforeCompile: function( input ) {
      return input.replace(/extends (.*)\n/, '');
    }
  }) );
  
  console.log( __dirname + '/../private');
  
  var lessmin = require('less-middleware');
  self.app.use( lessmin({
    debug: true,
    src: 'private/less',
    dest: 'public/css',
    prefix: '/css'
  }) );
  
  var statics = require('serve-static');
  self.app.use( statics( 'public' ) );

  self.app.use( provide() );
  self.app.use( require('body-parser')() );

  self.passport = require('passport');

  var session = require('express-session');
  var RedisStore = require('connect-redis')( session );

  // set up middlewares for session handling
  self.app.use( require('cookie-parser')( self.config.sessions.secret ) );
  self.app.use( session({
      store: new RedisStore({
        client: self.app.redis
      })
    , secret: self.config.sessions.secret
  }));

  /* Configure the registration and login system */
  self.app.use( self.passport.initialize() );
  self.app.use( self.passport.session() );

  self.app.locals.user = null;
  /* configure some local variables for use later */<commit_msg>Working inferred view folders.<commit_after>var fs = require('fs');
var pem = require('pem');

var http = require('http');
var https = require('https');
var express = require('express');
var mongoose = require('mongoose');

var redis = require('redis');
var async = require('async');

var _ = require('underscore');

var pathToRegex = require('path-to-regexp');

var Maki = function( config ) {
  var self = this;

  self.config = _.extend( require( __dirname + '/../config') , config );
  self.debug = (process.env.NODE_ENV === 'debug') ? true : false;

  self.app = express();
  self.mongoose = mongoose;

  self.app.models      = [];
  self.app.views       = [];
  self.app.controllers = [];
  
  self.routes = {};
  self.clients = {};
  self.resources = {};
  
  self.Database        = require('../lib/Database');
  self.Resource        = require('../lib/Resource');
  self.JSONRPC         = require('../lib/jsonrpc');
  self.Queue           = require('maki-queue');
  self.WebSocketServer = require('maki-service-websockets');

  self.app.engine('jade', require('jade').__express);
  self.app.set('view engine', 'jade');
  self.app.set('views', ['views', 'app/views', __dirname + '/../app/views'] );

  self.app.locals.pretty = true;
  self.app.locals.maki = {
    resources: []
  }
  
  var provide = function() {
    return function( req , res , next ) {
      var v = req.method.toLowerCase();
      
      res.error = function( code , message) {
        if (!message) {
          var message = code;
          var code = 500;
        }
        
        res.format({
          json: function() {
            res.status( code ).send( message );
          },
          html: function() {
            res.render('error', {
              error: message
            })
          }
        });
      }
      
      res.provide = function( name , data , handler ) {
        var resource = self.resources[ name ];
        var method = null;
        
        // METHOD SELECTION
        // of available resources r[], locate matching path p based on regex r
        // select appropriate method m from path p
        // resolves to r_i_m
        if (self.debug) console.log('verbs', resource.verbs);
        Object.keys( resource.verbs[ v ] ).forEach(function( i ) {
          var r = resource.verbs[ v ][ i ];
          var regex = pathToRegex( r.regex );
          
          if (self.debug) console.log('testing ' + req.path , r ,  resource.methods[ r ],  v , resource.verbs[ v ][ r ] );
          
          if (regex.test( req.path )) {
            if (!method) method = r.method;
            return;
          }
        });

        var subject = JSON.parse( JSON.stringify( data ) );

        res.format({
          json: function() {
            
            function clean( obj ) {
              if (!obj) return null;
              
              Object.keys( obj ).forEach(function( k ) {
                if ( resource.options.attributes[ k ] && resource.options.attributes[ k ].restricted ) {
                  delete obj[ k ];
                }
              });
              return obj;
            }
            
            if (data.toObject) {
              var object = clean( data.toObject() );
            } else if (data.map) {
              var object = data.map(function(i) {
                return clean( i );
              });
            } else {
              var object = clean( object );
            }
            
            res.send( object );
          },
          html: function() {
            // exit if there was an override handler
            if (handler) return handler( null , data );
            
            var locals = {};
            locals[ resource.names[ method ] ] = data;
            
            async.map( Object.keys(resource.requires) , function( requirement , done ) {
              if (self.debug) console.log('LE REQUIREMENTS', resource.requires );
              
              if (self.debug) console.log( requirement );
              if (self.debug) console.log( Object.keys( self.resources ) );
              
              //var provider = self.resources[ requirement ].query;
              
              if (self.debug) console.log( provider );
              
              done( null , [] );
            }, function(err, requirements) {
              if (self.debug) console.log('looking for ' + method + ' in ' , resource.templates );
              res.render( resource.templates[ method ] , locals , function( err , body , next ) {
                // internal express succeeded, send it!
                if (body) return res.send(body);

                // it didn't render, let's see if there wasn't a template...
                if (err && err.view && !err.view.path) {
                  
                  if (self.debug) console.log('4. no such view, falling back', err.view , subject );
                  
                  if (subject && !subject.length) {
                    return res.render('resource', {
                      resource: resource,
                      item: subject
                    });
                  } else {
                    return res.render('resource', {
                      resource: resource,
                      collection: subject
                    });
                  }

                }
                
              } );
            } );
          }
        });
      }

      next();
    }
  }
  
  var jademin = require('jade-browser');
  self.app.use( jademin('/js/templates.js', self.app.set('views'), {
    beforeCompile: function( input ) {
      return input.replace(/extends (.*)\n/, '');
    }
  }) );

  var lessmin = require('less-middleware');
  self.app.use( lessmin({
    debug: self.debug,
    src: 'private/less',
    dest: 'public/css',
    prefix: '/css'
  }) );
  
  var statics = require('serve-static');
  self.app.use( statics( 'public' ) );

  self.app.use( provide() );
  self.app.use( require('body-parser')() );

  self.passport = require('passport');

  var session = require('express-session');
  var RedisStore = require('connect-redis')( session );

  // set up middlewares for session handling
  self.app.use( require('cookie-parser')( self.config.sessions.secret ) );
  self.app.use( session({
      store: new RedisStore({
        client: self.app.redis
      })
    , secret: self.config.sessions.secret
  }));

  /* Configure the registration and login system */
  self.app.use( self.passport.initialize() );
  self.app.use( self.passport.session() );

  self.app.locals.user = null;
  /* configure some local variables for use later */
{'fraction_non_alphanumeric': 0.1421535197393347, 'fraction_numerical': 0.0006061983784193377, 'mean_word_length': 1.57270955165692, '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': '1395645', 'n_tokens_mistral': 3720, 'n_tokens_neox': 3617, 'n_words': 885}