Skip to content

Commit

Permalink
handle subdirs and default index
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoff Kendal committed Feb 5, 2024
1 parent 8ab239a commit feca52c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
21 changes: 20 additions & 1 deletion terraform/cloudfront.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ resource "aws_cloudfront_distribution" "www" {
cached_methods = ["GET", "HEAD"]
target_origin_id = aws_s3_bucket.www.bucket_regional_domain_name

function_association {
event_type = "viewer-request"
function_arn = aws_cloudfront_function.index_rewrite.arn
}

forwarded_values {
query_string = true

Expand Down Expand Up @@ -90,4 +95,18 @@ data "aws_iam_policy_document" "cloudfront_oac_access" {
values = [aws_cloudfront_distribution.www.arn]
}
}
}
}


resource "aws_cloudfront_function" "index_rewrite" {
name = "stumblefunk-org-uk-index-rewrite-${var.environment}"
runtime = "cloudfront-js-2.0"
comment = "Index.html rewrite"
publish = true
code = file("${path.module}/src/index_rewrite.js")

lifecycle {
create_before_destroy = true
}
}

2 changes: 1 addition & 1 deletion terraform/lambda.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
data "archive_file" "lambda" {
type = "zip"
source_file = "lambda/lambda.py"
source_file = "src/lambda.py"
output_path = "lambda_function_payload.zip"
}

Expand Down
20 changes: 9 additions & 11 deletions terraform/public_html/artist-info/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Stumblefunk</title>
</head>

<body>
<h1>Stumblefunk</h1>
<p>Le fluff diabolique!</p>
</body>

</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; url='https://forms.gle/u7Bj9rmMAY7n4pBZ6'" />
</head>
<body>
<p>Loading <a href="https://forms.gle/u7Bj9rmMAY7n4pBZ6">artist-info</a></p>
</body>
</html>
22 changes: 22 additions & 0 deletions terraform/src/index_rewrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function handler(event) {
var request = event.request;
var uri = request.uri;

// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
}
// Check whether the URI is missing a file extension.
else if (!uri.includes('.')) {
var response = {
statusCode: 302,
statusDescription: 'Moved Permanently',
headers: {
'location': { value: uri + '/' }
}
};
return response;
}

return request;
}
File renamed without changes.
File renamed without changes.

0 comments on commit feca52c

Please sign in to comment.