Go 标准库 http.FileServer 实现静态文件服务

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

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/


目录
相关文章
|
14天前
|
Go
go语言中的数据类型
go语言中的数据类型
12 0
|
20天前
|
Go 开发者
掌握Go语言:Go语言结构体,精准封装数据,高效管理实体对象(22)
掌握Go语言:Go语言结构体,精准封装数据,高效管理实体对象(22)
|
20天前
|
安全 Go
掌握Go语言:Go语言通道,并发编程的利器与应用实例(20)
掌握Go语言:Go语言通道,并发编程的利器与应用实例(20)
|
20天前
|
存储 缓存 安全
掌握Go语言:Go语言中的字典魔法,高效数据检索与应用实例解析(18)
掌握Go语言:Go语言中的字典魔法,高效数据检索与应用实例解析(18)
|
20天前
|
Go
使用Go语言发邮件
使用Go语言发邮件
20 2
|
1月前
|
缓存 安全 Java
Go语言小细节
Go语言小细节
36 0
|
20天前
|
存储 安全 Go
掌握Go语言:Go语言类型转换,无缝处理数据类型、接口和自定义类型的转换细节解析(29)
掌握Go语言:Go语言类型转换,无缝处理数据类型、接口和自定义类型的转换细节解析(29)
|
1天前
|
数据采集 存储 Go
使用Go语言和chromedp库下载Instagram图片:简易指南
Go语言爬虫示例使用chromedp库下载Instagram图片,关键步骤包括设置代理IP、创建带代理的浏览器上下文及执行任务,如导航至用户页面、截图并存储图片。代码中新增`analyzeAndStoreImage`函数对图片进行分析和分类后存储。注意Instagram的反爬策略可能需要代码适时调整。
使用Go语言和chromedp库下载Instagram图片:简易指南
|
4天前
|
API Go
使用Go语言通过API获取代理IP并使用获取到的代理IP
使用Go语言通过API获取代理IP并使用获取到的代理IP

热门文章

最新文章