Skip to content

Commit

Permalink
fix: optimize SNS handler
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-lemon committed Aug 9, 2019
1 parent 8da329c commit f496cce
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 179 deletions.
45 changes: 24 additions & 21 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,39 +146,42 @@ functions:
# Common SNS Service.
SNS:
handler: handler.SNS
timeout: 150 # max timeout. (= 5 min)
events:
- sns: 'arn:aws:sns:${self:provider.region}:#{AWS::AccountId}:${self:custom.topicName}'
#------------------
# Common SQS Service.
SQS:
handler: handler.SQS
timeout: 150 # max timeout. (= 5 min)
events:
- sqs:
arn:
Fn::GetAtt:
- MyQueue
- Arn
batchSize: 1 # the maximum number of SQS messages that AWS will send to your Lambda function on a single trigger. (default: 10)
#------------------
# Common WSS (WebSocket Server).
# - see: https://serverless.com/framework/docs/providers/aws/events/websocket/
WSS:
handler: handler.WSS
events:
- websocket:
route: $connect
- websocket:
route: $disconnect
- websocket:
route: $default
# authorizer: auth
# authorizer:
# name: auth
# identitySource:
# - 'route.request.header.Auth'
# - 'route.request.querystring.Auth'
- websocket:
route: echo # sample route by routeKey. echo request.
batchSize: 1 # the maximum number of SQS messages that AWS will send to your Lambda function on a single trigger. (default: 10)
#TODO - refactoring WSS
# #------------------
# # Common WSS (WebSocket Server).
# # - see: https://serverless.com/framework/docs/providers/aws/events/websocket/
# WSS:
# handler: handler.WSS
# events:
# - websocket:
# route: $connect
# - websocket:
# route: $disconnect
# - websocket:
# route: $default
# # authorizer: auth
# # authorizer:
# # name: auth
# # identitySource:
# # - 'route.request.header.Auth'
# # - 'route.request.querystring.Auth'
# - websocket:
# route: echo # sample route by routeKey. echo request.
#------------------
# REST API : /hello
hello:
Expand Down
134 changes: 0 additions & 134 deletions src/SNS.js

This file was deleted.

48 changes: 25 additions & 23 deletions src/api/hello-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
import { $U, _log, _inf, _err } from 'lemon-core';
import { loadJsonSync } from 'lemon-core';

import * as $kms from '../service/kms-service';
import * as $s3s from '../service/s3s-service';
import * as $sns from '../service/sns-service';
import AWS from 'aws-sdk';

//! define NS, and export default handler().
export const NS = $U.NS('HELO', 'yellow'); // NAMESPACE TO BE PRINTED.
import { $WEB } from 'lemon-core';
export default $WEB(NS, decode_next_handler);

//! import dependency
import $engine from '../engine';
import url from 'url';
import https from 'https';

import * as $kms from '../service/kms-service';
import * as $s3s from '../service/s3s-service';
import * as $sns from '../service/sns-service';
import AWS from 'aws-sdk';

