이유혁

EC2 merging

Showing 1 changed file with 17 additions and 7 deletions
...@@ -22,18 +22,19 @@ const line_channel = config.line_channel; ...@@ -22,18 +22,19 @@ const line_channel = config.line_channel;
22 const client = new line.Client(line_channel); 22 const client = new line.Client(line_channel);
23 23
24 // ELB health checker 24 // ELB health checker
25 -app.get('/', (req, res) => { 25 +app.get('/', (res) => {
26 console.log('ELB health check'); 26 console.log('ELB health check');
27 res.writeHead(200, { "Content-Type": "text/html" }); 27 res.writeHead(200, { "Content-Type": "text/html" });
28 res.end(); 28 res.end();
29 }); 29 });
30 30
31 // register a webhook handler with middleware 31 // register a webhook handler with middleware
32 -// about the middleware, please refer to doc
33 app.post('/webhook', line.middleware(line_channel), (req, res) => { 32 app.post('/webhook', line.middleware(line_channel), (req, res) => {
33 + // webhook post 요청에 대해 promises를 전체 수행한다.
34 + // 전체 수행은 순차수행이기 때문에 동기처리 필요 => async await 패턴을 사용한다
34 const promises = req.body.events.map(async (event) => { 35 const promises = req.body.events.map(async (event) => {
35 - let language_type = await language_detector(event); 36 + let api_connector = await api_connect(event);
36 - let options = await option_maker(language_type, event); 37 + let options = await option_maker(api_connector, event);
37 let result = await receive_result(options, event); 38 let result = await receive_result(options, event);
38 client.replyMessage(event.replyToken,result); 39 client.replyMessage(event.replyToken,result);
39 }) 40 })
...@@ -46,7 +47,9 @@ app.post('/webhook', line.middleware(line_channel), (req, res) => { ...@@ -46,7 +47,9 @@ app.post('/webhook', line.middleware(line_channel), (req, res) => {
46 }); 47 });
47 }); 48 });
48 49
49 -const language_detector = (event) => { 50 +// 이벤트 타입 검사 이후
51 +// language detector api url 및 client id, secret 을 담은 connector를 반환한다.
52 +const api_connect = (event) => {
50 if (event.type !== 'message' || event.message.type !== 'text') { 53 if (event.type !== 'message' || event.message.type !== 'text') {
51 // ignore non-text-message event 54 // ignore non-text-message event
52 return reject(new Error('메세지 혹은, 텍스트가 아닙니다.')); 55 return reject(new Error('메세지 혹은, 텍스트가 아닙니다.'));
...@@ -61,9 +64,12 @@ const language_detector = (event) => { ...@@ -61,9 +64,12 @@ const language_detector = (event) => {
61 }) 64 })
62 } 65 }
63 66
64 -const option_maker = (language_type, event) => { 67 +
68 +// post 요청으로 api connector를 이용해 language를 분석한 뒤
69 +// source 및 target 설정, 번역 API url을 포함한 options을 반환한다
70 +const option_maker = (api_connector, event) => {
65 return new Promise((resolve, reject) => { 71 return new Promise((resolve, reject) => {
66 - request.post(language_type, (error,response) => { 72 + request.post(api_connector, (error,response) => {
67 console.log(response.statusCode); 73 console.log(response.statusCode);
68 if(!error && response.statusCode == 200){ 74 if(!error && response.statusCode == 200){
69 let detect_body = JSON.parse(response.body); 75 let detect_body = JSON.parse(response.body);
...@@ -85,12 +91,15 @@ const option_maker = (language_type, event) => { ...@@ -85,12 +91,15 @@ const option_maker = (language_type, event) => {
85 resolve(options); 91 resolve(options);
86 } 92 }
87 else{ 93 else{
94 + // 예외처리
88 reject(new Error("request is failed")); 95 reject(new Error("request is failed"));
89 } 96 }
90 }) 97 })
91 }) 98 })
92 } 99 }
93 100
101 +// options를 받아서 post 요청을 통해 번역을 실행한다.
102 +// response를 JSON parsing 한 뒤 결과 result에 메세지를 담아서 반환한다.
94 const receive_result = (options, event) => { 103 const receive_result = (options, event) => {
95 return new Promise((resolve, reject) => { 104 return new Promise((resolve, reject) => {
96 var result = { type:'text', text: ''}; 105 var result = { type:'text', text: ''};
...@@ -105,6 +114,7 @@ const receive_result = (options, event) => { ...@@ -105,6 +114,7 @@ const receive_result = (options, event) => {
105 resolve(result); 114 resolve(result);
106 } 115 }
107 else{ 116 else{
117 + // 예외처리
108 result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.'; 118 result.text = '언어를 감지할 수 없습니다. \n 번역 언어는 한글 또는 영어만 가능합니다.';
109 client.replyMessage(event.replyToken,result).then(resolve).catch(reject); 119 client.replyMessage(event.replyToken,result).then(resolve).catch(reject);
110 reject(new Error("request is failed")); 120 reject(new Error("request is failed"));
......