基于 OpenWhisk 实时监测静态网页是否有乱码(2)实例-3
- UID
- 1066743
|
基于 OpenWhisk 实时监测静态网页是否有乱码(2)实例-3
自定义Garbage Char Detection Action对有改动或新增的页面进行乱码检测第四步主要介绍 Garbage Char Detection API,以及如何在Action中接收getPushPayloadAction传来数据,并调用Garbage Char Detection API。
用 创建garbageDetectionAction:
1
| $ wsk action create garbageDetectionAction garbageDetectionAction.js
|
garbageDetectionAction.js内容如代码清单2:
代码清单3. garbageDetectionAction.js1
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
| var request = require('request');
var http = require("http");
var querystring = require('querystring');
var utils = {
map : function(arr, func){
var res = [];
for(var i=0,len=arr.length; i<len; i++){
res.push(func(arr));
}
return res;
},
encodeStrings : function(arr){
return utils.map(arr, querystring.escape);
},
decodeStrings : function(arr){
return utils.map(arr, decodeURI);
},
};
function main(params) {
// payload is urls with ',' split, such as "url1,url2"
var payload = params["payload"].split(',') || [];
var data=[];
for(var i=0,len=payload.length; i<len; i++){
data.push({'url': payload});
}
console.log(data);
var options = {
host: 'garbagecodedetection.mybluemix.net',
port: 80,
path: '/rest/garbagechar_scan',
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
var req = http.request(options, function (res) {
res.on('data', function (message) {
var ret= eval('(' + message + ')');
console.log('response : ' ,ret);
console.log('response type: ', typeof ret);
for(var i=0; i<ret.length; i++) {
ret['garbled_lines'] =
utils.encodeStrings(ret['garbled_lines']);
}
console.log('Encoded response: ', ret)
whisk.done({result: ret});
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
whisk.done({text: 'Problem with request' + e.message});
});
// write data to request body
req.write(JSON.stringify(data));
req.end();
return whisk.async();
}
|
通过下面命令来测试garbageDetectionAction:
1
| $ wsk action invoke garbageDetectAction --blocking --result -p payload
|
,
在终端会输出如下结果如图7:
图7.检测结果 |
|
|
|
|
|