package main
import (
"context"
"fmt"
"io"
"os"
"sync"
"github.com/fastly/compute-sdk-go/fsthttp"
)
const BackendTest = "origin_0"
const BackendProd = "origin_1"
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
var (
err error
respTest *fsthttp.Response
respProd *fsthttp.Response
wg sync.WaitGroup
)
wg.Add(2)
rc := r.Clone()
go func() {
defer wg.Done()
var err error
respTest, err = r.Send(ctx, BackendTest)
if err != nil {
fmt.Fprintf(os.Stderr, "test backend: %s\n", err)
}
}()
go func() {
defer wg.Done()
respProd, err = rc.Send(ctx, BackendProd)
}()
wg.Wait()
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err)
return
}
sameStatus := respTest.StatusCode == respProd.StatusCode
sameContent := respTest.Header.Get("Content-Length") == respProd.Header.Get("Content-Length")
if sameStatus && sameContent {
fmt.Println("Prod and test are the same")
} else {
fmt.Println("The production and test environments returned different content")
}
w.Header().Reset(respProd.Header)
w.WriteHeader(respProd.StatusCode)
io.Copy(w, respProd.Body)
})
}