加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang http.handler接口详解

发布时间:2020-12-16 18:30:44 所属栏目:大数据 来源:网络整理
导读:golang http.handler接口详解 1.标准库接口定义 package httptype Handler interface { ServeHTTP(w ResponseWriter,r *Request)}func ListenAndServe(address string,h Handler) error ListenAndServe函数需要一个例如“localhost:8000”的服务器地址,和一

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: %sn",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: %qn",item)
                return
            }
            fmt.Fprintf(w,"%sn",price)
        default:
            w.WriteHeader(http.StatusNotFound) // 404
            fmt.Fprintf(w,"no such page: %sn",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本身的其它请求也可以访问的变量时一定要使用预防措施比如锁机制。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读