mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 01:39: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
|
go 1.21.3
|
||||||
|
|
||||||
require (
|
require github.com/syumai/workers v0.5.1
|
||||||
github.com/syumai/tinyutil v0.3.0
|
|
||||||
github.com/syumai/workers v0.5.1
|
|
||||||
)
|
|
||||||
|
|
||||||
replace github.com/syumai/workers => ../../
|
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/syumai/tinyutil/httputil"
|
|
||||||
"github.com/syumai/workers"
|
"github.com/syumai/workers"
|
||||||
|
"github.com/syumai/workers/cloudflare/fetch"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -33,12 +34,20 @@ func handleRequest(w http.ResponseWriter, req *http.Request) {
|
|||||||
u := *req.URL
|
u := *req.URL
|
||||||
u.Scheme = "https"
|
u.Scheme = "https"
|
||||||
u.Host = "syum.ai"
|
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 {
|
if err != nil {
|
||||||
handleError(w, http.StatusInternalServerError, "Internal Error")
|
handleError(w, http.StatusInternalServerError, "Internal Error")
|
||||||
log.Printf("failed to execute proxy request: %v\n", err)
|
log.Printf("failed to execute proxy request: %v\n", err)
|
||||||
return
|
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 k, values := range resp.Header {
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
w.Header().Add(k, v)
|
w.Header().Add(k, v)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user