-
Notifications
You must be signed in to change notification settings - Fork 3
/
servermain.js
347 lines (256 loc) · 8.03 KB
/
servermain.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
var express = require('express');
var Fiber = require('fibers');
var http = require('http');
var https = require('https');
var url = require('url');
var fs = require('fs');
var path = require("path");
var querystring = require('querystring');
var uuid = require('uuid');
var session = require('express-session');
var moment = require("moment");
var config = require('./func/config.js');//引用config目录内的config.js文件//执行读取缓存文件配置方法(必须在使用之前调用,缓存具体使用方法请参考config.js注释)
var redis = require("redis");
var cipher = require('./func/cipher.js');
var logs = require('./func/logs.js');
var version ='2.0.2';
console.log('version:'+version);
config.readfile();
/**进程错误监控*/
process.on('uncaughtException', function (err) {
console.log(err.stack);
logs.write('err',err.stack);
});
process.on('SyntaxError', function (err) {
console.log(err.stack);
logs.write('err',err.stack);
});
/**进程错误监控*/
/**http服务及https服务*/
var app = express();
//session存储时间 30分钟(单位=毫秒)
var sessionTime = 30 * 60 * 1001;
//redis存储session
if(config.get('app').redis.使用 == '是'){
var RedisStrore = require('connect-redis')(session);
var cf = {
"cookie" : {
"maxAge" : sessionTime
},
"sessionStore" : {
"host" : config.get('app').redis.host,
"port" : config.get('app').redis.port,
"pass" : config.get('app').redis.password,
"db" : config.get('app').redis.sessionDB,
"ttl" : 1800,
"logErrors" : true
}
};
app.use(session({
name : "hy-go",
secret : 'Asecret123-',
resave : true,
rolling:true,
saveUninitialized : false,
cookie : cf.cookie,
store : new RedisStrore(cf.sessionStore)
}));
}else{
app.use(session({
resave:true,
secret: uuid.v4(), //secret的值建议使用随机字符串
saveUninitialized: true,
cookie: {maxAge: sessionTime} // 过期时间(毫秒)
}));
}
var server = http.createServer(app);
server.listen(config.get('app').main.httpPort, function() {
console.log("HTTP SERVER 启动成功! 监听端口:"+ config.get('app').main.httpPort);
});
if(config.get('app').main.httpsPort != 0 || config.get('app').main.httpsPort != '0'){
//https证书配置
var options = {
key : fs.readFileSync('./config/https.key'),
cert :fs.readFileSync('./config/https.pem')
}
//https监听启动
https.createServer(options, app).listen(config.get('app').main.httpsPort, function () {
console.log('Https server 启动成功! 监听端口:' + config.get('app').main.httpsPort);
});
}
app.use(express.static('www'));
app.set('views', path.join(__dirname, 'www'));
app.set("view engine",'ejs');
app.post('/api.post',function(req,res){
var body = '';
req.on('data', function(chunk){
body += chunk;
});
req.on('end', function(){
try{
console.log('-----------------接收参数-----------------');
console.log(moment().format('YYYY-MM-DD HH:mm:ss')+body);
console.log('-----------------接收参数-----------------');
body = querystring.parse(body);
//调用开始时间毫秒数
body.startTime = new Date().getTime();
body.date = moment().format('YYYY-MM-DD HH:mm:ss');
body.ip = req.ip;
body.uuid = uuid.v4();
if(body.func == undefined || body.words == undefined){
res.send('{"code":"-2","msg":"func or words undefined "}').end();
}else{
//截取数据解密
var key = body.words.substr(0, 32);
var data = cipher.aesdecode(body.words.substr(32, body.words.length),key);
if(data == null){
res.send('{"code":"-3","msg":"words decode error"}').end();
}else{
body.receive = JSON.parse(data);
var api = require('./api/api.js');
var bool = api.searchfile(body.func);
if(bool){
api.index(req,res,body);
}else{
res.send('{"code":"-4","msg":"not find func or words "}').end();
}
}
}
}catch(e){
console.log(e);
res.send('{"code":"-1","msg":"body error"}').end();
}
});
});
app.get('/*.xhtml',function(req,res){
var data = {};
data.headers = req.headers;
data.method = "GET";
data.path = req.path;
data.arg = url.parse(req.url, true).query;
data.startTime = new Date().getTime();
data.session = req.session;
data.date = moment().format('YYYY-MM-DD HH:mm:ss');
data.ip = req.ip;
data.uuid = uuid.v4();
var db_pools = require('./func/db_pools.js');
db_pools('',function(name,db_obj){
var main = require('./routes/app_routes_main.js');
main.index(req,res,data,db_obj);
});
});
app.post('/*.xhtml',function(req,res){
var bufs = [];
req.on('data', function(chunk){
bufs.push(chunk);
}).on('end',function(){
var data = {};
data.headers = req.headers;
data.method = "POST";
data.path = req.path;
data.arg = url.parse(req.url, true).query;
data.data = Buffer.concat(bufs).toString();
data.buf = Buffer.concat(bufs);
data.startTime = new Date().getTime();
data.session = req.session;
data.date = moment().format('YYYY-MM-DD HH:mm:ss');
data.ip = req.ip;
data.uuid = uuid.v4();
var db_pools = require('./func/db_pools.js');
db_pools('',function(name,db_obj){
var main = require('./routes/app_routes_main.js');
main.index(req,res,data,db_obj);
});
});
});
//404处理
// app.use(function(req, res, next) {
// res.status(404).send('Sorry cant find that!');
// });
/**http服务及https服务*/
var schedule = require('node-schedule');
/**定时任务--每分钟执行一次*/
var rule = new schedule.RecurrenceRule();
var times = [];
for(var i=0; i<60; i++){
times.push(i);
}
rule.second = times;
var timeStart = schedule.scheduleJob(rule, function(){
var second = moment().second();
if(second == 30 || second == 0 ){
if(config.get('app').main.dbType == 'mysql'){
var mysql = require('./func/mysql.js');
console.log("mysql当前连接数"+mysql.pool._count);
console.log("mysql可用连接数"+mysql.pool._availableObjects.length);
}else{
var pgdb = require('./func/pgdb.js');
console.log("postgresql当前连接数"+pgdb.pool._count);
console.log("postgresql可用连接数"+pgdb.pool._availableObjects.length);
}
if(config.get('app').mongodb.使用 == '是'){
var mongo = require('./func/mongo.js');
console.log("mongodb当前连接数"+mongo.pool._count);
console.log("mongodb可用连接数"+mongo.pool._availableObjects.length);
}
if(config.get('app').redis.使用 == '是'){
var redisdb = require('./func/redisdb.js');
console.log("redis当前连接数"+redisdb.pool._count);
console.log("redis可用连接数"+redisdb.pool._availableObjects.length);
}
}
});
//文件上传
var formidable = require('formidable');
app.post("/temp",function(req,res){
var data = {};
var form = new formidable.IncomingForm();//实例化formidable对象
var dir = path.normalize(__dirname + "/temp/");//路径
form.uploadDir = dir;//更改上传地址
if(fs.existsSync(dir) == false) {
fs.mkdirSync(dir);
}
form.parse(req, function(err, fields, files) {//图片传入及解析
if(err) {
data.状态 = '文件保存失败';
res.send(data).end();
}else{
var length = 0;
for (item in files) {
length++;
}
if (length === 0) {
data.状态 = '文件保存失败';
res.send(data).end();
return;
}
for (item in files) {
var file = files[item];
var type = file.name.split('.')[1];
var ttt = moment().format("YYYYMMDDHHmmss");
var ran = parseInt(Math.random() * 89999 + 10000);
var oldpath = file.path;
var newName = ttt + ran;
var newpath = path.normalize(__dirname + "/temp/" + newName + "." + type);
fs.rename(oldpath, newpath, function(err) {
if(err) {
data.状态 = '文件改名失败';
res.send(data).end();
} else {
data.状态 = '成功';
data.newpath = newpath;
res.send(data).end();
}
});
}
}
});
});
var web_im = require('./func/web_im.js');
web_im.listen(server);
web_im.listen_channel();
web_im.io.on('connection', function (socket) {
socket.uuid = socket.id;
socket.emit('connectionResult', socket.id);
web_im.onObject(socket);
});