-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
192 lines (174 loc) · 5.44 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
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
var cfg = require("./config");
var bunyan = require('bunyan');
//目前这种简单的配置,导致log不支持[cluster](https://github.com/trentm/node-bunyan#stream-type-rotating-file)
var log = bunyan.createLogger({
name: cfg.log.name,
serializers: {request: bunyan.stdSerializers.req},
streams: [{
path: cfg.log.path,
type: 'rotating-file',
period: '1d', // daily rotation
count: 3
}]
});
//连接redis
var redis = require("redis"),
db = redis.createClient({
host: cfg.db.host,
port: cfg.db.port,
connect_timeout: cfg.db.connect_timeout,
max_attempts: cfg.db.retry
});
//redis报错处理
db.on("error", function(err){
log.error(err);
console.log(err);
var killTimer = setTimeout(function () {
process.exit(1);
}, 30000);
killTimer.unref();
server.close();
});
//创建服务器
var restify = require('restify');
var server = restify.createServer({
log: log
});
//处理全局的异常捕获
server.on("uncaughtException", function(request, response, route, error){
log.error(error);
response.send(error.statusCode, {status: false, msg: "server error"});
});
server.on("NotFound", function(request, response, error, cb){
log.error(error);
response.send(error.statusCode, {status: false, msg: error.message});
});
server.on("MethodNotAllowed", function(request, response, error, cb){
log.error(error);
response.send(error.statusCode, {status: false, msg: error.message});
});
server.on("VersionNotAllowed", function(request, response, error, cb){
log.error(error);
response.send(error.statusCode, {status: false, msg: error.message});
});
server.on("UnsupportedMediaType", function(request, response, error, cb){
log.error(error);
response.send(error.statusCode, {status: false, msg: error.message});
});
//加载必要插件
server.use(restify.queryParser()); //url参数解析
server.use(restify.requestLogger()); //为请求装载日志实例
//跨域允许
restify.CORS.ALLOW_HEADERS.push('auth');
server.pre(restify.CORS());
//根据配置加载服务路由
Object.keys(cfg.modules).forEach(function(type){
if(cfg.modules[type]){
var module = require("./modules/"+ type);
module.init(server, db);
}
});
//定义本中间件提供的2个rest服务
var api = require("./modules/datatrace");
server.get("/bindings", function(req, res, next){ //用于显示当前用户的第三方绑定账号状态
var token = req.header("AUTH", false);
if(!token){
req.log.error(new Error("request have no auth"));
res.send({status: false, msg: "携带参数缺失"});
next();
}else{
api.getUidBySessId(token, function(err, uid){
if(err){
req.log.error(err);
res.send({status: false, msg: err.message});
next();
}else{
//根据uid查看该用户的第三方账号绑定状态
db.hgetall("u2o"+uid, function(err, replies){
if(err){
req.log.error(err);
res.send({status: false, msg: err.message});
}else{
var result = {
weixin: false,
qq: false
};
if(replies){
result.weixin = replies.w?true:false;
result.qq = replies.q?true:false;
}
res.send({status: true, data: result});
}
next();
});
}
});
}
});
server.del("/bindings-clear/:type", function(req, res, next){ //用于清除当前用户的特定第三方账号绑定关系
var token = req.header("AUTH", false);
if(!token){
req.log.error(new Error("request have no auth"));
res.send({status: false, msg: "携带参数缺失"});
next();
}else{
api.getUidBySessId(token, function(err, uid){
if(err){
req.log.error(err);
res.send({status: false, msg: err.message});
next();
}else{
if(req.params.type == "weixin"){
db.hget("u2o"+uid, "w", function(err, openid){
if(err){
req.log.error(err);
res.send({status: false, msg: err.message});
}else{
db.multi()
.hdel("u2o"+uid, "w")
.del("w2u"+openid)
.exec(function(err, replies){
if(err){
req.log.error(err);
res.send({status: false, msg: err.message});
}else{
res.send({status: true});
}
next();
});
}
});
}else if(req.params.type == "qq"){
db.hget("u2o"+uid, "q", function(err, openid){
if(err){
req.log.error(err);
res.send({status: false, msg: err.message});
}else{
db.multi()
.hdel("u2o"+uid, "q")
.del("q2u"+openid)
.exec(function(err, replies){
if(err){
req.log.error(err);
res.send({status: false, msg: err.message});
}else{
res.send({status: true});
}
next();
});
}
});
}else{ //未识别的类型一律返回成功状态
res.send({status: true});
next();
}
}
});
}
});
db.on("ready", function(){
//绑定服务端口
server.listen(cfg.port, cfg.host, function() {
console.log('%s listening at %s', server.name, server.url);
});
});