//https://developer.fastly.com/solutions/examples/add-remove-or-change-http-headers

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) {

    // Remove a header from inbound requests. 
    fsthttp.Header(r.Header).Del("accept-encoding")
    // Add a new header to request
    fsthttp.Header(r.Header).Add("cdn-secret", "9yfncb340-6abf5oa-ejni22jkdg")
    
    resp, err := r.Send(ctx, BackendName)
    if err != nil {
      w.WriteHeader(fsthttp.StatusBadGateway)
      fmt.Fprintln(w, err.Error())
      return
    }

    // Add new header to response
    fsthttp.Header(resp.Header).Add("cache-control", "max-age=60")
    // Remove header from response
    fsthttp.Header(resp.Header).Del("x-powered-by")

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