-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (35 loc) · 981 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
package main
import (
"errors"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
)
func main() {
url, _ := url.Parse(os.Getenv("FORESTRY_URL"))
proxy := httputil.NewSingleHostReverseProxy(url)
proxy.ModifyResponse = func(r *http.Response) error {
if r.StatusCode == http.StatusNotFound {
return errors.New("NOT_FOUND")
}
return nil
}
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
if err.Error() == "NOT_FOUND" {
http.Error(w, "Not found", http.StatusNotFound)
}
}
http.HandleFunc("/http-check", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
req.Host = req.URL.Host
proxy.ServeHTTP(w, req)
})
http.DefaultTransport.(*http.Transport).MaxIdleConns = 0
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 0
http.NotFoundHandler()
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}