-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
130 lines (115 loc) · 3.54 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
/*!
* nodeclub - app.js
*highhome.org [email protected]
*
*/
/**
* Module dependencies.
*/
var fs = require('fs');
var path = require('path');
var Loader = require('loader');
var express = require('express');
var ndir = require('ndir');
var config;
config = require('./config').config;
var passport = require('passport');
var Models = require('./models');
var User = Models.User;
var GitHubStrategy = require('passport-github').Strategy;
var githubStrategyMiddleware = require('./middlewares/github_strategy');
var routes = require('./routes');
var auth = require('./middlewares/auth');
var maxAge = 3600000 * 24 * 30;
var staticDir = path.join(__dirname, 'public');
// assets
var assets = {};
if (config.mini_assets) {
try {
assets = JSON.parse(fs.readFileSync(path.join(__dirname, 'assets.json')));
} catch (e) {
console.log('You must execute `make build` before start app when mini_assets is true.');
throw e;
}
}
// host: http://127.0.0.1
var urlinfo = require('url').parse(config.host);
config.hostname = urlinfo.hostname || config.host;
// 对应的上传信息的内容
config.upload_dir = config.upload_dir || path.join(__dirname, 'public', 'user_data', 'images');
// ensure upload dir exists
ndir.mkdir(config.upload_dir, function (err) {
if (err) {
throw err;
}
});
var app = express.createServer();
// configuration in all env
app.configure(function () {
app.set('view engine', 'html');
app.set('views', path.join(__dirname, 'views'));
app.register('.html', require('ejs'));
app.use(express.bodyParser({
uploadDir: config.upload_dir
}));
app.use(express.cookieParser());
app.use(express.session({
secret: config.session_secret
}));
app.use(passport.initialize());
// custom middleware
app.use(require('./controllers/sign').auth_user);
app.use(auth.blockUser());
app.use('/upload/', express.static(config.upload_dir, { maxAge: maxAge }));
// old image url: http://host/user_data/images/xxxx
app.use('/user_data/', express.static(path.join(__dirname, 'public', 'user_data'), { maxAge: maxAge }));
});
app.configure('development', function () {
app.use('/public', express.static(staticDir));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function () {
app.use(function (req, res, next) {
var csrf = express.csrf();
// ignore upload image
if (req.body && req.body.user_action === 'upload_image') {
return next();
}
csrf(req, res, next);
});
app.use('/public', express.static(staticDir, { maxAge: maxAge }));
app.use(express.errorHandler());
app.set('view cache', true);
});
// set static, dynamic helpers
app.helpers({
config: config,
Loader: Loader,
assets: assets
});
app.dynamicHelpers(require('./common/render_helpers'));
if (process.env.NODE_ENV !== 'test') {
// plugins
var plugins = config.plugins || [];
for (var i = 0, l = plugins.length; i < l; i++) {
var p = plugins[i];
app.use(require('./plugins/' + p.name)(p.options));
}
}
// github oauth
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
passport.use(new GitHubStrategy(config.GITHUB_OAUTH, githubStrategyMiddleware));
// routes
routes(app);
if (process.env.NODE_ENV !== 'test') {
app.listen(config.port);
console.log("highhome listening on port %d in %s mode", config.port, app.settings.env);
console.log("God bless love....");
console.log("You can debug your app with http://" + config.hostname + ':' + config.port);
}
module.exports = app;