From c1c0938d3893fd5abc4958db898f524a81839f1b Mon Sep 17 00:00:00 2001 From: jiaming Date: Thu, 21 Mar 2019 10:55:33 +0800 Subject: [PATCH] Added json reply when it no param is given and change the script to ask for permission to deploy route to cloudflare --- api/src/cloudflare-api/mailGetHtml.js | 8 ++++++-- api/src/cloudflare-api/mailGetKey.js | 7 +++++-- api/src/cloudflare-api/mailList.js | 7 +++++-- deploy/cloudflare/deploy.sh | 11 ++++++++--- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/api/src/cloudflare-api/mailGetHtml.js b/api/src/cloudflare-api/mailGetHtml.js index 1ef8f50..92c9790 100644 --- a/api/src/cloudflare-api/mailGetHtml.js +++ b/api/src/cloudflare-api/mailGetHtml.js @@ -10,8 +10,12 @@ let config = require("../../config/mailgunConfig") module.exports = async function(url) { let mailKey = url.searchParams.get('mailKey') if (mailKey == null || mailKey === ""){ - return new Response('Missing parameter - `mailKey`', - { status: 400, statusText: 'No `mailKey` param found' }); + + return new Response("{error: 'No `mailKey` param found'}", + { status: 400, statusText: 'INVALID_PARAMETER', headers: { + "Content-Type": "application/json" + } + }); } // Setup the authentication option object diff --git a/api/src/cloudflare-api/mailGetKey.js b/api/src/cloudflare-api/mailGetKey.js index 746ab48..1318f16 100644 --- a/api/src/cloudflare-api/mailGetKey.js +++ b/api/src/cloudflare-api/mailGetKey.js @@ -10,8 +10,11 @@ let config = require("../../config/mailgunConfig") module.exports = async function(url) { let mailKey = url.searchParams.get('mailKey') if (mailKey == null || mailKey === ""){ - return new Response('Missing parameter - `mailKey`', - { status: 400, statusText: 'INVALID_PARAMETER' }); + return new Response("{error: 'No `mailKey` param found'}", + { status: 400, statusText: 'INVALID_PARAMETER', headers: { + "Content-Type": "application/json" + } + }); } // Setup the authentication option object diff --git a/api/src/cloudflare-api/mailList.js b/api/src/cloudflare-api/mailList.js index 6780e82..995e477 100644 --- a/api/src/cloudflare-api/mailList.js +++ b/api/src/cloudflare-api/mailList.js @@ -9,8 +9,11 @@ let config = require("../../config/mailgunConfig") module.exports = async function(url) { let recipient = url.searchParams.get('recipient') if (recipient == null){ - return new Response('Missing parameter - `recipient`', - { status: 400, statusText: 'No `recipient` param found' }); + return new Response("{error: 'No `recipient` param found'}", + { status: 400, statusText: 'INVALID_PARAMETER', headers: { + "Content-Type": "application/json" + } + }); } // strip off all @domain if there is any diff --git a/deploy/cloudflare/deploy.sh b/deploy/cloudflare/deploy.sh index ca2f34f..9df1f70 100755 --- a/deploy/cloudflare/deploy.sh +++ b/deploy/cloudflare/deploy.sh @@ -75,7 +75,12 @@ export CLOUDFLARE_EMAIL="$CLOUDFLARE_EMAIL" # Calling cloudflare deploy, with parameters passing forward echo ">> Deploying to cloudflare" echo -curl -v -X PUT "https://api.cloudflare.com/client/v4/zones/"$CLOUDFLARE_ZONE_ID"/workers/script" -H "X-Auth-Email:$CLOUDFLARE_EMAIL" -H "X-Auth-Key:$CLOUDFLARE_API_KEY" -H "Content-Type:application/javascript" --data-binary "@./dist/main.js" +curl -X PUT "https://api.cloudflare.com/client/v4/zones/"$CLOUDFLARE_ZONE_ID"/workers/script" -H "X-Auth-Email:$CLOUDFLARE_EMAIL" -H "X-Auth-Key:$CLOUDFLARE_API_KEY" -H "Content-Type:application/javascript" --data-binary "@./dist/main.js" -echo ">> Setting route on cloudflare" -curl -X POST "https://api.cloudflare.com/client/v4/zones/"$CLOUDFLARE_ZONE_ID"/workers/filters" -H "X-Auth-Email:$CLOUDFLARE_EMAIL" -H "X-Auth-Key:$CLOUDFLARE_API_KEY" -H "Content-type: application/json" -d '{"pattern": "'$MAILGUN_EMAIL_DOMAIN'/api/*", "enabled": true}' \ No newline at end of file +read -p ">> Set up route on cloudflare? (yes/no)" CLOUDFLARE_SETUP_ROUTE; +if [ "$CLOUDFLARE_SETUP_ROUTE" == "yes" ]; then + echo ">> Setting route on cloudflare" + curl -X POST "https://api.cloudflare.com/client/v4/zones/"$CLOUDFLARE_ZONE_ID"/workers/filters" -H "X-Auth-Email:$CLOUDFLARE_EMAIL" -H "X-Auth-Key:$CLOUDFLARE_API_KEY" -H "Content-type: application/json" -d '{"pattern": "'$MAILGUN_EMAIL_DOMAIN'/api/*", "enabled": true}' +fi + +echo ">> Cloudflare script completed" \ No newline at end of file