微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

golang http.handler接口详解

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,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,item)
        return
    }
    fmt.Fprintf(w,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:

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

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

原文地址:https://www.jb51.cc/go/189691.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