Last active
December 26, 2015 20:19
-
-
Save JohnMurray/7207746 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"log" | |
"net/http" | |
"net/url" | |
) | |
var ( | |
listen = flag.String("listen", "0.0.0.0:9000", "listen on address") | |
logp = flag.Bool("log", false, "enable logging") | |
) | |
func main() { | |
flag.Parse() | |
proxyHandler := http.HandlerFunc(proxyHandlerFunc) | |
log.Fatal(http.ListenAndServe(*listen, proxyHandler)) | |
log.Println("Started router-server on 0.0.0.0:9000") | |
} | |
func proxyHandlerFunc(w http.ResponseWriter, r *http.Request) { | |
// Log if requested | |
if *logp { | |
log.Println(r.URL) | |
} | |
/* | |
* Tweak the request as appropriate: | |
* - RequestURI may not be sent to client | |
* - Set new URL | |
*/ | |
r.RequestURI = "" | |
u, err := url.Parse("http://localhost:8080/") | |
if err != nil { | |
log.Fatal(err) | |
} | |
r.URL = u | |
// And proxy | |
// resp, err := client.Do(r) | |
c := make(chan *http.Response) | |
go doRequest(c) | |
resp := <-c | |
if resp != nil { | |
resp.Write(w) | |
} | |
} | |
func doRequest(c chan *http.Response) { | |
// new client for every request. | |
client := &http.Client{} | |
resp, err := client.Get("http://127.0.0.1:8080/test") | |
if err != nil { | |
log.Println(err) | |
c <- nil | |
} else { | |
c <- resp | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
http.createServer(function (req, res) { | |
// console.log("received request"); | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello World\n'); | |
}).listen(8080, '127.0.0.1'); | |
console.log('Server running at http://127.0.0.1:8080/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment