Skip to content

Commit

Permalink
fix basic-auth-proxy to use cloudflare/fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
syumai committed Jan 24, 2024
1 parent 5604a29 commit b7c9d03
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
5 changes: 1 addition & 4 deletions _examples/basic-auth-proxy/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ../../
2 changes: 0 additions & 2 deletions _examples/basic-auth-proxy/go.sum
Original file line number Diff line number Diff line change
@@ -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=
13 changes: 11 additions & 2 deletions _examples/basic-auth-proxy/main.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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)
Expand Down

0 comments on commit b7c9d03

Please sign in to comment.