package main

import (
	"context"
	"fmt"
	"io"
  "regexp"

	"github.com/fastly/compute-sdk-go/fsthttp"
)

// BackendName is the name of our service backend.
const BackendName = "origin_0"

func main() {
	fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
		// This requires your service to be configured with a backend
		// named "origin_0" and pointing to "https://http-me.glitch.me".

    // Check for known bad User-Agent with case insensitivity
    re := regexp.MustCompile(`(?i)BadBot|SpamBot`)
    UserAgent := r.Header.Get("User-Agent")
    // Print User Agent to console
    fmt.Println(UserAgent)
    if re.MatchString(UserAgent) {
      // Return 403 status error
      fsthttp.Error(w, "StatusForbidden", 403)
    }
		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)
	})
}