package main
import (
"context"
"fmt"
"io"
"strconv"
"github.com/fastly/compute-sdk-go/edgedict"
"github.com/fastly/compute-sdk-go/fsthttp"
)
const Backend = "origin_0"
const DictName = "ttls"
const DefaultTTL = 60
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
d, err := edgedict.Open(DictName)
if err != nil {
w.WriteHeader(fsthttp.StatusInternalServerError)
fmt.Fprintln(w, err)
return
}
r.CacheOptions.TTL = DefaultTTL
if v, _ := d.Get(r.URL.Path); v != "" {
if i, err := strconv.ParseUint(v, 10, 32); err == nil {
r.CacheOptions.TTL = uint32(i)
}
}
fmt.Println("TTL:", r.CacheOptions.TTL)
resp, err := r.Send(ctx, Backend)
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)
})
}