0

Code examples in Go

 2 years ago
source link: https://developer.fastly.com/solutions/examples/go/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Code examples in Go

These code examples have an implementation in Go. To learn more about using Go with our Compute@Edge platform, see using Go.

NOTE: Some of the code examples here are also available in languages other than Go. Being able to see how the same solution can be achieved in other languages is often useful for migrations or learning, so where a code example has multiple implementations, we show all implementations together.

Add a new field to a JSON response
Decorate API responses in JSON format with new fields
Geo-IP API at the edge
Create an API endpoint for fetching GeoIP data for the requesting browser, implemented 100% at the edge. The response should show your current approximate location, but no requests to any origin servers.
Add www. to apex hostname and subdomains
Detect requests that don't include a www. prefix, and redirect to the equivalent path on a hostname that starts with www., usually to make sure there's only one canonical location for your content.
Override TTLs for path prefixes
Set TTLs at the edge based on looking up a path prefix in an edge dictionary
Use microservices to divide up a domain
Send request to different origin servers based on the URL path.
Replace origin errors with 'safe' responses
Detect specified response statuses from backends and instead serve a precomposed error page or error content generated at the edge.
Rewrite URL path
Receive a request for one path but request a different path from origin, without a redirect.
Add or remove cookies
Read individual cookies, set new cookies in response.
Smoke test a new origin
Send a copy of your traffic to a test origin before returning a response from production.
Decompress and read gzipped responses
When you need to work on API and text responses from backends that support gzip.
Clean backend responses
Remove headers added by backends that you don't want to emit to the browser, like amz- or goog- headers.
Follow redirects at the edge
Protect clients from redirects by chasing them internally at the edge, and then return the eventual non-redirect response.
Auto retry a secondary backend
If primary backend fails, retry with a different backend without caching the failure or reducing cache efficiency.
Redirect old URLs at the edge
Use a dictionary of URL mappings to serve your redirects at lightning speed.

All code on this page is provided under both the BSD and MIT open source licenses.

Was this page helpful?
😀 Yes😔 No📝 Comment only

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK