Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an API route and rewrites to fetch sitemaps from an S3 bucket #234

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/constants/bucket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const DC_SITEMAP_BUCKET = process.env.NEXT_PUBLIC_DC_SITEMAP_BUCKET;

export { DC_SITEMAP_BUCKET };
47 changes: 47 additions & 0 deletions pages/api/sitemap/[filename].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { NextApiRequest, NextApiResponse } from "next";
import axios, { AxiosError, AxiosResponse } from "axios";
import { DC_SITEMAP_BUCKET } from "@/lib/constants/bucket";

class NotFound extends Error {
constructor() {
super("Not Found");
}
}

const getObject = async (filename: string): Promise<AxiosResponse> => {
if (!DC_SITEMAP_BUCKET || !filename) throw new NotFound();
try {
return await axios(`http://${DC_SITEMAP_BUCKET}/${filename}`, { responseType: "stream" });
} catch (err) {
console.warn('caught in getObject', err);
if (err instanceof AxiosError) throw new NotFound();
throw err;
}
};

export default async function handler(req: NextApiRequest, res: NextApiResponse): Promise<void> {
const { filename } = req.query;
try {
const response: AxiosResponse = await getObject(filename as string);

res
.status(200)
.setHeader("Content-Type", response.headers["content-type"] as string)
.setHeader("Content-Length", response.headers["content-length"] as string)
.setHeader("ETag", response.headers["etag"] as string)
.setHeader("Last-Modified", response.headers["last-modified"] as string);
response.data.pipe(res);
} catch (err) {
if (err instanceof NotFound) {
res.status(404).end("Not Found");
} else {
throw err;
}
}
}

export const config = {
api: {
responseLimit: false,
},
};
9 changes: 0 additions & 9 deletions public/sitemap.xml

This file was deleted.

31 changes: 24 additions & 7 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,39 @@ resource "aws_amplify_app" "dc-next" {

lifecycle {
ignore_changes = [
custom_rule,
basic_auth_credentials
]
}

environment_variables = {
ENV = var.environment_name
HONEYBADGER_API_KEY = var.honeybadger_api_key
HONEYBADGER_ENV = var.environment_name
NEXT_PUBLIC_DCAPI_ENDPOINT = var.next_public_dcapi_endpoint
NEXT_PUBLIC_DC_URL = var.next_public_dc_url
ENV = var.environment_name
HONEYBADGER_API_KEY = var.honeybadger_api_key
HONEYBADGER_ENV = var.environment_name
NEXT_PUBLIC_DCAPI_ENDPOINT = var.next_public_dcapi_endpoint
NEXT_PUBLIC_DC_URL = var.next_public_dc_url
NEXT_PUBLIC_DC_SITEMAP_BUCKET = aws_s3_bucket_website_configuration.sitemap_website.website_endpoint
}

custom_rule {
source = "/sitemap.xml"
target = "/api/sitemap/sitemap.xml"
status = 200
}

custom_rule {
source = "/sitemap.xml.gz"
target = "/api/sitemap/sitemap.xml.gz"
status = 200
}

custom_rule {
source = "/sitemap-<*>"
target = "/api/sitemap/sitemap-<*>"
status = 200
}
}

resource "aws_iam_role" "dc_next_amplify_role" {

name = "${var.project}-role"
assume_role_policy = join("", data.aws_iam_policy_document.assume_role.*.json)
managed_policy_arns = ["arn:aws:iam::aws:policy/AdministratorAccess-Amplify"]
Expand Down
28 changes: 28 additions & 0 deletions terraform/sitemap_bucket.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
data "aws_iam_policy_document" "allow_sitemap_bucket_access_from_cloudfront" {
statement {
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.sitemap_bucket.arn}/*"]

principals {
type = "AWS"
identifiers = ["*"]
}
}
}

resource "aws_s3_bucket_policy" "sitemap_bucket" {
bucket = aws_s3_bucket.sitemap_bucket.id
policy = data.aws_iam_policy_document.allow_sitemap_bucket_access_from_cloudfront.json
}

resource "aws_s3_bucket" "sitemap_bucket" {
bucket = "${var.project}-${var.environment_name}-sitemaps"
}

resource "aws_s3_bucket_website_configuration" "sitemap_website" {
bucket = aws_s3_bucket.sitemap_bucket.bucket

index_document {
suffix = "sitemap.xml"
}
}