- main
- manifest
- deps
- main
- Install
- Run
package main
import (
"context"
"fmt"
"io"
"strings"
"github.com/fastly/compute-sdk-go/fsthttp"
)
const Backend = "origin_0"
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
apex := "www."
if !strings.HasPrefix(r.URL.Host, apex) {
w.Header().Add("Location", fmt.Sprintf("%s%s", apex, r.URL.Host))
w.WriteHeader(fsthttp.StatusPermanentRedirect)
return
}
resp, err := r.Send(ctx, 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)
})
}