为了进一步加深你对 http 包的理解以及如何去构建一个 web 服务器的功能,学习并尝试练习下面代码:先将代码列出,它用到了的多种功能,输出会在下面说明。
示例 15.20—elaborated_webserver.go:
package mainimport ("bytes""expvar""flag""fmt""net/http""io""log""os""strconv")// hello world 的计数器var helloRequests = expvar.NewInt("hello-requests")// flags:var webroot = flag.String("root", "/home/user", "web root directory")// simple flag servervar booleanflag = flag.Bool("boolean", true, "another flag for testing")// 简单的服务器计数器,发布它将设置值type Counter struct {n int}// 一个通道type Chan chan intfunc main() {flag.Parse()http.Handle("/", http.HandlerFunc(Logger))http.Handle("/go/hello", http.HandlerFunc(HelloServer))// 计数器直接作为一个变量被发布ctr := new(Counter)expvar.Publish("counter", ctr)http.Handle("/counter", ctr)// http.Handle("/go/", http.FileServer(http.Dir("/tmp"))) // 使用操作系统文件系统http.Handle("/go/", http.StripPrefix("/go/", http.FileServer(http.Dir(*webroot))))http.Handle("/flags", http.HandlerFunc(FlagServer))http.Handle("/args", http.HandlerFunc(ArgServer))http.Handle("/chan", ChanCreate())http.Handle("/date", http.HandlerFunc(DateServer))err := http.ListenAndServe(":12345", nil)if err != nil {log.Panicln("ListenAndServe:", err)}}func Logger(w http.ResponseWriter, req *http.Request) {log.Print(req.URL.String())w.WriteHeader(404)w.Write([]byte("oops"))}func HelloServer(w http.ResponseWriter, req *http.Request) {helloRequests.Add(1)io.WriteString(w, "hello, world!\n")}// 通过这个方法满足 expvar.Var 接口,所以就可以直接导出它。func (ctr *Counter) String() string { return fmt.Sprintf("%d", ctr.n) }func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {switch req.Method {case "GET": // 给 n 加 1ctr.n++case "POST": // 设置 n 去发不值buf := new(bytes.Buffer)io.Copy(buf, req.Body)body := buf.String()if n, err := strconv.Atoi(body); err != nil {fmt.Fprintf(w, "bad POST: %v\nbody: [%v]\n", err, body)} else {ctr.n = nfmt.Fprint(w, "counter reset\n")}}fmt.Fprintf(w, "counter = %d\n", ctr.n)}func FlagServer(w http.ResponseWriter, req *http.Request) {w.Header().Set("Content-Type", "text/plain; charset=utf-8")fmt.Fprint(w, "Flags:\n")flag.VisitAll(func(f *flag.Flag) {if f.Value.String() != f.DefValue {fmt.Fprintf(w, "%s = %s [default = %s]\n", f.Name, f.Value.String(), f.DefValue)} else {fmt.Fprintf(w, "%s = %s\n", f.Name, f.Value.String())}})}// 简单参数服务器func ArgServer(w http.ResponseWriter, req *http.Request) {for _, s := range os.Args {fmt.Fprint(w, s, " ")}}func ChanCreate() Chan {c := make(Chan)go func(c Chan) {for x := 0; ; x++ {c <- x}}(c)return c}func (ch Chan) ServeHTTP(w http.ResponseWriter, req *http.Request) {io.WriteString(w, fmt.Sprintf("channel send #%d\n", <-ch))}// 执行一个程序,输出重定向func DateServer(rw http.ResponseWriter, req *http.Request) {rw.Header().Set("Content-Type", "text/plain; charset=utf-8")r, w, err := os.Pipe()if err != nil {fmt.Fprintf(rw, "pipe: %s\n", err)return}p, err := os.StartProcess("/bin/date", []string{"date"}, &os.ProcAttr{Files: []*os.File{nil, w, w}})defer r.Close()w.Close()if err != nil {fmt.Fprintf(rw, "fork/exec: %s\n", err)return}defer p.Release()io.Copy(rw, r)wait, err := p.Wait()if err != nil {fmt.Fprintf(rw, "wait: %s\n", err)return}if !wait.Exited() {fmt.Fprintf(rw, "date: %v\n", wait)return}}
处理浏览器中的网址: localhost:12345/ ,并根据 / 后面接的路径进行处理:
Logger: http://localhost:12345/ 浏览器输出: oops
Logger 会用 w.WriteHeader (404) 记录一个 404 Not Found header。
这个技术通常很有用,当 web 处理代码发生错误的时候,它可以想这样应用:
if err != nil {w.WriteHeader(400)return}
它也可以通过 logger 函数和每一次请求的 url 在 web 服务器的命令窗口上记录日期 + 时间
译者注: 简单的把上面内容整理一下,就是当 url 中的地址不存在(没有对应的路由)时,就会去匹配
/对应的处理函数(Logger()),它会在页面中显示一个 oops ,并且在 header 中写入 404(可以通过浏览器调试模式的 console 或者 network 查看,直接是看不到 404 的),在命令行窗口(也可以理解成日志文件)中记录下错误信息,就像这样的结果:2018/05/27 21:08:42 /,这个里面包含了访问的地址和发生的时间。
HelloServer: http://localhost:12345/go/hello 浏览器输出:hello, world!
HelloServer 使用到了 expvar 包,它可以创建一个变量(可能是 int、float 或者 string 类型),并且通过发布去公开他们。使用 JSON 格式在 HTTP /debug/vars (译者注:就是可以通过 localhost:12345/debug/vars 查看这些被公开的变量) 公开这些变量。它一般用于服务器中的操作计数器; helloRequests 是一个 int64 类型的变量,访问 localhost:12345/go/hello ,将向这个变量的值加 1, 然后像浏览器中输出 「 hello, world! 」
Counter: http://localhost:12345/counter counter = 1GET 方式刷新结果是: counter = 2
Counter 对象 ctr 有一个 String () 方法,所以它就实现了 Var 接口(译者注:因为 Var 接口只有一个 String 方法)。 虽然它是一个结果体,但是这样就可以将它发布(译者注: publish 的第二个参数是个 Var 接口,所以想要发布的结构体必须实现这个接口)。ServeHTTP 是 ctr 的 Handler 方法,因为它有一个正确的签名(译者注:ctr 实现了 ServeHTTP 方法,就实现了 Handler 接口,可以看到示例中,就不需要再通过 HandlerFunc 了,因为它自己就已经是一个 Handler 了)。
FileServer: http://localhost:12345/go/ggg.html 浏览器输出:404 page not found
FileServer 返回一个 root 参数的值为根目录的文件来处理 HTTP 请求。通过 http.Dir 去使用操作系统的文件系统,如:
http.Handle("/go/", http.FileServer(http.Dir("/tmp")))
译者注: 可以在 /tmp 目录下创建一个 ggg.html , 再访问 /go/ggg.html 的时候就会直接在浏览器中显示 ggg.html 的内容。
FlagServer: http://localhost:12345/flags结果:Flags:boolean = trueroot = /home/user
这个 handler 通过 flag.VisitAll 函数去遍历所有的 flags (前面的两个命令行参数),打印他们的变量名、值、默认值(如果值不是默认值的时候)。
ArgServer: http://localhost:12345/args 输出结果: ./elaborated_webserver.exe
这个 handler 遍历 os.Args 去打印所有的命令行参数;如果没有就只会打印程序的名称(可执行文件的目录)。
译者注: 这个有点类似 Linux 的 $0 $1 $2 。
Channel: http://localhost:12345/chan结果:channel send #1刷新后: channel send #2
通道的 ServeHTTP 方法在每个新请求中显示来自通道的下一个整数。所以一个 Web 服务器可以从一个通道接收响应,由另一个函数填充(甚至是客户端)。下面代码片段显示了一个可以完成这个工作的 handler 函数,但是它会在 30 秒后超时:
func ChanResponse(w http.ResponseWriter, req *http.Request) {timeout := make (chan bool)go func () {time.Sleep(30e9)timeout <- true}()select {case msg := <-messages:io.WriteString(w, msg)case stop := <-timeout:return}}DateServer: http://localhost:12345/date输出结果:显示当前的时间 (只能用于类 Unix,因为它调用了 /bin/date)
可能会输出: Thu Sep 8 12:41:09 CEST 2011
os.Pipe() 返回一对连接的文件;从 r 读取,并且返回的字节写入 w。它返回文件和一个错误(如果有的话): func Pipe() (r *File, w *File, err error) 。
