- main
- manifest
- deps
- main
- Install
- Run
package main
import (
"context"
"fmt"
"io"
"regexp"
"strings"
"github.com/fastly/compute-sdk-go/fsthttp"
)
const BackendName = "origin_0"
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
re := regexp.MustCompile(`/$`)
if(re.Match([]byte(r.URL.Path))){
NormURL := re.ReplaceAllLiteralString(r.URL.Path, "")
h := fsthttp.NewHeader()
h.Add("Location", NormURL)
resp := &fsthttp.Response{
Header: h,
StatusCode: fsthttp.StatusTemporaryRedirect,
Body: io.NopCloser(strings.NewReader("")),
}
flush(resp, w)
return
}
r.URL.Path = re.ReplaceAllLiteralString(r.URL.Path, "")
resp, err := r.Send(ctx, BackendName)
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err.Error())
return
}
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)
}