Skip to content

Move API docs redirects to CloudFront #366

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 6 additions & 8 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ dist-docs_versions: $(OUTPUT_DIR)/versions-$(CRYSTAL_VERSION).json ## Update `/v
$(OUTPUT_DIR)/versions-$(CRYSTAL_VERSION).json: $(OUTPUT_DIR)/$(OUTPUT_DOCS_BASE_NAME).tar.gz

.PHONY: dist-redirect_latest
dist-redirect_latest: $(OUTPUT_DIR)/aws-config.json ## Apply redirect from `api/latest` to `api/$(CRYSTAL_VERSION)` in S3 (needs to be manually applied)
$(AWS_CLI) s3api put-bucket-website --bucket "$(AWS_BUCKET)" --website-configuration "file://$<"
dist-redirect_latest: MATCH_CURRENT_ETAG = --name crystal-api-docs-redirects --if-match "$$($(AWS_CLI) cloudfront describe-function --name crystal-api-docs-redirects --output text --query 'ETag')"
dist-redirect_latest: $(OUTPUT_DIR)/aws-cloudfront-redirect.js ## Apply redirect from `api/latest` to `api/$(CRYSTAL_VERSION)` in CloudFront (needs to be manually applied)
$(AWS_CLI) cloudfront update-function $(MATCH_CURRENT_ETAG) --function-config '{"Runtime":"cloudfront-js-2.0","Comment":"Redirects for Crystal API docs $(CRYSTAL_VERSION)"}' --function-code "$(shell base64 -i $(OUTPUT_DIR)/aws-cloudfront-redirect.js)"
$(AWS_CLI) cloudfront publish-function $(MATCH_CURRENT_ETAG)

.PHONY: get-website-configuration
get-website-configuration: ## Pull website configuration from S3 bucket (run as `make pull-website-configuration -s > aws-config.json`)
$(AWS_CLI) s3api get-bucket-website --bucket "$(AWS_BUCKET)"

.PHONY: $(OUTPUT_DIR)/aws-config.json
$(OUTPUT_DIR)/aws-config.json: aws-config.json $(OUTPUT_DIR)
.PHONY: $(OUTPUT_DIR)/aws-cloudfront-redirect.js
$(OUTPUT_DIR)/aws-cloudfront-redirect.js: aws-cloudfront-redirect.js $(OUTPUT_DIR)
sed 's/$${CRYSTAL_VERSION}/$(CRYSTAL_VERSION)/g' "$<" > $@

.PHONY: help
Expand Down
41 changes: 41 additions & 0 deletions docs/aws-cloudfront-redirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Since CloudFront doesn't run functions when the origin returns a statusCode >=400
// this function has to run upon "viewer request", before hitting the origin

function redirect(path) {
return {
statusCode: 302,
headers: {
'location': { value: path }
}
}
}

const LATEST_INDEX_REDIRECTION_PATHS = [ '/api', '/api/', '/api/index.html', '/api/latest']

function handler(event) {
var requestPath = event.request.uri

// well-known paths that should be queried as-is
if(
requestPath == '/api/versions.json' ||
requestPath.startsWith('/api/1.') ||
requestPath.startsWith('/api/0.') ||
requestPath == '/api/master' ||
requestPath.startsWith('/api/master/')
) {
return event.request
}

// prefixes that go to the current version's index
if(LATEST_INDEX_REDIRECTION_PATHS.includes(requestPath)) {
return redirect('/api/${CRYSTAL_VERSION}/')
}

// "latest" prefix should be re-written to the current version
if(requestPath.startsWith('/api/latest/')) {
return redirect(requestPath.replace('/api/latest/', '/api/${CRYSTAL_VERSION}/'))
}

// at this point, we assume the request omited the version - eg, `/api/String.html`
return redirect(requestPath.replace('/api/', '/api/${CRYSTAL_VERSION}/'))
}
57 changes: 0 additions & 57 deletions docs/aws-config.json

This file was deleted.