Simplify how the conditional is created for setting the S3 backend, caching...

Simplify how the conditional is created for setting the S3 backend, caching related behavior for non-logged in users and maintaining language settings and passing to backend
parent 7a6be030
var async = require('async'); var async = require('async');
var defaults = require('lodash.defaults'); var defaults = require('lodash.defaults');
var fastlyConfig = require('./lib/fastly-config-methods'); var fastlyConfig = require('./lib/fastly-config-methods');
const languages = require('./languages.json');
var route_json = require('../src/routes.json'); var route_json = require('../src/routes.json');
...@@ -46,18 +47,42 @@ async.auto({ ...@@ -46,18 +47,42 @@ async.auto({
var passStatement = fastlyConfig.negateConditionStatement(notPassStatement); var passStatement = fastlyConfig.negateConditionStatement(notPassStatement);
// For a non-pass condition, point backend at s3 // For a non-pass condition, point backend at s3
var backendCondition = fastlyConfig.setBackend( var recvCondition = '' +
'F_s3', 'if (' + notPassStatement + ') {\n' +
S3_BUCKET_NAME, ' set req.backend = F_s3;\n' +
notPassStatement ' set req.http.host = \"' + S3_BUCKET_NAME + '\";\n' +
); '} else {\n' +
// For a pass condition, set forwarding headers ' if (!req.http.Fastly-FF) {\n' +
var forwardCondition = fastlyConfig.setForwardHeaders(passStatement); ' if (req.http.X-Forwarded-For) {\n' +
' set req.http.Fastly-Temp-XFF = req.http.X-Forwarded-For \", \" client.ip;\n' +
' } else {\n' +
' set req.http.Fastly-Temp-XFF = client.ip;\n' +
' }\n' +
' } else {\n' +
' set req.http.Fastly-Temp-XFF = req.http.X-Forwarded-For;\n' +
' }\n' +
' set req.grace = 60s;\n' +
' if (req.http.Cookie:scratchlanguage) {\n' +
' set req.http.Accept-Language = req.http.Cookie:scratchlanguage;\n' +
' } else {\n' +
' set req.http.Accept-Language = accept.language_lookup("' +
Object.keys(languages).join(':') + '", ' +
'"en", ' +
'std.tolower(req.http.Accept-Language)' +
');\n' +
' }\n' +
' if (req.url ~ "^/projects/" && !req.http.Cookie:scratchsessionid) {\n' +
' set req.http.Cookie = req.http.Cookie:scratchlanguage;\n' +
' } else {\n' +
' return(pass);\n' +
' }\n' +
'}\n';
fastly.setCustomVCL( fastly.setCustomVCL(
results.version, results.version,
'recv-condition', 'recv-condition',
backendCondition + forwardCondition, recvCondition,
cb cb
); );
}], }],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment