package main
import (
"context"
"fmt"
"io"
"net"
"github.com/fastly/compute-sdk-go/fsthttp"
"github.com/fastly/compute-sdk-go/geo"
)
const BackendName = "http_me"
const ChunkSize = 0.1
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
if ChunkSize > 0 {
clientIP := net.ParseIP(r.RemoteAddr)
geo, err := geo.Lookup(clientIP)
if err != nil {
w.WriteHeader(fsthttp.StatusInternalServerError)
fmt.Fprintln(w, err.Error())
return
}
GridID := fmt.Sprintf("%.1f,%.1f", geo.Latitude, geo.Longitude)
fmt.Println("Grid ID:", GridID, "Nearest city:", geo.City)
r.Header.Add("geo-grid-id", GridID)
}
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)
})
}