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 的接口函數 Open,http.Dir("/") 實際傳回的是 http.Dir 類型,将字元串路徑轉換成檔案系統。
1// 所屬檔案: src/net/http/fs.go, 26-87行type Dir stringfunc (d Dir) Open(name string) (File, error) { // ...}type FileSystem interface {
2 Open(name string) (File, error)
3}
http.FileServer()
http.FileServer() 方法傳回的是 fileHandler 執行個體,而 fileHandler 結構體實作了 Handler 接口的方法ServeHTTP()。ServeHTTP 方法内的核心是 serveFile() 方法。
1// 所屬檔案: src/net/http/fs.go, 690-716行type fileHandler struct {
2 root FileSystem
3}func FileServer(root FileSystem) Handler { return &fileHandler{root}
4}func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
5 upath := r.URL.Path if !strings.HasPrefix(upath, "/") {
6 upath = "/" + upath
7 r.URL.Path = upath
8 }
9 serveFile(w, r, f.root, path.Clean(upath), true)
10}
1// 所屬檔案: src/net/http/server.go, 82行type Handler interface {
2 ServeHTTP(ResponseWriter, *Request)
3}
serveFile() 方法判斷,如果通路路徑是目錄,則列出目錄内容,如果是檔案則使用 serveContent() 方法輸出檔案内容。serveContent() 方法則是個讀取檔案内容并輸出的方法,此處不再貼代碼。
1// 所屬檔案: src/net/http/fs.go, 540行// name is '/'-separated, not filepath.Separator.func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) { // 中間代碼已省略
2
3 if d.IsDir() { if checkIfModifiedSince(r, d.ModTime()) == condFalse {
4 writeNotModified(w) return
5 }
6 w.Header().Set("Last-Modified", d.ModTime().UTC().Format(TimeFormat))
7 dirList(w, r, f) return
8 } // serveContent will check modification time
9 sizeFunc := func() (int64, error) { return d.Size(), nil }
10 serveContent(w, r, d.Name(), d.ModTime(), sizeFunc, f)
11}
支援子目錄路徑
http.StripPrefix() 方法配合 http.Handle() 或 http.HandleFunc() 可以實作帶路由字首的檔案服務。
1package mainimport ( "net/http"
2 "fmt")func main() {
3
4 http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
5
6 err := http.ListenAndServe(":8080", nil) if err != nil {
7 fmt.Println(err)
8 }
9
10}
原文釋出時間為:2018-08-21
本文作者:輿圖易稿
本文來自雲栖社群合作夥伴“
Golang語言社群”,了解相關資訊可以關注“
”。