aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-03-23 00:54:04 +0800
committerRunxi Yu <me@runxiyu.org>2025-03-23 00:54:04 +0800
commit584c969a7f122f29e6fed923586333ab38cb3d6a (patch)
tree13f7609ce25c63748cc2511d253f253ccbb454b2 /main.go
parentInitial commit (diff)
downloadpowxy-584c969a7f122f29e6fed923586333ab38cb3d6a.tar.gz
powxy-584c969a7f122f29e6fed923586333ab38cb3d6a.tar.zst
powxy-584c969a7f122f29e6fed923586333ab38cb3d6a.zip
Basic reverse proxy
Diffstat (limited to '')
-rw-r--r--main.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..3a0df60
--- /dev/null
+++ b/main.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+ "io"
+ "log"
+ "maps"
+ "net/http"
+)
+
+var client = http.Client{
+ CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
+}
+
+func main() {
+ log.Fatal(http.ListenAndServe(":8081", http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
+ log.Println(request.RemoteAddr, request.RequestURI)
+
+ request.Host = "127.0.0.1:8080"
+ request.URL.Host = "127.0.0.1:8080"
+ request.URL.Scheme = "http"
+ request.RequestURI = ""
+
+ response, err := client.Do(request)
+ if err != nil {
+ http.Error(writer, err.Error(), http.StatusBadGateway)
+ return
+ }
+
+ maps.Copy(writer.Header(), response.Header)
+ writer.WriteHeader(response.StatusCode)
+ io.Copy(writer, response.Body)
+ })))
+}