package main
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"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) {
if r.Method == fsthttp.MethodPost {
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(fsthttp.StatusInternalServerError)
fmt.Fprintln(w, err.Error())
return
}
encodedBody := make([]byte, base64.StdEncoding.EncodedLen(len(body)))
base64.StdEncoding.Encode(encodedBody, body)
fmt.Println(string(encodedBody))
r.Body = io.NopCloser(bytes.NewReader(encodedBody))
}
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)
})
}