解析Go 標(biāo)準(zhǔn)庫(kù) http.FileServer 實(shí)現(xiàn)靜態(tài)文件服務(wù)
http.FileServer 方法屬于標(biāo)準(zhǔn)庫(kù) 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)庫(kù) http.FileServer 實(shí)現(xiàn)靜態(tài)文件服務(wù),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Go語(yǔ)言操作mysql數(shù)據(jù)庫(kù)簡(jiǎn)單例子
- Golang的os標(biāo)準(zhǔn)庫(kù)中常用函數(shù)的整理介紹
- 完美解決golang go get私有倉(cāng)庫(kù)的問題
- Golang標(biāo)準(zhǔn)庫(kù)syscall詳解(什么是系統(tǒng)調(diào)用)
- 深入解析Go語(yǔ)言的io.ioutil標(biāo)準(zhǔn)庫(kù)使用
- Go編寫定時(shí)器與定時(shí)任務(wù)詳解(附第三方庫(kù)gocron用法)
- 解決Go語(yǔ)言數(shù)據(jù)庫(kù)中null值的問題
- 一步步教你打造高效可靠的Go庫(kù)
相關(guān)文章
golang的匿名函數(shù)和普通函數(shù)的區(qū)別解析
匿名函數(shù)是不具名的函數(shù),可以在不定義函數(shù)名的情況下直接使用,通常用于函數(shù)內(nèi)部的局部作用域中,這篇文章主要介紹了golang的匿名函數(shù)和普通函數(shù)的區(qū)別,需要的朋友可以參考下2023-03-03
聊聊golang中多個(gè)defer的執(zhí)行順序
這篇文章主要介紹了golang中多個(gè)defer的執(zhí)行順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-05-05
go?zero微服務(wù)實(shí)戰(zhàn)系服務(wù)拆分
這篇文章主要為大家介紹了go?zero微服務(wù)實(shí)戰(zhàn)系服務(wù)拆分的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Go語(yǔ)言dolphinscheduler任務(wù)調(diào)度處理
這篇文章主要為大家介紹了Go語(yǔ)言dolphinscheduler任務(wù)調(diào)度處理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

