// https://developer.fastly.com/solutions/examples/failover-to-a-secondary-backend
package main

import (
	"context"
	"fmt"
	"io"

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

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

// BackendFailover is the name of our failover service backend.
const BackendFailover = "origin_1"

func main() {
	fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
		// For non-GET/HEAD methods just proxy the request with no special logic.
		if r.Method != fsthttp.MethodGet && r.Method != fsthttp.MethodHead {
			resp, err := r.Send(ctx, Backend)
			if err != nil {
				w.WriteHeader(fsthttp.StatusBadGateway)
				fmt.Fprintln(w, err)
				return
			}

			// Write response to client.
			flush(resp, w)
			return
		}

		// Send the request to the named primary backend.
		resp, err := r.Send(ctx, Backend)
		if err != nil || isErrStatus(resp.StatusCode) {
			// A request can't call .Send() twice.
			rc := r.Clone()

			// We are now going to modify the cloned request.
			// For the purposes of this example, we change the request path to
			// something that we expect to succeed for the failover backend.
			rc.URL.Path = "/"

			// Send the request to the named failover backend.
			resp, err = rc.Send(ctx, BackendFailover)
			if err != nil {
				w.WriteHeader(fsthttp.StatusBadGateway)
				fmt.Fprintln(w, err)
				return
			}
		}

		// Write response to client.
		flush(resp, w)
	})
}

func flush(resp *fsthttp.Response, w fsthttp.ResponseWriter) {
	w.Header().Reset(resp.Header)
	w.WriteHeader(resp.StatusCode)
	io.Copy(w, resp.Body)
}

func isErrStatus(statusCode int) bool {
	return statusCode == fsthttp.StatusForbidden || (statusCode >= 500 && statusCode < 600)
}