Skip to content

Instantly share code, notes, and snippets.

@snooc
Forked from justinas/1_singlehost.go
Created April 8, 2014 03:48
Show Gist options
  • Save snooc/10088535 to your computer and use it in GitHub Desktop.
Save snooc/10088535 to your computer and use it in GitHub Desktop.

Revisions

  1. @justinas justinas created this gist Oct 19, 2013.
    34 changes: 34 additions & 0 deletions 1_singlehost.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    package main

    import (
    "net/http"
    )

    type SingleHost struct {
    handler http.Handler
    allowedHost string
    }

    func NewSingleHost(handler http.Handler, allowedHost string) *SingleHost {
    return &SingleHost{handler: handler, allowedHost: allowedHost}
    }

    func (s *SingleHost) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    host := r.Host
    if host == s.allowedHost {
    s.handler.ServeHTTP(w, r)
    } else {
    w.WriteHeader(403)
    }
    }

    func myHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Success!"))
    }

    func main() {
    single := NewSingleHost(http.HandlerFunc(myHandler), "example.com")

    println("Listening on port 8080")
    http.ListenAndServe(":8080", single)
    }
    29 changes: 29 additions & 0 deletions 2_singlehost_closure.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    package main

    import (
    "net/http"
    )

    func SingleHost(handler http.Handler, allowedHost string) http.Handler {
    ourFunc := func(w http.ResponseWriter, r *http.Request) {
    host := r.Host
    if host == allowedHost {
    handler.ServeHTTP(w, r)
    } else {
    w.WriteHeader(403)
    }
    }
    return http.HandlerFunc(ourFunc)
    }


    func myHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Success!"))
    }

    func main() {
    single := SingleHost(http.HandlerFunc(myHandler), "example.com")

    println("Listening on port 8080")
    http.ListenAndServe(":8080", single)
    }
    25 changes: 25 additions & 0 deletions 3_append.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    package main

    import (
    "net/http"
    )

    type AppendMiddleware struct {
    handler http.Handler
    }

    func (a *AppendMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    a.handler.ServeHTTP(w, r)
    w.Write([]byte("<!-- Middleware says hello! -->"))
    }

    func myHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Success!"))
    }

    func main() {
    mid := &AppendMiddleware{http.HandlerFunc(myHandler)}

    println("Listening on port 8080")
    http.ListenAndServe(":8080", mid)
    }
    42 changes: 42 additions & 0 deletions 4_modifier.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    package main

    import (
    "net/http"
    "net/http/httptest"
    )

    type ModifierMiddleware struct {
    handler http.Handler
    }

    func (m *ModifierMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    rec := httptest.NewRecorder()
    // passing a ResponseRecorder instead of the original RW
    m.handler.ServeHTTP(rec, r)
    // after this finishes, we have the response recorded
    // and can modify it before copying it to the original RW

    // we copy the original headers first
    for k, v := range rec.Header() {
    w.Header()[k] = v
    }
    // and set an additional one
    w.Header().Set("X-We-Modified-This", "Yup")
    // only then the status code, as this call writes the headers as well
    w.WriteHeader(418)
    // the body hasn't been written yet, so we can prepend some data.
    w.Write([]byte("Middleware says hello again. "))
    // then write out the original body
    w.Write(rec.Body.Bytes())
    }

    func myHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Success!"))
    }

    func main() {
    mid := &ModifierMiddleware{http.HandlerFunc(myHandler)}

    println("Listening on port 8080")
    http.ListenAndServe(":8080", mid)
    }