package main
import (
"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) {
q := r.URL.Query()
userValue := q.Get("user")
pwValue := q.Get("pw")
if userValue != "" && pwValue != "" {
basic_auth := base64.StdEncoding.EncodeToString([]byte(userValue + ":" + pwValue))
r.Header.Add("Authorization", "Basic "+basic_auth)
q.Del("user")
q.Del("pw")
r.URL.RawQuery = q.Encode()
}
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)
})
}