zoukankan      html  css  js  c++  java
  • Go 标准库 http.FileServer 实现静态文件服务

    http.FileServer 方法属于标准库 net/http,返回一个使用 FileSystem 接口 root 提供文件访问服务的 HTTP 处理器。可以方便的实现静态文件服务器。

    http.ListenAndServe(":8080", http.FileServer(http.Dir("/files/path")))
    

    访问 http://127.0.0.1:8080,即可看到类似 Nginx 中 autoindex 目录浏览功能。

    源码解析

    我们现在开始将上述的那仅有的一行代码进行剖析,看看到底是如何实现的。源码中英文注释也比较详细,可以参考。

    我们先看 http.Dir(),再看 http.FileServer(),而 http.ListenAndServe() 监听 TCP 端口并提供路由服务,此处不赘述。

    http.Dir()

    从以下源码我们可以看出,type Dir string 实现了 type FileSystem interface 的接口函数 Openhttp.Dir("/") 实际返回的是 http.Dir 类型,将字符串路径转换成文件系统。

    // 所属文件: src/net/http/fs.go, 26-87行
    
    type Dir string
    
    func (d Dir) Open(name string) (File, error) {
    	// ...
    }
    
    type FileSystem interface {
    	Open(name string) (File, error)
    }
    

    http.FileServer()

    http.FileServer() 方法返回的是 fileHandler 实例,而 fileHandler 结构体实现了 Handler 接口的方法 ServeHTTP()ServeHTTP 方法内的核心是 serveFile() 方法。

    // 所属文件: src/net/http/fs.go, 690-716行
    
    type fileHandler struct {
    	root FileSystem
    }
    
    func FileServer(root FileSystem) Handler {
    	return &fileHandler{root}
    }
    
    func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
    	upath := r.URL.Path
    	if !strings.HasPrefix(upath, "/") {
    		upath = "/" + upath
    		r.URL.Path = upath
    	}
    	serveFile(w, r, f.root, path.Clean(upath), true)
    }
    
    // 所属文件: src/net/http/server.go, 82行
    
    type Handler interface {
    	ServeHTTP(ResponseWriter, *Request)
    }
    

    serveFile() 方法判断,如果访问路径是目录,则列出目录内容,如果是文件则使用 serveContent() 方法输出文件内容。serveContent() 方法则是个读取文件内容并输出的方法,此处不再贴代码。

    // 所属文件: src/net/http/fs.go, 540行
    
    // name is '/'-separated, not filepath.Separator.
    func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {
    
    	// 中间代码已省略
    
    	if d.IsDir() {
    		if checkIfModifiedSince(r, d.ModTime()) == condFalse {
    			writeNotModified(w)
    			return
    		}
    		w.Header().Set("Last-Modified", d.ModTime().UTC().Format(TimeFormat))
    		dirList(w, r, f)
    		return
    	}
    
    	// serveContent will check modification time
    	sizeFunc := func() (int64, error) { return d.Size(), nil }
    	serveContent(w, r, d.Name(), d.ModTime(), sizeFunc, f)
    }
    

    支持子目录路径

    http.StripPrefix() 方法配合 http.Handle()http.HandleFunc() 可以实现带路由前缀的文件服务。

    package main
    
    import (
        "net/http"
        "fmt"
    )
    
    func main() {
    
        http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
    
        err := http.ListenAndServe(":8080", nil)
        if err != nil {
            fmt.Println(err)
        }
    
    }
    

    原文地址: https://shockerli.net/post/golang-pkg-http-file-server/

  • 相关阅读:
    CSS3(4)---动画(animation)
    MDT 2010驱动管理新方法。
    windows 2012 试用180天
    Mac地址绑定的wifi
    用于主题检测的临时日志(b2d5c7b3-e3f6-4b0f-bfa4-a08e923eda9b
    samba4.1.9安装
    samba权限之easy举例说明--原创
    NFS性能优化
    工作组环境下管理windows.
    管理windows防火墙
  • 原文地址:https://www.cnblogs.com/shockerli/p/golang-pkg-http-file-server.html
Copyright © 2011-2022 走看看