aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 3a0df60eaa9e902cf83c183c23fb009bc6f7d1b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)
	})))
}