@@ -8,21 +8,15 @@ import (
88 "net/http"
99 "net/http/httptest"
1010 "net/http/httputil"
11- "net/url"
1211 "time"
1312)
1413
1514func main () {
1615 addr := flag .String ("addr" , ":19091" , "HTTP network address" )
17- up := flag .String ("up" , "" , "upstream address for proxy address, if not set, there is no upstream and the sink returns always with 200" )
1816 sleep := flag .Duration ("sleep" , 0 , "sleeptime for non proxy function" )
1917 flag .Parse ()
2018
21- if * up == "" {
22- http .HandleFunc ("/" , rootHandler (* sleep ))
23- } else {
24- http .HandleFunc ("/" , proxyHandler (* up ))
25- }
19+ http .HandleFunc ("/" , rootHandler (* sleep ))
2620
2721 log .Printf ("Starting server on %s\n " , * addr )
2822
@@ -35,26 +29,7 @@ func main() {
3529func rootHandler (sleep time.Duration ) http.HandlerFunc {
3630 return func (w http.ResponseWriter , req * http.Request ) {
3731 time .Sleep (sleep )
38- _ , _ = fmt .Fprintf (w , "<h1>Hello World</h1><div>Welcome to whereever you are</div>" )
39- }
40- }
41-
42- func proxyHandler (target string ) http.HandlerFunc {
43- return func (w http.ResponseWriter , req * http.Request ) {
44- // parse the targetURL
45- targetURL , _ := url .Parse (target )
46-
47- // create the reverse proxy
48- proxy := httputil .NewSingleHostReverseProxy (targetURL )
49-
50- // Update the headers to allow for SSL redirection
51- req .URL .Host = targetURL .Host
52- req .URL .Scheme = targetURL .Scheme
53- req .Header .Set ("X-Forwarded-Host" , req .Header .Get ("Host" ))
54- req .Host = targetURL .Host
55-
56- // Note that ServeHttp is non blocking and uses a go routine under the hood
57- proxy .ServeHTTP (w , req )
32+ _ , _ = fmt .Fprintf (w , "" )
5833 }
5934}
6035
@@ -72,7 +47,7 @@ func logRequest(handler http.Handler) http.Handler {
7247 //log.Printf("%s %s\n", err, string(body))
7348 rec := httptest .NewRecorder ()
7449 handler .ServeHTTP (rec , r )
75- message := fmt .Sprintf ("Request: \n % s\n Response Code: %d\n Response:\n %s\n \n " , string (x ), rec .Code , rec .Body .String ())
50+ message := fmt .Sprintf ("Time:%v \n Url: \n %sRequest: \n % s\n Response Code: %d\n Response:\n %s\n \n " , time . Now (), r . URL . Path , string (x ), rec .Code , rec .Body .String ())
7651 if rec .Code >= 500 {
7752 color .Error .Block (message )
7853 } else if rec .Code >= 400 {
0 commit comments