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');
|
2017-04-25 13:18:13 -04:00
|
|
|
var fastlyConfig = require('./lib/fastly-config-methods');
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-05-20 10:33:13 -04:00
|
|
|
var route_json = require('../src/routes.json');
|
2016-04-16 10:17:20 -04:00
|
|
|
|
2016-04-18 12:32:20 -04:00
|
|
|
const FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID || '';
|
|
|
|
const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME || '';
|
2016-04-16 13:01:39 -04:00
|
|
|
|
2016-04-18 12:32:20 -04:00
|
|
|
var fastly = require('./lib/fastly-extended')(process.env.FASTLY_API_KEY, FASTLY_SERVICE_ID);
|
|
|
|
|
2016-04-15 19:42:57 -04:00
|
|
|
var extraAppRoutes = [
|
|
|
|
// Homepage with querystring.
|
|
|
|
// TODO: Should this be added for every route?
|
2016-07-22 12:38:24 -04:00
|
|
|
'/\\?',
|
2016-04-15 19:42:57 -04:00
|
|
|
// View html
|
2016-07-22 12:38:24 -04:00
|
|
|
'/[^\/]*\.html$'
|
2016-04-16 10:17:20 -04:00
|
|
|
];
|
2016-04-15 19:42:57 -04:00
|
|
|
|
2016-05-20 10:33:13 -04:00
|
|
|
var routes = route_json.map(function (route) {
|
2017-04-25 13:18:13 -04:00
|
|
|
return defaults({}, {pattern: fastlyConfig.expressPatternToRegex(route.pattern)}, route);
|
2016-05-20 10:33:13 -04:00
|
|
|
});
|
|
|
|
|
2016-04-18 12:32:20 -04:00
|
|
|
async.auto({
|
2016-04-18 14:07:27 -04:00
|
|
|
version: function (cb) {
|
2016-04-18 12:32:20 -04:00
|
|
|
fastly.getLatestVersion(function (err, response) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
// Validate latest version before continuing
|
2016-04-19 13:04:45 -04:00
|
|
|
if (response.active || response.locked) {
|
|
|
|
fastly.cloneVersion(response.number, function (err, response) {
|
|
|
|
if (err) return cb('Failed to clone latest version: ' + err);
|
|
|
|
cb(null, response.number);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb(null, response.number);
|
|
|
|
}
|
2016-04-18 12:32:20 -04:00
|
|
|
});
|
|
|
|
},
|
2017-04-25 11:06:57 -04:00
|
|
|
recvCustomVCL: ['version', function (cb, results) {
|
|
|
|
// For all the routes in routes.json, construct a varnish-style regex that matches
|
|
|
|
// on any of those route conditions.
|
2017-04-25 13:18:13 -04:00
|
|
|
var notPassStatement = fastlyConfig.getAppRouteCondition('../build/*', routes, extraAppRoutes, __dirname);
|
2017-04-25 11:06:57 -04:00
|
|
|
|
|
|
|
// For all the routes in routes.json, construct a varnish-style regex that matches
|
|
|
|
// only if NONE of those routes are matched.
|
2017-04-25 13:18:13 -04:00
|
|
|
var passStatement = fastlyConfig.negateConditionStatement(notPassStatement);
|
2017-04-25 11:06:57 -04:00
|
|
|
|
|
|
|
// For a non-pass condition, point backend at s3
|
2017-04-25 13:18:13 -04:00
|
|
|
var backendCondition = fastlyConfig.setBackend(
|
2017-04-25 11:06:57 -04:00
|
|
|
'F_s3',
|
|
|
|
S3_BUCKET_NAME,
|
|
|
|
notPassStatement
|
|
|
|
);
|
|
|
|
// For a pass condition, set forwarding headers
|
2017-04-25 13:18:13 -04:00
|
|
|
var forwardCondition = fastlyConfig.setForwardHeaders(passStatement);
|
2017-04-25 11:06:57 -04:00
|
|
|
|
|
|
|
fastly.setCustomVCL(
|
|
|
|
results.version,
|
|
|
|
'recv-condition',
|
|
|
|
backendCondition + forwardCondition,
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
}],
|
|
|
|
fetchCustomVCL: ['version', function (cb, results) {
|
2017-04-25 13:18:13 -04:00
|
|
|
var passStatement = fastlyConfig.negateConditionStatement(
|
|
|
|
fastlyConfig.getAppRouteCondition('../build/*', routes, extraAppRoutes, __dirname)
|
|
|
|
);
|
|
|
|
var ttlCondition = fastlyConfig.setResponseTTL(passStatement);
|
2017-04-25 11:06:57 -04:00
|
|
|
fastly.setCustomVCL(results.version, 'fetch-condition', ttlCondition, cb);
|
2016-04-18 12:32:20 -04:00
|
|
|
}],
|
|
|
|
appRouteRequestConditions: ['version', function (cb, results) {
|
|
|
|
var conditions = {};
|
|
|
|
async.forEachOf(routes, function (route, id, cb2) {
|
|
|
|
var condition = {
|
2017-04-25 13:18:13 -04:00
|
|
|
name: fastlyConfig.getConditionNameForRoute(route, 'request'),
|
2016-04-18 12:32:20 -04:00
|
|
|
statement: 'req.url ~ "' + route.pattern + '"',
|
|
|
|
type: 'REQUEST',
|
2016-04-27 15:27:45 -04:00
|
|
|
// Priority needs to be > 1 to not interact with http->https redirect
|
|
|
|
priority: 10 + id
|
2016-04-18 12:32:20 -04:00
|
|
|
};
|
|
|
|
fastly.setCondition(results.version, condition, function (err, response) {
|
|
|
|
if (err) return cb2(err);
|
|
|
|
conditions[id] = response;
|
|
|
|
cb2(null, response);
|
|
|
|
});
|
|
|
|
}, function (err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
cb(null, conditions);
|
|
|
|
});
|
|
|
|
}],
|
|
|
|
appRouteHeaders: ['version', 'appRouteRequestConditions', function (cb, results) {
|
|
|
|
var headers = {};
|
|
|
|
async.forEachOf(routes, function (route, id, cb2) {
|
2016-04-19 18:42:03 -04:00
|
|
|
if (route.redirect) {
|
|
|
|
async.auto({
|
|
|
|
responseCondition: function (cb3) {
|
|
|
|
var condition = {
|
2017-04-25 13:18:13 -04:00
|
|
|
name: fastlyConfig.getConditionNameForRoute(route, 'response'),
|
2016-04-19 18:42:03 -04:00
|
|
|
statement: 'req.url ~ "' + route.pattern + '"',
|
|
|
|
type: 'RESPONSE',
|
|
|
|
priority: id
|
|
|
|
};
|
|
|
|
fastly.setCondition(results.version, condition, cb3);
|
|
|
|
},
|
|
|
|
responseObject: function (cb3) {
|
|
|
|
var responseObject = {
|
2017-04-25 13:18:13 -04:00
|
|
|
name: fastlyConfig.getResponseNameForRoute(route),
|
2016-04-19 18:42:03 -04:00
|
|
|
status: 301,
|
|
|
|
response: 'Moved Permanently',
|
2017-04-25 13:18:13 -04:00
|
|
|
request_condition: fastlyConfig.getConditionNameForRoute(route, 'request')
|
2016-04-19 18:42:03 -04:00
|
|
|
};
|
|
|
|
fastly.setResponseObject(results.version, responseObject, cb3);
|
|
|
|
},
|
|
|
|
redirectHeader: ['responseCondition', function (cb3, redirectResults) {
|
|
|
|
var header = {
|
2017-04-25 13:18:13 -04:00
|
|
|
name: fastlyConfig.getHeaderNameForRoute(route),
|
2016-04-19 18:42:03 -04:00
|
|
|
action: 'set',
|
|
|
|
ignore_if_set: 0,
|
|
|
|
type: 'RESPONSE',
|
|
|
|
dst: 'http.Location',
|
|
|
|
src: '"' + route.redirect + '"',
|
|
|
|
response_condition: redirectResults.responseCondition.name
|
|
|
|
};
|
|
|
|
fastly.setFastlyHeader(results.version, header, cb3);
|
|
|
|
}]
|
|
|
|
}, function (err, redirectResults) {
|
|
|
|
if (err) return cb2(err);
|
|
|
|
headers[id] = redirectResults.redirectHeader;
|
|
|
|
cb2(null, redirectResults);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var header = {
|
2017-04-25 13:18:13 -04:00
|
|
|
name: fastlyConfig.getHeaderNameForRoute(route, 'request'),
|
2016-04-19 18:42:03 -04:00
|
|
|
action: 'set',
|
|
|
|
ignore_if_set: 0,
|
|
|
|
type: 'REQUEST',
|
|
|
|
dst: 'url',
|
2016-04-21 18:16:49 -04:00
|
|
|
src: '"/' + route.name + '.html"',
|
2016-04-19 18:42:03 -04:00
|
|
|
request_condition: results.appRouteRequestConditions[id].name,
|
|
|
|
priority: 10
|
|
|
|
};
|
|
|
|
fastly.setFastlyHeader(results.version, header, function (err, response) {
|
|
|
|
if (err) return cb2(err);
|
|
|
|
headers[id] = response;
|
|
|
|
cb2(null, response);
|
|
|
|
});
|
|
|
|
}
|
2016-04-18 12:32:20 -04:00
|
|
|
}, function (err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
cb(null, headers);
|
2016-04-18 14:07:27 -04:00
|
|
|
});
|
2016-04-18 12:32:20 -04:00
|
|
|
}]},
|
2016-04-19 13:04:45 -04:00
|
|
|
function (err, results) {
|
2016-04-18 12:32:20 -04:00
|
|
|
if (err) throw new Error(err);
|
2016-04-19 13:04:45 -04:00
|
|
|
if (process.env.FASTLY_ACTIVATE_CHANGES) {
|
|
|
|
fastly.activateVersion(results.version, function (err, response) {
|
|
|
|
if (err) throw new Error(err);
|
|
|
|
process.stdout.write('Successfully configured and activated version ' + response.number + '\n');
|
2016-04-29 15:40:44 -04:00
|
|
|
fastly.purgeAll(FASTLY_SERVICE_ID, function (err) {
|
|
|
|
if (err) throw new Error(err);
|
|
|
|
process.stdout.write('Purged all.\n');
|
|
|
|
});
|
2016-04-19 13:04:45 -04:00
|
|
|
});
|
|
|
|
}
|
2016-04-16 12:49:48 -04:00
|
|
|
}
|
2016-04-18 12:32:20 -04:00
|
|
|
);
|