Created
November 16, 2016 12:50
-
-
Save braoru/0e44575009cff3d06f2cfae547c26bb8 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"fmt" | |
"net/http" | |
"net/http/httptest" | |
"github.com/vulcand/oxy/forward" | |
"github.com/vulcand/oxy/testutils" | |
"strconv" | |
) | |
// Variables to identify the build | |
var ( | |
Version string | |
Build string | |
) | |
func redirect(hello_string string) http.Handler { | |
fn := func(w http.ResponseWriter, r *http.Request) { | |
fwd, _ := forward.New() | |
recorder := httptest.NewRecorder() | |
// let us forward this request to another server | |
r.URL = testutils.ParseURI("https://jsonplaceholder.typicode.com:443/posts/1") | |
fwd.ServeHTTP(recorder, r) | |
// yoda | |
data := []byte(hello_string) | |
// and set an additional one | |
w.Header().Set("X-We-Modified-This", "Yup") | |
// But the Content-Length might have been set already, | |
// we should modify it by adding the length | |
clen, _ := strconv.Atoi(r.Header.Get("Content-Length")) | |
clen += len(data) | |
r.Header.Set("Content-Length", strconv.Itoa(clen)) | |
// finally, write out our data and the original body | |
w.Write(data) | |
w.Write(recorder.Body.Bytes()) | |
} | |
return http.HandlerFunc(fn) | |
} | |
func main() { | |
fmt.Println("Version: ", Version) | |
fmt.Println("Git commit hash: ", Build) | |
redirect := redirect("To the hand you talk, because listening I'm not") | |
s := &http.Server{ | |
Addr: ":8080", | |
Handler: redirect, | |
} | |
s.ListenAndServe() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exactly what are you trying to accomplish?