-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
154 lines (120 loc) · 3.71 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Module dependencies.
*/
var express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
http = require('http'),
path = require('path');
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
GoogleStrategy = require('passport-google').Strategy;
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
//sessions
app.use(express.cookieParser());
app.use(express.session({secret: 'slap4my5name'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
var appDomain = "nameslap.com";
// development only
if ('development' == app.get('env')) {
appDomain = "localhost:3000";
app.use(express.errorHandler());
}
function removeSubdomain(hostname){
var hostnameArr = hostname.split('.');
hostnameArr.shift();
return hostnameArr.join('.');
}
require("./User.js");
User = new User();
require("./DomainSearcher.js");
domainSearcher = new DomainSearcher();
domainSearcher.testConnection();
app.get('/*', function(req, res, next) {
if (req.headers.host.match(/^www/) !== null )
res.redirect('http://' + removeSubdomain(req.headers.host) + req.url, 301);
else next();
});
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use(new GoogleStrategy({
returnURL: 'http://'+appDomain+'/auth/google/return',
realm: 'http://'+appDomain+'/'
},
function(identifier, profile, done) {
User.findOrCreate({ openId: identifier, userInfo: profile, provider: "google" }, function(err, user) {
done(err, user);
});
}
));
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function(err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}
));
app.get('/auth/google', passport.authenticate('google'));
app.get('/auth/google/return',
passport.authenticate('google', { successRedirect: '/',
failureRedirect: '/login' }));
app.get('/', function(req, res){
res.render('random_gen',{});
});
app.post('/randomGen', function(req, res){
var suggestion = req.body.suggestion;
var callbackFunc = function(results){
res.send(JSON.stringify(results));
};
if(suggestion === undefined)
domainSearcher.generateAndTestWords(callbackFunc);
else
domainSearcher.generateAndTestWords(callbackFunc, suggestion);
});
app.get('/listWords:limit?', function(req, res){
var limit = req.params.limit;
if (!limit)
limit = 100;
domainSearcher.allWords(limit, function(rows){
res.render('all_words',{
words: rows
});
});
});
app.get('/login', function(req, res){
res.render('login');
});
app.post('/login',
passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login',
failureFlash: true })
);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});