/** ********************************************************************************************************************
* Decode Next Handler
** ********************************************************************************************************************/
Expand Down Expand Up @@ -99,9 +104,6 @@ const NODES = [
* @param {*} message Object or String.
*/
const postMessage = (hookUrl, message) => {
const url = require('url');
const https = require('https');

const body = typeof message === 'string' ? message : JSON.stringify(message);
const options = url.parse(hookUrl);
options.method = 'POST';
Expand Down Expand Up @@ -356,7 +358,7 @@ const chain_process_callback = ({ subject, data, context }) => {
* @param {*} $body body parameters (json)
* @param {*} $ctx context (optional)
*/
function do_list_hello(ID, $param, $body, $ctx) {
export function do_list_hello(ID, $param, $body, $ctx) {
_log(NS, `do_list_hello(${ID})....`);

const that = {};
Expand All @@ -373,7 +375,7 @@ function do_list_hello(ID, $param, $body, $ctx) {
* ```sh
* $ http ':8888/hello/0'
*/
function do_get_hello(ID, $param, $body, $ctx) {
export function do_get_hello(ID, $param, $body, $ctx) {
_log(NS, `do_get_hello(${ID})....`);

const id = $U.N(ID, 0);
Expand All @@ -392,7 +394,7 @@ function do_get_hello(ID, $param, $body, $ctx) {
* ```sh
* $ echo '{"size":1}' | http PUT ':8888/hello/1'
*/
function do_put_hello(ID, $param, $body, $ctx) {
export function do_put_hello(ID, $param, $body, $ctx) {
_log(NS, `do_put_hello(${ID})....`);
$param = $param || {};

Expand All @@ -409,7 +411,7 @@ function do_put_hello(ID, $param, $body, $ctx) {
* ```sh
* $ echo '{"name":"lemoncloud"}' | http POST ':8888/hello/0'
*/
function do_post_hello(ID, $param, $body, $ctx) {
export function do_post_hello(ID, $param, $body, $ctx) {
_log(NS, `do_post_hello(${ID})....`);
$param = $param || {};
if (!$body && !$body.name) return Promise.reject(new Error('.name is required!'));
Expand All @@ -433,7 +435,7 @@ function do_post_hello(ID, $param, $body, $ctx) {
* @param {*} $body {error?:'', message:'', data:{...}}
* @param {*} $ctx context
*/
function do_post_hello_slack(ID, $param, $body, $ctx) {
export function do_post_hello_slack(ID, $param, $body, $ctx) {
_log(NS, `do_post_hello_slack(${ID})....`);
_log(NS, '> body =', $body);
$param = $param || {};
Expand Down Expand Up @@ -511,13 +513,14 @@ function do_post_hello_slack(ID, $param, $body, $ctx) {
* $ cat data/error-1.json | http ':8888/hello/!/event?subject=error'
* $ cat data/error-2.json | http ':8888/hello/!/event?subject=error'
*/
function do_post_hello_event(ID, $param, $body, $ctx) {
_log(NS, `do_post_hello_event(${ID})....`);
export function do_post_hello_event(id, $param, $body, $ctx) {
_inf(NS, `do_post_hello_event(${id})....`);
$param = $param || {};
const subject = `${$param.subject || ''}`;
const data = $body;
const context = $ctx;

//! decode next-chain.
const chain_next = false
? null
: subject.startsWith('ALARM:')
Expand All @@ -539,7 +542,7 @@ function do_post_hello_event(ID, $param, $body, $ctx) {
* ```sh
* $ http DELETE ':8888/hello/1'
*/
function do_delete_hello(ID, $param, $body, $ctx) {
export function do_delete_hello(ID, $param, $body, $ctx) {
_log(NS, `do_delete_hello(${ID})....`);

return do_get_hello(ID, null, null, $ctx).then(node => {
Expand All @@ -558,7 +561,7 @@ function do_delete_hello(ID, $param, $body, $ctx) {
* $ http ':8888/hello/alarm/test-sns'
* $ http ':8888/hello/failure/test-sns'
*/
function do_get_test_sns(ID, $param, $body, $ctx) {
export function do_get_test_sns(ID, $param, $body, $ctx) {
_log(NS, `do_get_test_sns(${ID})....`);

//! build event body, then start promised
Expand All @@ -584,8 +587,7 @@ function do_get_test_sns(ID, $param, $body, $ctx) {

//! call sns handler.
const local_chain_handle_sns = event => {
// const $SNS = require('../../SNS')(_$);
const SNS = require('../engine').SNS;
const SNS = $engine.SNS;
if (!SNS) return Promise.reject(new Error('.SNS is required!'));

//! validate event
Expand Down Expand Up @@ -626,7 +628,7 @@ function do_get_test_sns(ID, $param, $body, $ctx) {
* ```sh
* $ http ':8888/hello/0/test-sns-arn'
*/
function do_get_test_sns_arn(ID, $param, $body, $ctx) {
export function do_get_test_sns_arn(ID, $param, $body, $ctx) {
_log(NS, `do_get_test_sns_arn(${ID})....`);
return $sns.arn().then(arn => {
_log(NS, '> arn =', arn);
Expand All @@ -640,7 +642,7 @@ function do_get_test_sns_arn(ID, $param, $body, $ctx) {
* ```sh
* $ http ':8888/hello/0/test-sns-err'
*/
function do_get_test_sns_err(ID, $param, $body, $ctx) {
export function do_get_test_sns_err(ID, $param, $body, $ctx) {
_log(NS, `do_get_test_sns_err(${ID})....`);
const e = new Error('Test Error');
return $sns.reportError(e).then(mid => {
Expand All @@ -655,7 +657,7 @@ function do_get_test_sns_err(ID, $param, $body, $ctx) {
* ```sh
* $ http ':8888/hello/0/test-encrypt'
*/
function do_get_test_encrypt(ID, $param, $body, $ctx) {
export function do_get_test_encrypt(ID, $param, $body, $ctx) {
_log(NS, `do_get_test_encrypt(${ID})....`);
const message = 'hello lemon';
return $kms
Expand All @@ -673,7 +675,7 @@ function do_get_test_encrypt(ID, $param, $body, $ctx) {
* ```sh
* $ http ':8888/hello/0/test-s3-put'
*/
function do_get_test_s3_put(ID, $param, $body, $ctx) {
export function do_get_test_s3_put(ID, $param, $body, $ctx) {
_log(NS, `do_get_test_s3_put(${ID})....`);
const message = 'hello lemon';
const data = { message };
Expand Down
Loading

0 comments on commit f496cce

Please sign in to comment.