-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (42 loc) · 1003 Bytes
/
main.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"io"
"fmt"
"log"
"net/http"
"os"
)
func forwardHandler(w http.ResponseWriter, req *http.Request) {
baseurl := os.Getenv("HTTP_FORWARD_BASEURL")
if baseurl == "" {
log.Fatal("HTTP_FORWARD_BASEURL env variable must be set")
}
forward, err := http.NewRequest(req.Method, baseurl+req.RequestURI, req.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
forward.Header = req.Header
resp, err := http.DefaultClient.Do(forward)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
for k, v := range resp.Header {
w.Header()[k] = v
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
func main() {
port := os.Getenv("HTTP_FORWARD_PORT")
if port == "" {
port = "8080"
}
port = fmt.Sprintf(":%s", port)
http.HandleFunc("/", forwardHandler)
if err := http.ListenAndServe(port, nil); err != nil {
log.Fatal("Failed to start server:", err)
}
}