Skip to content

Instantly share code, notes, and snippets.

@braoru
Created November 16, 2016 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braoru/0e44575009cff3d06f2cfae547c26bb8 to your computer and use it in GitHub Desktop.
Save braoru/0e44575009cff3d06f2cfae547c26bb8 to your computer and use it in GitHub Desktop.
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()
}
@lalloni
Copy link

lalloni commented Nov 17, 2016

Exactly what are you trying to accomplish?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment