forked from zensh/jsgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
157 lines (154 loc) · 6.67 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
155
156
157
var domain = require('domain'),
http = require('http'),
fs = require('fs'),
path = require('path'),
zlib = require('zlib');
var processPath = path.dirname(process.argv[1]);
var serverDm = domain.create();
global.jsGen = {}; // 注册全局变量jsGen
jsGen.version = '0.3.2';
serverDm.on('error', function (err) {
delete err.domain;
jsGen.errlog.error(err);
});
serverDm.run(function () {
jsGen.conf = module.exports.conf = require('./config/config'); // 注册rrestjs配置文件
jsGen.module = {};
jsGen.module.rrestjs = require('rrestjs');
jsGen.module.marked = require('marked');
jsGen.module.mongoskin = require('mongoskin');
jsGen.module.nodemailer = require('nodemailer');
jsGen.module.xss = require('xss');
jsGen.errlog = jsGen.module.rrestjs.restlog;
jsGen.lib = {};
jsGen.lib.tools = require('./lib/tools.js');
jsGen.lib.CacheLRU = require('./lib/cacheLRU.js');
jsGen.lib.CacheTL = require('./lib/cacheTL.js');
jsGen.lib.msg = require('./lib/msg.js');
jsGen.lib.json = require('./lib/json.js');
jsGen.lib.converter = require('./lib/anyBaseConverter.js');
jsGen.lib.email = require('./lib/email.js');
jsGen.Err = jsGen.lib.tools.Err;
jsGen.dao = {};
jsGen.dao.db = require('./dao/mongoDao.js').db;
jsGen.dao.article = require('./dao/articleDao.js');
jsGen.dao.collection = require('./dao/collectionDao.js');
jsGen.dao.index = require('./dao/indexDao.js');
jsGen.dao.message = require('./dao/messageDao.js');
jsGen.dao.tag = require('./dao/tagDao.js');
jsGen.dao.user = require('./dao/userDao.js');
jsGen.config = {};
(function () {
var that = this;
this._update = function (obj) {
jsGen.lib.tools.union(this, obj);
this._initTime = Date.now();
};
jsGen.dao.index.getGlobalConfig(serverDm.intercept(function (doc) {
if (!doc) {
var install = require('./api/install.js');
install(function () {
var doc = jsGen.lib.json.GlobalConfig;
todo(doc);
});
} else todo(doc);
function todo(doc) {
that._update(doc);
jsGen.cache = {};
jsGen.cache.pagination = new jsGen.lib.CacheTL(doc.paginationCache[0] * 1000, doc.paginationCache[1]);
jsGen.cache.timeInterval = new jsGen.lib.CacheTL(doc.TimeInterval * 1000, 0, true);
jsGen.cache.online = new jsGen.lib.CacheTL(900000, 0, true);
jsGen.cache.user = new jsGen.lib.CacheLRU(doc.userCache);
jsGen.cache.article = new jsGen.lib.CacheLRU(doc.articleCache);
jsGen.cache.comment = new jsGen.lib.CacheLRU(doc.commentCache);
jsGen.cache.list = new jsGen.lib.CacheLRU(doc.listCache);
jsGen.cache.tag = new jsGen.lib.CacheLRU(doc.tagCache);
jsGen.cache.collection = new jsGen.lib.CacheLRU(doc.collectionCache);
jsGen.cache.message = new jsGen.lib.CacheLRU(doc.messageCache);
jsGen.cache.updateList = [];
jsGen.cache.hotsList = [];
jsGen.cache.hotCommentsList = [];
jsGen.robot = {};
jsGen.robot.reg = new RegExp(doc.robots || 'Baiduspider|Googlebot|BingBot|Slurp!', 'i');
jsGen.api = {};
jsGen.api.index = require('./api/index.js');
jsGen.api.user = require('./api/user.js');
jsGen.api.article = require('./api/article.js');
jsGen.api.tag = require('./api/tag.js');
jsGen.api.collection = require('./api/collection.js');
jsGen.api.message = require('./api/message.js');
fs.readFile(processPath + '/package.json', 'utf8', serverDm.intercept(function (data) {
jsGen.config.info = JSON.parse(data);
jsGen.config.info.version = jsGen.version;
jsGen.config.info.nodejs = process.versions.node;
jsGen.config.info.rrestjs = _restConfig._version;
}));
createServer();
};
}));
}).call(jsGen.config);
});
function createServer() {
var server = http.createServer(function (req, res) {
var dm = domain.create();
dm.on('error', function (err) {
console.log(err);
delete err.domain;
err.type = 'error';
try {
res.on('finish', function () {
//jsGen.dao.db.close();
process.nextTick(function () {
dm.dispose();
});
});
if (err.hasOwnProperty('name')) {
res.sendjson({
err: err
});
} else {
//console.log('ReqErr:******************');
console.log(req.session.Uid + ':' + req.method + ' : ' + req.path);
jsGen.errlog.error(err);
res.sendjson({
err: {
name: '请求错误',
message: '对不起,请求出错了!',
type: 'error',
url: '/'
}
});
}
} catch (err) {
delete err.domain;
//console.log('CatchERR:******************');
jsGen.errlog.error(err);
dm.dispose();
}
});
dm.run(function () {
if (req.path[0] === 'api' && jsGen.api[req.path[1]]) {
jsGen.api[req.path[1]][req.method.toUpperCase()](req, res, dm);
if (req.path[1] === 'index') {
jsGen.api.index.updateOnlineCache(req);
}
} else if (jsGen.robot.reg.test(req.useragent)) {
jsGen.api.article.robot(req, res, dm);
} else {
jsGen.config.visitors += 1;
jsGen.dao.index.setGlobalConfig({
visitors: 1
});
res.setHeader("Content-Type", "text/html");
if (jsGen.indexTpl) {
res.send(jsGen.indexTpl);
} else {
fs.readFile(processPath + '/static/index.html', 'utf8', serverDm.intercept(function (data) {
jsGen.indexTpl = data.replace(/_jsGenVersion_/g, jsGen.version);
res.send(jsGen.indexTpl);
}));
}
}
});
}).listen(jsGen.module.rrestjs.config.listenPort);
};