|
@@ -0,0 +1,84 @@
|
|
|
+/**
|
|
|
+ * Created by root on 2015-07-20.
|
|
|
+ */
|
|
|
+var restify = require('restify');
|
|
|
+var loginRecordProvider = require('./database/loginRecord');
|
|
|
+var BufferHelper = require('bufferhelper');
|
|
|
+var db = require('./database/db');
|
|
|
+
|
|
|
+var server = restify.createServer({
|
|
|
+ name: 'BehaviorMonitor',
|
|
|
+ version: '1.0.0'
|
|
|
+});
|
|
|
+server.use(restify.acceptParser(server.acceptable));
|
|
|
+server.use(restify.queryParser());
|
|
|
+server.use(restify.bodyParser());
|
|
|
+
|
|
|
+server.post('/api/loginrecord', function(req, res)
|
|
|
+{
|
|
|
+ var bufferHelper = new BufferHelper();
|
|
|
+ req.on('data',function(chunk)
|
|
|
+ {
|
|
|
+ bufferHelper.concat(chunk);
|
|
|
+ });
|
|
|
+
|
|
|
+ req.on('end', function()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var chunks = bufferHelper.toBuffer().toString();
|
|
|
+ console.log('receive data:\n'+chunks);
|
|
|
+ var json = JSON.parse(chunks);
|
|
|
+ json.gateway = getRemoteAdress(req);
|
|
|
+ return loginRecordHandler(res, json, chunks);
|
|
|
+ }
|
|
|
+ catch (ex)
|
|
|
+ {
|
|
|
+ console.log('error:'+ex);
|
|
|
+ return error(res,'data format error.');
|
|
|
+ }
|
|
|
+ });
|
|
|
+});
|
|
|
+
|
|
|
+function getRemoteAdress(req)
|
|
|
+{
|
|
|
+ var ip = req.headers['x-forwarded-for'] ||
|
|
|
+ req.connection.remoteAddress ||
|
|
|
+ req.socket.remoteAddress ||
|
|
|
+ req.connection.socket.remoteAddress;
|
|
|
+ return ip;
|
|
|
+};
|
|
|
+
|
|
|
+function loginRecordHandler(res,msg,raw)
|
|
|
+{
|
|
|
+ if(!msg.loginName)
|
|
|
+ return error(res,'loginName is null.');
|
|
|
+
|
|
|
+ loginRecordProvider.insert(msg, function(err)
|
|
|
+ {
|
|
|
+ if(err)
|
|
|
+ error(res,error);
|
|
|
+ else
|
|
|
+ return ok(res);
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+function error(res,error)
|
|
|
+{
|
|
|
+ console.log(error);
|
|
|
+ sendJSON(res,0,error);
|
|
|
+};
|
|
|
+
|
|
|
+function ok(res)
|
|
|
+{
|
|
|
+ sendJSON(res,1,'ok');
|
|
|
+};
|
|
|
+
|
|
|
+db.active(function(err){
|
|
|
+ if (err)
|
|
|
+ console.log(err);
|
|
|
+ else
|
|
|
+ server.listen(8080, function () {
|
|
|
+ console.log('%s listening at %s', server.name, server.url);
|
|
|
+ });
|
|
|
+});
|