package main
import (
"context"
"fmt"
"io"
"strings"
"github.com/fastly/compute-sdk-go/fsthttp"
)
const Backend1 = "origin_0"
const Backend2 = "origin_1"
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
if r.URL.Path == "/" {
resp, err := r.Send(ctx, Backend1)
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err)
return
}
flush(resp, w)
return
}
if strings.HasPrefix(r.URL.Path, "/status") {
resp, err := r.Send(ctx, Backend2)
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err)
return
}
flush(resp, w)
return
}
resp := &fsthttp.Response{
StatusCode: fsthttp.StatusNotFound,
Body: io.NopCloser(strings.NewReader("The page you requested could not be found")),
}
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)
}