diff options
author | Kenny Ballou <kballou@devnulllabs.io> | 2020-01-14 18:53:39 -0700 |
---|---|---|
committer | Kenny Ballou <kballou@devnulllabs.io> | 2020-01-14 18:53:39 -0700 |
commit | 1bb882edd5c1745d1a1bd4cc12e30fcbd8f81be9 (patch) | |
tree | 235c07e9061b23f81e6ea62ab0c2a582a87e3b64 | |
parent | 6a2fd28da9a3f70ae8c784edc0a521a0fde3c0e7 (diff) | |
download | kennyballou.com-1bb882edd5c1745d1a1bd4cc12e30fcbd8f81be9.tar.gz kennyballou.com-1bb882edd5c1745d1a1bd4cc12e30fcbd8f81be9.tar.xz |
uri-rewrite: rewrite function in python
Now that python3.7 is available as a lambda@edge runtime environment,
switch to the URI rewrite lambda function.
Signed-off-by: Kenny Ballou <kballou@devnulllabs.io>
-rw-r--r-- | stacks/blog.tpl | 2 | ||||
-rw-r--r-- | stacks/uri-rewrite.in | 43 |
2 files changed, 27 insertions, 18 deletions
diff --git a/stacks/blog.tpl b/stacks/blog.tpl index 6ab65d1..454540c 100644 --- a/stacks/blog.tpl +++ b/stacks/blog.tpl @@ -176,7 +176,7 @@ "Handler": "index.handler", "MemorySize": 128, "Role": {"Fn::GetAtt": ["URIRewriteLambdaRole", "Arn"]}, - "Runtime": "nodejs8.10", + "Runtime": "python3.7", "Tags": [ {"Key": "Domain", "Value": {"Ref": "DomainName"}} ] diff --git a/stacks/uri-rewrite.in b/stacks/uri-rewrite.in index 83a391d..e474cec 100644 --- a/stacks/uri-rewrite.in +++ b/stacks/uri-rewrite.in @@ -1,25 +1,34 @@ [+ autogen5 template -*- mode: json -*- +] {"Fn::Join": ["\n", [ - "'use strict';", - "exports.handler = (event, context, callback) => {", - " var whitelist = [", - " 'html',", + "def handler(event, _context):", + " whitelist = [", + " 'asc',", " 'css',", + " 'gif',", + " 'html',", + " 'ico',", + " 'jpeg',", " 'jpg',", - " 'svg',", + " 'js',", + " 'json',", + " 'map',", + " 'md',", + " 'ogg',", + " 'pdf',", " 'png',", + " 'pug',", + " 'sass',", + " 'scss',", + " 'svg',", " 'txt',", " 'xml',", - " 'pdf',", - " 'ico',", - " 'ogg',", - " 'asc'", - " ];", - " var request = event.Records[0].cf.request;", - " var extension = request.uri.split('.').pop();", - " if (typeof extension == 'undefined' || !whitelist.includes(extension)) {", - " request.uri = request.uri.replace(/\\\/?$/, '\/index.html');", - " }", - " return callback(null, request);", - "};" + " ]", + " request = event['Records'][0]['cf']['request']", + " extension = request['uri'].split('.')[-1]", + " if extension is None or extension not in whitelist:", + " if request['uri'][-1] == '/':", + " request['uri'] += 'index.html'", + " else:", + " request['uri'] += '/index.html'", + " return request" ]]} |