Golang中如何管理多个静态文件目录

Golang中如何管理多个静态文件目录 在Go语言中,我们可以通过将静态文件目录定义为静态目录来处理静态文件,如下所示:

		fs := http.FileServer(http.Dir("./static/")) 
		http.Handle("/files/", fs)

其中,静态文件文件夹 static 应位于二进制文件旁边。

另一种方法是使用新的 //go embed 指令,如下所示:

        //go:embed static
        var staticFiles embed.FS

		// http.FS can be used to create a http Filesystem
		var staticFS = http.FS(staticFiles)
		fs := http.FileServer(staticFS) // embeded static files
		// Serve static files
		http.Handle("/static/", fs)

但是,如果我希望将大部分静态文件嵌入到二进制文件中,而另一些不嵌入,以便它们可以与二进制文件一起使用,我尝试混合使用上述两种方法,但没有成功。只有嵌入的文件运行顺利,下面的代码失败了,有什么想法吗?:

        //go:embed static
        var staticFiles embed.FS

		// http.FS can be used to create a http Filesystem
		var staticFS = http.FS(staticFiles)
		fs := http.FileServer(staticFS) // embeded static files
		// Serve static files
		http.Handle("/static/", fs)

		www := http.FileServer(http.Dir("./files/")) // side static files, to be beside binary
		// Serve static files
		http.Handle("/files/", www)

更多关于Golang中如何管理多个静态文件目录的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

我找到了问题所在,原来是缺少了 http.StripPrefix。下面的代码对我来说运行得非常完美,并且可以处理多个静态文件夹:

package main

import (
	"embed"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

//go:embed static
var staticFiles embed.FS

func main() {
	go func() {
		http.HandleFunc("/favicon.ico", func(rw http.ResponseWriter, r *http.Request) {})
		// http.FS 可以用来创建一个 http 文件系统
		var staticFS = http.FS(staticFiles)
		fs := http.FileServer(staticFS) // 嵌入的静态文件
		// 提供静态文件服务,这些文件将被嵌入到二进制文件中
		http.Handle("/static/", fs)

		// 提供公共文件服务,这些文件位于二进制文件旁边
		http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./files"))))

		http.HandleFunc("/getSkills", getSkills)
		log.Println("Listening on :3000...")
		err := http.ListenAndServe(":3000", nil)
		if err != nil {
			log.Fatal(err)
		}
}

更多关于Golang中如何管理多个静态文件目录的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在Go中同时管理嵌入式静态文件系统和外部文件系统是完全可行的。问题可能出在路由处理或文件系统路径配置上。以下是修正后的示例:

package main

import (
    "embed"
    "net/http"
)

//go:embed static/*
var staticFiles embed.FS

func main() {
    // 嵌入式文件系统
    embeddedFS := http.FS(staticFiles)
    embeddedHandler := http.FileServer(embeddedFS)
    // 使用StripPrefix确保正确路径
    http.Handle("/static/", http.StripPrefix("/static/", embeddedHandler))

    // 外部文件系统
    externalHandler := http.FileServer(http.Dir("./files"))
    // 同样需要StripPrefix
    http.Handle("/files/", http.StripPrefix("/files/", externalHandler))

    http.ListenAndServe(":8080", nil)
}

如果仍然有问题,可以尝试更明确的处理方式:

package main

import (
    "embed"
    "io/fs"
    "net/http"
)

//go:embed static
var embeddedStatic embed.FS

func main() {
    // 获取嵌入式文件系统的子目录
    embeddedSubFS, _ := fs.Sub(embeddedStatic, "static")
    
    // 嵌入式文件处理器
    http.Handle("/static/", 
        http.StripPrefix("/static/", 
            http.FileServer(http.FS(embeddedSubFS))))

    // 外部文件处理器
    http.Handle("/files/", 
        http.StripPrefix("/files/", 
            http.FileServer(http.Dir("./files"))))

    http.ListenAndServe(":8080", nil)
}

确保目录结构正确:

项目目录/
├── main.go
├── static/          # 嵌入式文件
│   ├── index.html
│   └── css/
└── files/           # 外部文件(与二进制文件同级)
    ├── uploads/
    └── config.json

检查文件权限和路径,确保外部files目录存在且可访问。

回到顶部