-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
140 lines (114 loc) · 3.65 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
'use strict';
//dependencies
var config = require('./config'),
express = require('express'),
mongoStore = require('connect-mongo')(express),
http = require('http'),
path = require('path'),
passport = require('passport'),
mongoose = require('mongoose'),
helmet = require('helmet'),
request = require("request"),
cheerio = require("cheerio"),
crawler = require("simplecrawler");
//create express app
var app = express();
//keep reference to config
app.config = config;
//setup the web server
app.server = http.createServer(app);
//use MongoDB
if(config.mongodb.uri != '') {
//setup mongoose
app.db = mongoose.createConnection(config.mongodb.uri);
app.db.on('error', console.error.bind(console, 'mongoose connection error: '));
app.db.once('open', function () {
//and... we have a data store
});
//config data models
require('./models')(app, mongoose);
//setup the session store
app.sessionStore = new mongoStore({ url: config.mongodb.uri });
}
//config express in all environments
app.configure(function(){
//settings
app.disable('x-powered-by');
app.set('port', config.port);
app.set('views', __dirname + '/api');
app.set('view engine', 'jade');
app.set('strict routing', true);
app.set('project-name', config.projectName);
app.set('company-name', config.companyName);
app.set('system-email', config.systemEmail);
app.set('crypto-key', config.cryptoKey);
app.set('require-account-verification', config.requireAccountVerification);
//smtp settings
app.set('smtp-from-name', config.smtp.from.name);
app.set('smtp-from-address', config.smtp.from.address);
app.set('smtp-credentials', config.smtp.credentials);
//middleware
app.use(express.logger('dev'));
app.use(express.compress());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.urlencoded());
app.use(express.json());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
secret: config.cryptoKey,
store: app.sessionStore
}));
app.use(passport.initialize());
app.use(passport.session());
helmet.defaults(app);
//response locals
app.use(function(req, res, next) {
res.locals.user = {};
res.locals.user.defaultReturnUrl = req.user && req.user.defaultReturnUrl();
res.locals.user.username = req.user && req.user.username;
next();
});
// makeeapi response locals
app.use(function(req, res, next) {
res.locals.currentURL = function(path) {
if (path !== '') path = '/' + path;
return (req._parsedUrl.path + path).replace('//', '/');
};
res.locals.now = function() {
return new Date().now;
};
res.locals.user = req.user;
next();
});
// makeeapi configs
app.use(function(req, res, next) {
res.locals.cdnServer = '';
next();
});
//mount the routes
app.use(app.router);
//error handler
app.use(require('./api/http/index').http500);
//global locals
app.locals.projectName = app.get('project-name');
app.locals.copyrightYear = new Date().getFullYear();
app.locals.copyrightName = app.get('company-name');
app.locals.cacheBreaker = 'br34k-01';
});
//config express in dev environment
app.configure('development', function(){
app.use(express.errorHandler());
});
//route requests
require('./routes')(app, passport);
//setup utilities
app.utility = {};
app.utility.sendmail = require('drywall-sendmail');
app.utility.slugify = require('drywall-slugify');
app.utility.workflow = require('drywall-workflow');
//listen up
app.server.listen(app.get('port'), function(){
//爬蟲主程式
require('./spider')(app, crawler, request, cheerio);
});