package main
import (
"context"
"fmt"
"io"
"os"
"github.com/fastly/compute-sdk-go/fsthttp"
)
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
var FastlyRegion = os.Getenv("FASTLY_REGION")
RegionsToOrigins := map[string]string{
"AF-West": "origin_0",
"APAC": "origin_1",
"Asia": "origin_1",
"Asia-South": "origin_1",
"EU-Central": "origin_0",
"EU-East": "origin_0",
"EU-West": "origin_0",
"North-America": "origin_2",
"SA-East": "origin_2",
"SA-North": "origin_2",
"SA-South": "origin_2",
"South-Africa": "origin_0",
"US-Central": "origin_2",
"US-East": "origin_2",
"US-West": "origin_2",
}
BackendName, found := RegionsToOrigins[FastlyRegion]
if !found {
BackendName = "origin_0"
}
resp, err := r.Send(ctx, BackendName)
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err.Error())
return
}
w.Header().Reset(resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
})
}