forked from lukeddy/tuer.me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (104 loc) · 2.72 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
/**
* Module dependencies.
*/
var express = require('express'),
config = require('./lib/config'),
rootdir = config.rootdir,
RedisStore = require('connect-redis')(express);
var myOAP = require('./lib/OAP');
var toobusy = require('toobusy');
var app = express.createServer();
var wap = express.createServer();
// Configuration
function Configuration(app, rootdir) {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', {
layout: false
});
app.use(express.bodyParser({
uploadDir: rootdir + '/public/images/'
}));
app.use(express.cookieParser());
app.use(express.query());
app.use(express.session({
secret: 'keyboard cat',
store: new RedisStore
}));
app.use(express.methodOverride());
app.use(express['static'](__dirname + '/public'));
app.use(express.favicon(__dirname + '/public/favicon.ico'), {
maxAge: 2592000000
});
app.use(function(req,res,next){
res.header("Access-Control-Allow-Origin","*");
res.header("Access-Control-Allow-Headers","X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
if(toobusy()){
res.send(503,"兔耳开小差了,请稍后访问,或手动刷新");
}else{
next();
}
});
app.use(myOAP.oauth());
app.use(myOAP.login());
app.use(function(req, res, next) {
//判断ie版本
var sys = {};
var s;
var ua = req.headers['user-agent'].toLowerCase();
var host = req.headers['host'];
if (!((/^m.tuer.me/).test(host)) && (s = ua.match(/msie ([\d.]+)/)) && parseInt(s[1], 10) <= 7) {
res.render('custom/ie',{version:s[1]});
} else {
next();
}
});
app.use(app.router);
}
function development(app) {
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
}
function production(app) {
app.use(express.errorHandler());
}
exports.start = function(conf) {
if (conf) {
for (var i in conf) {
if (config.hasOwnProperty(i)) config[i] = conf[i];
}
}
app.configure(function() {
Configuration(app, config.rootdir);
});
app.configure('development', function() {
development(app);
});
app.configure('production', function() {
production(app);
});
wap.configure(function() {
Configuration(wap, config.rootdir);
});
wap.configure('development', function() {
development(wap);
});
wap.configure('production', function() {
production(wap);
});
//controllers
require('./routes')(app);
require('./wapRoutes')(wap);
server = app.listen(config.port);
wap.listen(config.mport);
process.on('SIGINT',function(){
server.close();
toobusy.shutdown();
process.exit();
});
console.log('app server on ' + config.host);
console.log('wap server on ' + config.mhost);
};