佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 解析Go 標(biāo)準(zhǔn)庫 http.FileServer 實(shí)現(xiàn)靜態(tài)文件服務(wù)

解析Go 標(biāo)準(zhǔn)庫 http.FileServer 實(shí)現(xiàn)靜態(tài)文件服務(wù)

熱門標(biāo)簽:Linux服務(wù)器 百度競(jìng)價(jià)排名 服務(wù)外包 鐵路電話系統(tǒng) 呼叫中心市場(chǎng)需求 AI電銷 網(wǎng)站排名優(yōu)化 地方門戶網(wǎng)站

http.FileServer 方法屬于標(biāo)準(zhǔn)庫 net/http,返回一個(gè)使用 FileSystem 接口 root 提供文件訪問服務(wù)的 HTTP 處理器??梢苑奖愕膶?shí)現(xiàn)靜態(tài)文件服務(wù)器。

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

訪問 http://127.0.0.1:8080,即可看到類似 Nginx 中 autoindex 目錄瀏覽功能。

源碼解析

我們現(xiàn)在開始將上述的那僅有的一行代碼進(jìn)行剖析,看看到底是如何實(shí)現(xiàn)的。源碼中英文注釋也比較詳細(xì),可以參考。

我們先看 http.Dir(),再看 http.FileServer(),而 http.ListenAndServe() 監(jiān)聽 TCP 端口并提供路由服務(wù),此處不贅述。

http.Dir()

從以下源碼我們可以看出,type Dir string 實(shí)現(xiàn)了 type FileSystem interface 的接口函數(shù) Open,http.Dir("/") 實(shí)際返回的是 http.Dir 類型,將字符串路徑轉(zhuǎn)換成文件系統(tǒng)。

// 所屬文件: 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 實(shí)例,而 fileHandler 結(jié)構(gòu)體實(shí)現(xiàn)了 Handler 接口的方法 ServeHTTP()。ServeHTTP 方法內(nèi)的核心是 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() 方法判斷,如果訪問路徑是目錄,則列出目錄內(nèi)容,如果是文件則使用 serveContent() 方法輸出文件內(nèi)容。serveContent() 方法則是個(gè)讀取文件內(nèi)容并輸出的方法,此處不再貼代碼。

// 所屬文件: 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() 可以實(shí)現(xiàn)帶路由前綴的文件服務(wù)。

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)
  }
}

總結(jié)

以上所述是小編給大家介紹的解析Go 標(biāo)準(zhǔn)庫 http.FileServer 實(shí)現(xiàn)靜態(tài)文件服務(wù),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

您可能感興趣的文章:
  • Go 實(shí)現(xiàn)熱重啟的詳細(xì)介紹
  • 詳解如何熱重啟golang服務(wù)器
  • 在Go中構(gòu)建并發(fā)TCP服務(wù)器
  • Go語言的http/2服務(wù)器功能及客戶端使用
  • MongoDB4.0在windows10下的安裝與服務(wù)配置教程詳解
  • goland服務(wù)熱重啟的配置文件

標(biāo)簽:銅川 衡水 湖南 黃山 崇左 蘭州 仙桃 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解析Go 標(biāo)準(zhǔn)庫 http.FileServer 實(shí)現(xiàn)靜態(tài)文件服務(wù)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    武强县| 曲阳县| 达尔| 徐州市| 北票市| 龙江县| 鲜城| 米脂县| 宁陕县| 博罗县| 惠州市| 霍山县| 芷江| 汶上县| 嘉兴市| 和田县| 永泰县| 安顺市| 龙泉市| 临湘市| 宝清县| 双桥区| 红原县| 白银市| 阿巴嘎旗| 莆田市| 伊春市| 鄂温| 罗田县| 湖州市| 大厂| 紫金县| 南漳县| 朔州市| 伊通| 遵义市| 和龙市| 甘洛县| 运城市| 获嘉县| 黄大仙区|