fix basic-auth-proxy to use cloudflare/fetch

This commit is contained in:
syumai 2024-01-24 23:58:02 +09:00
parent 5604a29f06
commit b7c9d03668
3 changed files with 12 additions and 8 deletions

View File

@ -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 => ../../

View File

@ -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=

View File

@ -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)