<commit_before>'use strict';

var express = require('express'),
    cookieParser = require('cookie-parser'),
    bodyParser = require('body-parser'),
    engines = require('consolidate'),
    session = require('express-session'),
    async = require('async'),
    passport = require('passport'),
    GoogleStrategy = require('passport-google-oauth2').Strategy,

    MongoClient = require('mongodb').MongoClient,
    ObjectID = require('mongodb').ObjectID,

    PetController = require('./PetController'),
    UserController = require('./UserController'),
    hostname = process.env.HOST || 'localhost'; // TODO: Update to OAuth2

var Server = function(opts) {
    opts = opts || {};
    this.port = opts.port || 8888;
    this.mongoURI = process.env.MONGO_URI || 'mongodb://localhost:27017/homeward-bound';

    // Configure Models
    this.models = {
        pet: null,
        user: null
    };

    this.configureModels(function() {
        // Configure Controllers
        this.controllers = {
            pet: new PetController(this.models.user, this.models.pet),
            user: new UserController(this.models.user, this.models.pet)
        };

        // Configure authentication
        this.configureAuthentication();

        // Configure app endpoints
        this.app = express();
        this.configureEndpoints();

        // If the server has already been started
        if (this.onStart !== null) {
            this.start(this.onStart);
        }
    }.bind(this));
};

Server.prototype.configureAuthentication = function() {
    passport.serializeUser(function(user, done) {
        done(null, user._id);
    });
    passport.deserializeUser(function(id, done) {
        this.models.user.findOne({_id: ObjectID(id)}, function(err, user) {
            done(err, user);
        });
    }.bind(this));

    passport.use(new GoogleStrategy({
        clientID: process.env.CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET,
        returnURL: '/auth/google/return',
        passReqToCallback: true
    }, function(request, accessToken, profile, done) {
        // Find the user or create the user
        this.models.user.findOne({googleId: profile.id}, /*{limit: 1},*/ function(err, user) {
            if (!user) {
                this.models.user.insert({googleId: profile.id, 
                                         email: profile.emails[0].value},  // First email
                                         function(err, res) {
                    done(err, res);
                }.bind(this));<commit_msg>Changed returnURL to absolute path<commit_after>'use strict';

var express = require('express'),
    cookieParser = require('cookie-parser'),
    bodyParser = require('body-parser'),
    engines = require('consolidate'),
    session = require('express-session'),
    async = require('async'),
    passport = require('passport'),
    GoogleStrategy = require('passport-google-oauth2').Strategy,

    MongoClient = require('mongodb').MongoClient,
    ObjectID = require('mongodb').ObjectID,

    PetController = require('./PetController'),
    UserController = require('./UserController'),
    hostname = process.env.HOST || 'localhost'; // TODO: Update to OAuth2

var Server = function(opts) {
    opts = opts || {};
    this.port = opts.port || 8888;
    this.mongoURI = process.env.MONGO_URI || 'mongodb://localhost:27017/homeward-bound';

    // Configure Models
    this.models = {
        pet: null,
        user: null
    };

    this.configureModels(function() {
        // Configure Controllers
        this.controllers = {
            pet: new PetController(this.models.user, this.models.pet),
            user: new UserController(this.models.user, this.models.pet)
        };

        // Configure authentication
        this.configureAuthentication();

        // Configure app endpoints
        this.app = express();
        this.configureEndpoints();

        // If the server has already been started
        if (this.onStart !== null) {
            this.start(this.onStart);
        }
    }.bind(this));
};

Server.prototype.configureAuthentication = function() {
    passport.serializeUser(function(user, done) {
        done(null, user._id);
    });
    passport.deserializeUser(function(id, done) {
        this.models.user.findOne({_id: ObjectID(id)}, function(err, user) {
            done(err, user);
        });
    }.bind(this));

    passport.use(new GoogleStrategy({
        clientID: process.env.CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET,
        returnURL: 'http://'+hostname+':'+this.port+'/auth/google/return',
        passReqToCallback: true
    }, function(request, accessToken, profile, done) {
        // Find the user or create the user
        this.models.user.findOne({googleId: profile.id}, /*{limit: 1},*/ function(err, user) {
            if (!user) {
                this.models.user.insert({googleId: profile.id, 
                                         email: profile.emails[0].value},  // First email
                                         function(err, res) {
                    done(err, res);
                }.bind(this));
{'fraction_non_alphanumeric': 0.13876994939665238, 'fraction_numerical': 0.00506033476060724, 'mean_word_length': 2.3283678756476682, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3997306', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1400, 'n_words': 283}