Demo for inlining NetStorage hosted JSON in HTML with Akamai EdgeWorkers
Keyword(s): response-provider, api-orchestration, circleci
This example demonstrates how EdgeWorkers can be used to merge an internal call to Akamai NetStorage to retrieve some data and inline it into the HTML for the final response to the user. This example is based on the find-replace-stream use-case in the official Akamai repository.
This EdgeWorker fetches (httpRequest
function) the contents of a file hosted in NetStorage. The Akamai property is configured to route the request to NetStorage by using the same hostname from the original request.In this use-case the content is a JSON string, however it will be treated as a string and not as a JSON object.
{"unicorns": "awesome", "abc": [4, 5, 6], "careful": "to escape ' quotes"}
In the EdgeWorker the above string is assembled to form a data class json-data to inline in the HTML.
<data class="json-data" value='{"unicorns": "awesome", "abc": [4, 5, 6], "careful": "to escape ' quotes"}'></data>
- When NetStorage is used as an origin a path rewrite occurs to prepend /cpcode/ which the EdgeWorker will use by default.
- NetStorage is for static assets so a mininmum caching time of 10 min is required. This is configurable in the Akamai property.
Based on the Turbo Akamai Edgeworkers project. CircleCI can automate the deployment to staging for this EdgeWorker on each git commit
. The asociated configuration is in the .circleci.yml
file.
CircleCI Contexts is are used to pass on sensitive information in variables to the configuration file .circleci.yml
. The following variables should be managed under CirecleCI Contexts for this example.
- $ACCOUNTKEY: Optional. Used to switch between accounts. If not needed remove all associations and the
--accountkey
flag from the commands. - $HOSTNAME: Hostname associated to the property running EdegeWorkers. Used to create the enhanced debugging token.
- $ACCESS_TOKEN
- $HOST
- $CLIENT_SECRET
- $CLIENT_TOKEN
SubRequests being made from EdgeWorkers can open up numerous possibilities to generate responses for APIs.