婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

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

熱門標(biāo)簽:南通如皋申請(qǐng)開通400電話 江西轉(zhuǎn)化率高的羿智云外呼系統(tǒng) 學(xué)海導(dǎo)航地圖標(biāo)注 地圖標(biāo)注的汽車標(biāo) 浙江高速公路地圖標(biāo)注 西部云谷一期地圖標(biāo)注 中國(guó)地圖標(biāo)注省會(huì)高清 廣州呼叫中心外呼系統(tǒng) 高德地圖標(biāo)注口訣

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

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

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

源碼解析

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

我們先看 http.Dir(),再看 http.FileServer(),而 http.ListenAndServe() 監(jiān)聽(tīng) 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() 方法判斷,如果訪問(wèn)路徑是目錄,則列出目錄內(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)庫(kù) http.FileServer 實(shí)現(xiàn)靜態(tài)文件服務(wù),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

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

標(biāo)簽:常州 東營(yíng) 曲靖 德宏 保定 吐魯番 許昌 貴州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解析Go 標(biāo)準(zhǔn)庫(kù) http.FileServer 實(shí)現(xiàn)靜態(tài)文件服務(wù)》,本文關(guān)鍵詞  解析,標(biāo)準(zhǔn),庫(kù),http.FileServer,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《解析Go 標(biāo)準(zhǔn)庫(kù) http.FileServer 實(shí)現(xiàn)靜態(tài)文件服務(wù)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于解析Go 標(biāo)準(zhǔn)庫(kù) http.FileServer 實(shí)現(xiàn)靜態(tài)文件服務(wù)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 临海市| 阜南县| 崇信县| 九龙坡区| 论坛| 福海县| 通河县| 南平市| 长兴县| 明水县| 宁河县| 巫溪县| 宜黄县| 云霄县| 长治市| 柳河县| 牙克石市| 六盘水市| 长岭县| 会同县| 金华市| 霞浦县| 抚顺市| 习水县| 扶风县| 富源县| 伊川县| 濮阳县| 天长市| 赫章县| 和林格尔县| 泰安市| 札达县| 碌曲县| 湟中县| 游戏| 宁武县| 和田县| 天等县| 通州市| 铁力市|