// https://developer.fastly.com/solutions/examples/prohibit-browser-caching

package main

import (
	"context"
	"fmt"
	"io"

	"github.com/fastly/compute-sdk-go/fsthttp"
)

// BackendName is the name of our service backend.
const BackendName = "origin_0"

func main() {
	fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
		// This requires your service to be configured with a backend
		// named "origin_0" and pointing to "https://http-me.glitch.me".

		resp, err := r.Send(ctx, BackendName)
		if err != nil {
			w.WriteHeader(fsthttp.StatusBadGateway)
			fmt.Fprintln(w, err.Error())
			return
		}

    // Cache-Control: no-store prevents browsers from writing the object
    // to disk.  We add 'private' in case there are any public caches
    // downstream of Fastly, between us and the browser, which might
    // perform request collapsing on public content.
    resp.Header.Set("cache-control", "private, no-store")

    // Remove all other caching-related response headers.
    resp.Header.Del("expires")
    resp.Header.Del("etag")
    resp.Header.Del("last-modified")

		w.Header().Reset(resp.Header)
		w.WriteHeader(resp.StatusCode)
		io.Copy(w, resp.Body)
	})
}