package main

import (
	"context"
	"fmt"
	"io"
	"time"

	"github.com/fastly/compute-sdk-go/fsthttp"
)
func main() {
	opts := fsthttp.NewBackendOptions()
	opts.HostOverride("http-me.glitch.me")
	opts.ConnectTimeout(time.Duration(1) * time.Second)
	opts.FirstByteTimeout(time.Duration(15) * time.Second)
	opts.BetweenBytesTimeout(time.Duration(10) * time.Second)
	opts.UseSSL(true)
	fsthttp.RegisterDynamicBackend("my_backend", "http-me.glitch.me", opts)

	fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {

		resp, err := r.Send(ctx, "my_backend")
		if err != nil {
			w.WriteHeader(fsthttp.StatusBadGateway)
			fmt.Fprintln(w, err)
			return
		}

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