summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-04-23 20:52:16 -0500
committersanine <sanine.not@pm.me>2023-04-23 20:52:16 -0500
commitc3a2afa0246ee1cc34409ad83c9333e2b0cb1019 (patch)
tree40ec797ffb62d9cc0f0d6b4d2002c3fab95b0fc9 /main.go
initial commit
Diffstat (limited to 'main.go')
-rw-r--r--main.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..bd85dcc
--- /dev/null
+++ b/main.go
@@ -0,0 +1,72 @@
+package main
+
+import (
+ "fmt"
+ "io"
+ "strings"
+ "net"
+ "net/http"
+ "net/url"
+ log "github.com/sirupsen/logrus"
+)
+
+
+//type Endpoint struct {
+// path string
+// address string
+//}
+//
+//
+//func proxy(w http.ResponseWriter, req *http.Request, end Endpoint) {
+//}
+//
+//func makeProxiedRequest(w http.ResponseWriter, req *http.Request, end Endpoint) {
+//
+//}
+//
+
+func main() {
+ const addr = "localhost:3333"
+ log.Info("configuring reverse proxy...")
+ origin, err := url.Parse("http://localhost:8000")
+ if err != nil {
+ log.Fatal(err)
+ }
+ log.Infof("listening on %v", addr)
+ http.ListenAndServe(addr, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+ req.Host = origin.Host
+ req.URL.Host = origin.Host
+ req.URL.Scheme = origin.Scheme
+ req.RequestURI = ""
+ // set X-Forwarded-For
+ forwardedFor, _, _ := net.SplitHostPort(req.RemoteAddr)
+ req.Header.Set("X-Forwarded-For", forwardedFor)
+ response, err := http.DefaultClient.Do(req)
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ fmt.Fprintf(w, "%v", err)
+ log.Fatal(err)
+ }
+
+ // copy header
+ for key, values := range response.Header {
+ for _, value := range values {
+ w.Header().Add(key, value)
+ }
+ }
+
+ // get trailer keys
+ trailerKeys := []string{}
+ for key := range response.Trailer {
+ trailerKeys = append(trailerKeys, key)
+ }
+ if (len(trailerKeys) > 0) {
+ w.Header().Set("Trailer", strings.Join(trailerKeys, ","))
+ }
+
+ w.WriteHeader(response.StatusCode)
+
+ // copy body
+ io.Copy(w, response.Body)
+ }))
+}