golang 去我的博客文章中间件样本。 http://justinas.org/writing-http-middleware-in-go/

Posted

篇首语:本文由小常识网小编为大家整理,主要介绍了golang 去我的博客文章中间件样本。 http://justinas.org/writing-http-middleware-in-go/相关的知识,希望对你有一定的参考价值。

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 (to the real RW) yet, // so we can prepend some data. data := []byte("Middleware says hello again. ") // But the Content-Length might have been set already, // we should modify it by adding the length // of our own data. // Ignoring the error is fine here: // if Content-Length is empty or otherwise invalid, // Atoi() will return zero, // which is just what we'd want in that case. clen, _ := strconv.Atoi(r.Header.Get("Content-Length")) clen += len(data) w.Header.Set("Content-Length", strconv.Itoa(clen)) // finally, write out our data w.Write(data) // 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) }
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) } 
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) }
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) }

以上是关于golang 去我的博客文章中间件样本。 http://justinas.org/writing-http-middleware-in-go/的主要内容,如果未能解决你的问题,请参考以下文章

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人,因此内容不代表本站观点、本站不对文章中的任何观点负责,内容版权归原作者所有、内容只用于提供信息阅读,无任何商业用途。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站(文章、内容、图片、音频、视频)有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至2811500808@qq.com举报,一经查实,本站将立刻删除、维护您的正当权益!