├── .gitignore ├── README.md ├── app.js ├── lib └── get-config.js ├── package.json └── sample.config.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | config.json 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # O-Devs CloudFlare cache purger 2 | 3 | This project is run on AWS Lambda and is executed every time a pull request is merged on the [Orlando Devs blog repo](https://github.com/OrlandoDevs/orlandodevs.github.io). 4 | 5 | ## Configuring 6 | 7 | Before running this project you need to create a `config.json` file within the root. Feel free to copy and paste the `sample.config.json` before proceeding. You'll need to fill out every field within `config.json`. 8 | 9 | Below is an example of what your `config.json` file should look like: 10 | 11 | ```js 12 | // File location: `/config.js` 13 | { 14 | "cloudflare": { 15 | "key": "cloudflare-key", 16 | "email": "cloudflare-email", 17 | "zone": "cloudflare-zone", 18 | "endpoint": "cloudflare-endpoint" 19 | } 20 | } 21 | ``` 22 | 23 | ## Deploying 24 | 25 | Before deploying this project you will need to: 26 | 27 | 1. Clone this project 28 | 2. Run `npm install` 29 | 3. Zip project and upload to AWS Lambda 30 | 31 | Be sure to take the following steps when zipping and uploading the project to AWS Lambda: 32 | 33 | - Include every file within this project in the zip file, including the `node_modules` directory 34 | - Be sure to have `app.js`, `node_modules`, etc in the **root** of your zip files. DO NOT ZIP the parent directory, as this will make things fail on Amazon (trust me I learned this the hard way). 35 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 2 | // AWS Lambda doesn't have promise 3 | require('es6-promise').polyfill(); 4 | 5 | var axios = require('axios'); 6 | var getConfig = require('./lib/get-config') 7 | 8 | 9 | exports.handler = function(event, context) { 10 | 11 | getConfig() 12 | .then(function(config) { 13 | return axios({ 14 | method: 'delete', 15 | headers: { 16 | 'X-AUTH-KEY': config.key, 17 | 'X-AUTH-EMAIL': config.email 18 | }, 19 | url: config.endpoint + '/zones/' + config.zone + '/purge_cache', 20 | data: { purge_everything: true } 21 | }); 22 | }) 23 | .then(function(apiResponse) { 24 | console.log('success...', apiResponse.data); 25 | context.done(null, apiResponse.data) 26 | }) 27 | .catch(function(err) { 28 | console.log('error...', err); 29 | context.done(null, {error: err}) 30 | }) 31 | }; 32 | -------------------------------------------------------------------------------- /lib/get-config.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Exports get config function 3 | * @type {Function} 4 | */ 5 | module.exports = getConfig; 6 | 7 | /** 8 | * Gets cloudflare config 9 | * Checks if `config.json` file exists and validates it before using for API 10 | * @return {Promise} 11 | */ 12 | function getConfig() { 13 | return new Promise(function configPromise(resolve, reject) { 14 | 15 | try { 16 | 17 | // gets config 18 | var config = require('../config'); 19 | var cloudflare = config.cloudflare; 20 | 21 | // validates config and resolves promise 22 | if (cloudflare.key && cloudflare.email && cloudflare.zone && cloudflare.endpoint) { 23 | resolve(cloudflare); 24 | } else { 25 | throw 'Invalid config'; 26 | } 27 | 28 | } catch(err) { 29 | 30 | // Pretty up error message for module not found 31 | if (err.code === 'MODULE_NOT_FOUND') { 32 | err = 'Config file not found.'; 33 | } 34 | 35 | // Rejects promise 36 | reject({ error: err }); 37 | 38 | } 39 | }); 40 | }; 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cache-be-gone", 3 | "version": "0.0.1", 4 | "description": "Purge cache from CloudFlare", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "axios": "^0.7.0", 13 | "es6-promise": "^3.0.2" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /sample.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "cloudflare": { 3 | "key": "cloudflare-key", 4 | "email": "cloudflare-email", 5 | "zone": "cloudflare-zone", 6 | "endpoint": "cloudflare-endpoint" 7 | } 8 | } 9 | --------------------------------------------------------------------------------