package main
import (
"context"
"fmt"
"io"
"net"
"strconv"
"github.com/fastly/compute-sdk-go/fsthttp"
"github.com/fastly/compute-sdk-go/geo"
)
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
client_geo, err := geo.Lookup(net.ParseIP(r.RemoteAddr))
if err != nil {
fmt.Print(err)
w.WriteHeader(fsthttp.StatusInternalServerError)
fmt.Fprint(w, "GEO Lookup Error: ", err)
return
}
r.Header.Add("client-ip", r.RemoteAddr)
r.Header.Add("client-geo-city", client_geo.City)
r.Header.Add("client-geo-continent", client_geo.ContinentCode)
r.Header.Add("client-geo-country", client_geo.CountryName)
r.Header.Add("client-geo-latitude", strconv.FormatFloat(client_geo.Latitude, 'f', -1, 64))
r.Header.Add("client-geo-longitude", strconv.FormatFloat(client_geo.Longitude, 'f', -1, 64))
r.CacheOptions.Pass = true
resp, err := r.Send(ctx, "origin_0")
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err)
return
}
w.Header().Reset(resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
return
})
}