Juliet 轻量级 Golang 中间件 Juliet

agolangf • 1389 次点击    
这是一个分享于 的项目,其中的信息可能已经有所发展或是发生改变。
Juliet 是一款轻量级的 Golang 中间件链接助手,将 Context(地图)对象从中间件传递到下一个。 示例: <pre>package main import (     &#34;net/http&#34;     &#34;log&#34;     &#34;net&#34;     &#34;fmt&#34;     &#34;github.com/root-gg/juliet&#34; ) // Juliet is a lightweight middleware chaining helper that pass a Context (map) object // from a middleware to the next one. // // Middlewre is a pattern where http request/response are passed through many handlers to reuse code. // For example this classic middleware log the requested url func logMiddleware(next http.Handler) http.Handler {     return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {         // Our middleware logic goes here...         log.Println(r.URL.String())         // Pass the request to the next middleware / handler         next.ServeHTTP(w, r)     }) } // Juliet adds a context parameter to the middleware pattern that will be passed along the Chain. // For example this middleware puts the request&#39;s source IP address in the context. func getSourceIpMiddleware(ctx *juliet.Context, next http.Handler) http.Handler {     return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {         // Get the source IP from the request remote address         ip, _, err := net.SplitHostPort(r.RemoteAddr)         // You can handle failure at any point of the chain by not calling next.ServeHTTP         if err != nil {             http.Error(w, &#34;Unable to parse source ip address&#34;, 500)             return         }         // Save the source ip in the context         ctx.Set(&#34;ip&#34;, ip)         // Pass the request to the next middleware / handler         next.ServeHTTP(w, r)     }) } // As a context is nothing more that a map[interface{}]interface{} with syntactic sugar you have to ensure you // check types of values you get from the context. To keep your code clean you can write helpers to do that and keep // type safety everywhere. func getSourceIp(ctx *juliet.Context) string {     if sourceIP, ok := ctx.Get(&#34;ip&#34;); ok {         return sourceIP.(string)     }     return &#34;&#34; } // The last link of a middleware chain is the application Handler func pingHandler(w http.ResponseWriter, r *http.Request) {     w.Write([]byte(&#34;pong\n&#34;)) } // Juliet can also pass the context parameter to application Handlers func ipHandler(ctx *juliet.Context, resp http.ResponseWriter, req *http.Request) {     // write http response     resp.Write([]byte(fmt.Sprintf(&#34;your IP address is : %s\n&#34;, getSourceIp(ctx)))) } func main(){     // Juliet links middleware and handler with chain objects     chain := juliet.NewChain()     // Chain objects are immutable and any operation on it returns a new chain object.     // You can append one or more middleware to the chain at a time using the Append method.     chain = chain.Append(getSourceIpMiddleware)     // You can append a middleware to the beginning of the chain with the AppendChain method.     // When working with a non context-aware ( Juliet ) middleware you have to use the Adapt method.     chain = juliet.NewChain(juliet.Adapt(logMiddleware)).AppendChain(chain)     // Now we have this middleware chain : logUrl &gt; getSourceIp &gt; ApplicationHandler     // We could have built it in one pass this way :     //  chain := juliet.NewChain(juliet.Adapt(logMiddleware),getSourceIpMiddleware)     // It&#39;s now time to add some application handlers and to bind everything to some HTTP route.     // With a context handler     http.Handle(&#34;/ip&#34;, chain.Then(ipHandler))     // With a classic http.HandlerFunc     http.Handle(&#34;/ping&#34;, chain.ThenHandlerFunc(pingHandler))     // With a classic http.Handler     http.Handle(&#34;/404&#34;, chain.ThenHandler(http.NotFoundHandler()))     log.Fatal(http.ListenAndServe(&#34;:1234&#34;, nil)) } // $ go run main.go // 2016/02/01 12:20:39 /ip // 2016/02/01 12:20:44 /ping // 2016/02/01 12:20:56 /404 // // $ curl 127.0.0.1:1234/ip // your IP address is : 127.0.0.1 // $ curl 127.0.0.1:1234/ping // pong // $ curl 127.0.0.1:1234/404 // 404 page not found</pre>
授权协议:
MIT
开发语言:
Google Go 查看源码»
操作系统:
跨平台
1389 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传