golang http.handler接口详解

secretx · · 14498 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

golang http.handler接口详解

1.标准库接口定义

package http type Handler interface { ServeHTTP(w ResponseWriter, r *Request) } func ListenAndServe(address string, h Handler) error
ListenAndServe函数需要一个例如“localhost:8000”的服务器地址,和一个处理所有请求的Handler接口实例。它会一直运行,直到这个服务因为一个错误而失败(或者启动失败),它的返回值一定是一个非空的错误。 

2.小Demo

type dollars float32 func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) } type MyHandler map[string]dollars func (self MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { switch req.URL.Path { case "/list": for item, price := range self { fmt.Fprintf(w, "%s: %s\n", item, price) } case "/price": item := req.URL.Query().Get("item") price, ok := self[item] if !ok { w.WriteHeader(http.StatusNotFound) // 404 fmt.Fprintf(w, "no such item: %q\n", item) return } fmt.Fprintf(w, "%s\n", price) default: w.WriteHeader(http.StatusNotFound) // 404 fmt.Fprintf(w, "no such page: %s\n", req.URL) } } func main() { handler := MyHandler{"shoes": 50, "socks": 5} log.Fatal(http.ListenAndServe("localhost:8000", handler)) } 
现在handler基于URL的路径部分(req.URL.Path)来决定执行什么逻辑。如果这个handler不能识别这个路径,它会通过调用w.WriteHeader(http.StatusNotFound)返回客户端一个HTTP404错误 
$ curl http://localhost:8000/list shoes: $50.00 socks: $5.00 $ curl http://localhost:8000/price?item=socks $5.00 $ curl http://localhost:8000/price?item=shoes $50.00 $ curl http://localhost:8000/price?item=hat no such item: "hat" $ curl http://localhost:8000/help no such page: /help 
显然我们可以继续向ServeHTTP方法中添加case,但在一个实际的应用中,将每个case中的逻辑定义到一个分开的方法或函数中会很实用。对于更复杂的应用,一个ServeMux将一批http.Handler聚集到一个单一的http.Handler中,通过组合来处理更加错综复杂的路由需求。 

3.ServeMux.Handle改进版

type MyHandler map[string]dollars func (self MyHandler) list(w http.ResponseWriter, req *http.Request) { for item, price := range self { fmt.Fprintf(w, "%s: %s\n", item, price) } } func (self MyHandler) price(w http.ResponseWriter, req *http.Request) { item := req.URL.Query().Get("item") price, ok := self[item] if !ok { w.WriteHeader(http.StatusNotFound) // 404 fmt.Fprintf(w, "no such item: %q\n", item) return } fmt.Fprintf(w, "%s\n", price) } func main() { handler := MyHandler{"shoes": 50, "socks": 5} mux := http.NewServeMux() mux.Handle("/list", http.HandlerFunc(handler.list)) mux.Handle("/price", http.HandlerFunc(handler.price)) log.Fatal(http.ListenAndServe("localhost:8000", mux)) } 
语句http.HandlerFunc(handler.list)是一个转换而非一个函数调用,因为http.HandlerFunc是一个类型。它有如下的定义: 
package http type HandlerFunc func(w ResponseWriter, r *Request) func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) { f(w, r) } 
HandlerFunc显示了在Go语言接口机制中一些不同寻常的特点。这是一个有实现了接口http.Handler方法的函数类型。ServeHTTP方法的行为调用了它本身的函数。因此HandlerFunc是一个让函数值满足一个接口的适配器(此处是http.Handler接口适配器,因为实现了ServeHTTP方法),这里函数和这个接口仅有的方法有相同的函数签名。实际上,这个技巧让一个单一的类型例如MyHandler以多种方式满足http.Handler接口:一种通过它的list方法,一种通过它的price方法等等。 

4.ServeMux.HandleFunc改进版

因为段落3中ServeMux.Handle方式注册非常普遍,ServeMux有一个方便的HandleFunc方法(ServeMux.Handle的该进),它帮我们简化handler注册代码成这样: 
mux.HandleFunc("/list", handler.list) mux.HandleFunc("/price", handler.price) 
所以为了方便,net/http包提供了一个全局的ServeMux实例DefaultServerMux和包级别的http.Handle和http.HandleFunc函数。现在,为了使用DefaultServeMux作为服务器的主handler,我们不需要将它传给ListenAndServe函数;nil值就可以工作。 
func main() { handler := MyHandler{"shoes": 50, "socks": 5} http.HandleFunc("/list", handler.list) http.HandleFunc("/price", handler.price) log.Fatal(http.ListenAndServe("localhost:8000", nil)) }
代码与段落3区别在于不手动创建ServeMux实例,而是使用net/http包的DefaultServerMux全局实例. 

More:

基于并发考虑,web服务器应该在一个新的协程中调用每一个handler,所以当handler获取其它协程或者这个handler本身的其它请求也可以访问的变量时一定要使用预防措施比如锁机制。

本文源自《GO语言圣经》章节7.7. http.Handler接口


有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:secretx

查看原文:golang http.handler接口详解

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

14498 次点击  ∙  3 赞  
加入收藏 微博
被以下专栏收入,发现更多相似内容
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传