mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
45 lines
853 B
Go
45 lines
853 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"time"
|
|
|
|
"github.com/syumai/workers"
|
|
"github.com/syumai/workers/cloudflare"
|
|
"github.com/syumai/workers/cloudflare/fetch"
|
|
)
|
|
|
|
func handler(w http.ResponseWriter, req *http.Request) {
|
|
cloudflare.PassThroughOnException()
|
|
|
|
// logging after responding
|
|
cloudflare.WaitUntil(func() {
|
|
for i := 0; i < 5; i++ {
|
|
time.Sleep(time.Second)
|
|
}
|
|
fmt.Println("5-second task completed")
|
|
})
|
|
|
|
// panic if x-error header has provided
|
|
if req.Header.Get("x-error") != "" {
|
|
panic("error")
|
|
}
|
|
|
|
// responds with origin server
|
|
fc := fetch.NewClient()
|
|
proxy := httputil.ReverseProxy{
|
|
Transport: fc.HTTPClient(fetch.RedirectModeManual).Transport,
|
|
Director: func(r *http.Request) {
|
|
r.URL = req.URL
|
|
},
|
|
}
|
|
|
|
proxy.ServeHTTP(w, req)
|
|
}
|
|
|
|
func main() {
|
|
workers.Serve(http.HandlerFunc(handler))
|
|
}
|