-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
122 lines (104 loc) · 3.49 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
"use strict";
var config = require("./config/config");
var express = require("express");
var path = require("path");
var favicon = require("serve-favicon");
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
//var logger = require("morgan");
//app.use(logger("dev"));
var routes = require("./routes/index");
var sessionMiddleware = {};
if( config.api.active ) {
var User = require('./modules/users/modelUser');
var jf = require('jsonfile');
var accounts = jf.readFileSync( __dirname + "/config/account.json");
var session = require("express-session");
var TwitterStrategy = require("passport-twitter").Strategy;
var methodOverride = require("method-override");
var passport = require("passport");
var mongoConnector = require("./modules/mongo/mongoConnector.js");
var MongoStore = require("connect-mongo")(session);
var auth = require("./routes/auth");
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 TwitterStrategy({
consumerKey: accounts.TWITTER_CONSUMER_KEY,
consumerSecret: accounts.TWITTER_CONSUMER_SECRET,
callbackURL: "http://" + config.webServer.apiAddress + ":" + config.webServer.apiPort + "/auth/twitter/callback"
},
function( token, tokenSecret, profile, done ){
process.nextTick(function(){
User.findOne({ "twitter.id": profile.id}, function(err, user){
if(!user){
User.create({
twitter:{
id: profile.id,
username: profile.username,
token: token,
tokenSecret: tokenSecret,
photo: {
url: profile.photos[0].value
}
}
},function(err, user){
done(err, user);
});
} else{
done(err, user);
}
});
});
}
));
sessionMiddleware = session({
secret: "keyboard cat",
name: "token",
proxy: true,
resave: true,
store: new MongoStore({
mongooseConnection: mongoConnector.getConnection(),
stringify: false
}),
saveUninitialized: false,
cookie: {secure: false}
});
}
var app = express();
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(favicon(__dirname + "/public/images/favicon.ico"));
//routes
if( config.webServer.active ){
app.use(express.static(path.join(__dirname, "public")));
}
if( config.api.active ){
app.use(bodyParser.json());
app.use(methodOverride());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(sessionMiddleware);
app.use(passport.initialize());
app.use(passport.session());
app.use("/auth", auth.router);
}
//routes
if( config.webServer.active ){
app.use("/", routes);
}
// catch 404
app.use(function( req, res ){
console.log( "Not Found url: " + req.originalUrl );
res.status( 404 );
res.send("File not found!");
});
module.exports = {
app: app,
sessionMiddleware : sessionMiddleware
};