本书未发布
3.4. Server对象
net/http Server
Server
Server对象定义:
type Server struct {
Addr string // TCP address to listen on, ":http" if empty
Handler Handler // handler to invoke, http.DefaultServeMux if nil
TLSConfig *tls.Config
ReadTimeout time.Duration
ReadHeaderTimeout time.Duration // Go 1.8
WriteTimeout time.Duration
IdleTimeout time.Duration // Go 1.8
MaxHeaderBytes int
TLSNextProto map[string]func(*Server, *tls.Conn, Handler) // Go 1.1
ConnState func(net.Conn, ConnState) // Go 1.3
ErrorLog *log.Logger // Go 1.3
BaseContext func(net.Listener) context.Context // Go 1.13
ConnContext func(ctx context.Context, c net.Conn) context.Context // Go 1.13
}
属性 | 类型 | 版本 | 解释 |
---|---|---|---|
Addr | string | 监听端口的地址 | |
Handler | Handler | Server请求的处理对象 | |
TLSConfig | *tls.Config | 监听端口的tls配置 | |
ReadTimeout | time.Duration | ||
ReadHeaderTimeout | time.Duration | 1.8 | |
WriteTimeout | time.Duration | ||
IdleTimeout | time.Duration | 1.8 | |
MaxHeaderBytes | int | ||
TLSNextProto | map[string]func(*Server, *tls.Conn, Handler) | 1.1 | |
ConnState | func(net.Conn, ConnState) | 1.3 | |
ErrorLog | *log.Logger | 1.3 | |
BaseContext | func(net.Listener) context.Context | 1.13 | |
ConnContext | func(ctx context.Context, c net.Conn) context.Context | 1.13 |
Server Method
func (srv *Server) Serve(l net.Listener) error
func (srv *Server) SetKeepAlivesEnabled(v bool)
func (srv *Server) RegisterOnShutdown(f func())
func (srv *Server) Shutdown(ctx context.Context) error
func (srv *Server) Close() error
func (srv *Server) ListenAndServe() error
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error
func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error
Handler
http.HandleFunc
http.HandleFunc使用http.DefaultServeMux这个默认路由调用HandleFunc方法注册一个路由。
// HandleFunc registers the handler function for the given pattern
// in the DefaultServeMux.
// The documentation for ServeMux explains how patterns are matched.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
DefaultServeMux.HandleFunc(pattern, handler)
}
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request))
http.Handler
http.Handler是net/http库处理请求的接口,http.Server直接调用Handler处理请求。
http.ServeMux是net/http库内置的路由器,执行了基本匹配,但是实现了http.Handler接口,Server就直接使用Mux。
HandlerFunc是处理函数,但是这个类型实现了http.Handler接口,就将一个函数转换成了接口。
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)
// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
f(w, r)
}
一下两种方法都将func(ResponseWriter, *Request){}
这样一个函数转换成了http.Handler接口
http.HandlerFunc(func(ResponseWriter, *Request){})
func Convert(h http.HandlerFunc) http.Handler {
return h
}
Middleware
基于net/http简单实现中间件,使用http.Handler接口,使用装饰器模式嵌套一层。
Logger对象实现了http.Handler接口,会先输出请求信息,然后调用路由处理这个请求。
http.ServeMux是标准库实现的路由器,会匹配并处理请求。
package main
import "fmt"
import "net/http"
func main() {
// 创建并注册路由
mux := &http.ServeMux{}
mux.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", r.URL.Path)
})
// 启动服务,给予处理者是Logger
http.ListenAndServe(":8080", &Logger{mux})
}
type Logger struct {
h http.Handler
}
// 实现http.Handler接口
func (log *Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// 输出日志信息
fmt.Printf("%s %s\n", r.Method, r.URL.Path)
// 使用下一个处理者处理请求
log.h.ServeHTTP(w, r)
}
反馈和交流请加群组:QQ群373278915。