mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
fix basic-auth-proxy to use cloudflare/fetch
This commit is contained in:
parent
5604a29f06
commit
b7c9d03668
@ -2,9 +2,6 @@ module github.com/syumai/workers/_examples/basic-auth-server
|
||||
|
||||
go 1.21.3
|
||||
|
||||
require (
|
||||
github.com/syumai/tinyutil v0.3.0
|
||||
github.com/syumai/workers v0.5.1
|
||||
)
|
||||
require github.com/syumai/workers v0.5.1
|
||||
|
||||
replace github.com/syumai/workers => ../../
|
||||
|
@ -1,2 +0,0 @@
|
||||
github.com/syumai/tinyutil v0.3.0 h1:sgWeE8oQyequIRLNeHZgR1PddpY4mxcdkfMgx2m53IE=
|
||||
github.com/syumai/tinyutil v0.3.0/go.mod h1:/owCyUs1bh6tKxH7K1Ze3M/zZtZ+vGrj3h82fgNHDFI=
|
@ -1,12 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/syumai/tinyutil/httputil"
|
||||
"github.com/syumai/workers"
|
||||
"github.com/syumai/workers/cloudflare/fetch"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -33,12 +34,20 @@ func handleRequest(w http.ResponseWriter, req *http.Request) {
|
||||
u := *req.URL
|
||||
u.Scheme = "https"
|
||||
u.Host = "syum.ai"
|
||||
resp, err := httputil.Get(u.String())
|
||||
r, err := fetch.NewRequest(req.Context(), req.Method, u.String(), req.Body)
|
||||
if err != nil {
|
||||
handleError(w, http.StatusInternalServerError, "Internal Error")
|
||||
log.Printf("failed to execute proxy request: %v\n", err)
|
||||
return
|
||||
}
|
||||
r.Header = req.Header.Clone()
|
||||
cli := fetch.NewClient()
|
||||
resp, err := cli.Do(r, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
for k, values := range resp.Header {
|
||||
for _, v := range values {
|
||||
w.Header().Add(k, v)
|
||||
|
Loading…
x
Reference in New Issue
Block a user