2016-04-16 12:49:48 -04:00
|
|
|
var async = require('async');
|
2016-04-15 19:42:57 -04:00
|
|
|
var defaults = require('lodash.defaults');
|
|
|
|
var glob = require('glob');
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
var routes = require('../server/routes.json');
|
2016-04-16 10:17:20 -04:00
|
|
|
var serviceId = process.env.FASTLY_SERVICE_ID
|
|
|
|
var s3Bucket = process.env.S3_BUCKET_NAME;
|
|
|
|
|
|
|
|
var fastly = require('./lib/fastly-extended')(process.env.FASTLY_API_KEY, serviceId);
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-04-16 13:01:39 -04:00
|
|
|
const PASS_REQUEST_CONDITION_NAME = 'Pass';
|
|
|
|
const NOT_PASS_REQUEST_CONDITION_NAME = '!(Pass)'
|
|
|
|
const PASS_CACHE_CONDITION_NAME = 'Cache Pass';
|
|
|
|
const BUCKET_NAME_HEADER_NAME = 'Bucket name';
|
|
|
|
|
2016-04-15 19:42:57 -04:00
|
|
|
var extraAppRoutes = [
|
|
|
|
// Homepage with querystring.
|
|
|
|
// TODO: Should this be added for every route?
|
|
|
|
'^/\\?',
|
|
|
|
// Version output by build
|
|
|
|
'/version\.txt$',
|
|
|
|
// View html
|
|
|
|
'^/[^\/]*\.html'
|
2016-04-16 10:17:20 -04:00
|
|
|
];
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-04-16 13:46:03 -04:00
|
|
|
/*
|
|
|
|
* Given the relative path to the static directory, return an array of
|
|
|
|
* patterns matching the files and directories there.
|
|
|
|
*/
|
2016-04-15 19:42:57 -04:00
|
|
|
var getStaticPaths = function (pathToStatic) {
|
|
|
|
var staticPaths = glob.sync(path.resolve(__dirname, pathToStatic));
|
|
|
|
return staticPaths.map( function (pathName) {
|
|
|
|
// Reduce absolute path to relative paths like '/js'
|
|
|
|
var base = path.dirname(path.resolve(__dirname, pathToStatic));
|
|
|
|
return '^' + pathName.replace(base, '');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-16 13:46:03 -04:00
|
|
|
/*
|
|
|
|
* Given a list of express routes, return a list of patterns to match
|
|
|
|
* the express route and a static view file associated with the route
|
|
|
|
*/
|
2016-04-15 19:42:57 -04:00
|
|
|
var getViewPaths = function (routes) {
|
|
|
|
return routes.map(function (route) {
|
|
|
|
return route.pattern;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-16 13:46:03 -04:00
|
|
|
/*
|
|
|
|
* Given a list of patterns for paths, OR all of them together into one
|
|
|
|
* string suitable for a Fastly condition
|
|
|
|
*/
|
2016-04-15 19:42:57 -04:00
|
|
|
var pathsToCondition = function (paths) {
|
|
|
|
return paths.reduce(function(conditionString, pattern) {
|
|
|
|
var patternCondition = 'req.url ~ "' + pattern + '"';
|
|
|
|
return conditionString + (conditionString ? ' || ' : '') + patternCondition;
|
|
|
|
}, '');
|
|
|
|
}
|
|
|
|
|
2016-04-16 13:46:03 -04:00
|
|
|
/*
|
|
|
|
* Combine static paths, routes, and any additional paths to a single
|
|
|
|
* fastly condition to match req.url
|
|
|
|
*/
|
2016-04-15 19:42:57 -04:00
|
|
|
var getAppRouteCondition = function (pathToStatic, routes, additionalPaths) {
|
|
|
|
var staticPaths = getStaticPaths(pathToStatic);
|
|
|
|
var viewPaths = getViewPaths(routes);
|
|
|
|
var allPaths = [].concat(staticPaths, viewPaths, additionalPaths);
|
|
|
|
return pathsToCondition(allPaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
var getConditionNameForView = function (view) {
|
|
|
|
return 'routes/' + view;
|
|
|
|
};
|
|
|
|
|
|
|
|
var getHeaderNameForView = function (view) {
|
|
|
|
return 'rewrites/' + view;
|
|
|
|
}
|
|
|
|
|
|
|
|
var notPassCondition = {
|
2016-04-16 13:01:39 -04:00
|
|
|
name: NOT_PASS_REQUEST_CONDITION_NAME,
|
2016-04-15 19:42:57 -04:00
|
|
|
statement: getAppRouteCondition('../static/*', routes, extraAppRoutes),
|
|
|
|
type: 'REQUEST',
|
|
|
|
priority: 10
|
|
|
|
};
|
|
|
|
var passCondition = {
|
2016-04-16 13:01:39 -04:00
|
|
|
name: PASS_REQUEST_CONDITION_NAME,
|
2016-04-16 10:17:20 -04:00
|
|
|
statement: fastly.negateConditionStatement(notPassCondition.statement),
|
2016-04-15 19:42:57 -04:00
|
|
|
type: 'REQUEST',
|
|
|
|
priority: 10
|
|
|
|
};
|
2016-04-16 13:01:39 -04:00
|
|
|
var passCacheCondition = defaults({name: PASS_CACHE_CONDITION_NAME, type: 'CACHE'}, passCondition);
|
2016-04-16 12:49:48 -04:00
|
|
|
var bucketNameHeader = {
|
2016-04-16 13:01:39 -04:00
|
|
|
name: BUCKET_NAME_HEADER_NAME,
|
2016-04-16 12:49:48 -04:00
|
|
|
action: 'set',
|
|
|
|
ignore_if_set: 0,
|
|
|
|
type: 'REQUEST',
|
|
|
|
dst: 'http.host',
|
|
|
|
src: '"' + s3Bucket + '"',
|
2016-04-16 13:01:39 -04:00
|
|
|
request_condition: NOT_PASS_REQUEST_CONDITION_NAME,
|
2016-04-16 12:49:48 -04:00
|
|
|
priority: 1
|
|
|
|
};
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-04-16 12:49:48 -04:00
|
|
|
async.waterfall([
|
|
|
|
fastly.getLatestVersion.bind(fastly),
|
|
|
|
function (version, next) {
|
|
|
|
if (version.active) return next('Latest version is active. Will not modify.');
|
|
|
|
if (version.locked) return next('Latest version is locked. Cannot modify.');
|
|
|
|
async.parallel([
|
|
|
|
function (cb) {
|
|
|
|
async.series([
|
|
|
|
async.apply(fastly.setCondition.bind(fastly), version.number, notPassCondition),
|
|
|
|
async.apply(fastly.setFastlyHeader.bind(fastly), version.number, bucketNameHeader)
|
|
|
|
], cb)
|
|
|
|
},
|
|
|
|
function (cb) {
|
|
|
|
async.series([
|
|
|
|
async.apply(fastly.setCondition.bind(fastly), version.number, passCondition),
|
|
|
|
function (cb) {
|
|
|
|
async.parallel([
|
|
|
|
async.apply(
|
|
|
|
fastly.request.bind(fastly), 'PUT',
|
|
|
|
fastly.getFastlyAPIPrefix(serviceId, version.number) + '/request_settings/Pass',
|
2016-04-16 13:01:39 -04:00
|
|
|
{request_condition: PASS_REQUEST_CONDITION_NAME}
|
2016-04-16 12:49:48 -04:00
|
|
|
),
|
|
|
|
async.apply(
|
|
|
|
fastly.request.bind(fastly), 'PUT',
|
|
|
|
fastly.getFastlyAPIPrefix(serviceId, version.number) + '/backend/femto',
|
2016-04-16 13:01:39 -04:00
|
|
|
{request_condition: PASS_REQUEST_CONDITION_NAME}
|
2016-04-16 12:49:48 -04:00
|
|
|
)
|
|
|
|
], cb)
|
2016-04-16 10:17:20 -04:00
|
|
|
}
|
2016-04-16 12:49:48 -04:00
|
|
|
], cb)
|
|
|
|
},
|
|
|
|
function (cb) {
|
|
|
|
async.series([
|
|
|
|
async.apply(fastly.setCondition.bind(fastly), version.number, passCacheCondition),
|
|
|
|
async.apply(
|
|
|
|
fastly.request.bind(fastly), 'PUT',
|
|
|
|
fastly.getFastlyAPIPrefix(serviceId, version.number) + '/cache_settings/Pass',
|
2016-04-16 14:05:16 -04:00
|
|
|
{cache_condition: PASS_CACHE_CONDITION_NAME}
|
2016-04-16 12:49:48 -04:00
|
|
|
),
|
|
|
|
], cb)
|
|
|
|
},
|
|
|
|
function (cb) {
|
|
|
|
async.forEachOf(routes, function (route, id, cb) {
|
|
|
|
var condition = {
|
|
|
|
name: getConditionNameForView(route.view),
|
|
|
|
statement: 'req.url ~ "' + route.pattern + '"',
|
|
|
|
type: 'REQUEST',
|
2016-04-16 14:08:53 -04:00
|
|
|
priority: id
|
2016-04-16 12:49:48 -04:00
|
|
|
};
|
|
|
|
var header = {
|
|
|
|
name: getHeaderNameForView(route.view),
|
|
|
|
action: 'set',
|
|
|
|
ignore_if_set: 0,
|
|
|
|
type: 'request',
|
|
|
|
dst: 'url',
|
|
|
|
src: '"/' + route.view + '.html"',
|
|
|
|
request_condition: getConditionNameForView(route.view),
|
2016-04-16 14:08:53 -04:00
|
|
|
priority: 10
|
2016-04-16 12:49:48 -04:00
|
|
|
};
|
|
|
|
async.series([
|
|
|
|
async.apply(fastly.setCondition.bind(fastly), version.number, condition),
|
|
|
|
async.apply(fastly.setFastlyHeader.bind(fastly), version.number, header)
|
|
|
|
], cb);
|
|
|
|
}, cb)
|
2016-04-16 10:17:20 -04:00
|
|
|
}
|
2016-04-16 12:49:48 -04:00
|
|
|
], next);
|
|
|
|
}], function (err, responses) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
process.exit(1);
|
2016-04-15 19:42:57 -04:00
|
|
|
}
|
2016-04-16 12:49:48 -04:00
|
|
|
}
|
|
|
|
);
|