多个静态文件目录

huangapple go评论80阅读模式
英文:

Multiple static files directories

问题

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

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

其中,静态文件夹static应该与二进制文件放在一起。

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

	//go:embed static
	var staticFiles embed.FS

	// 使用http.FS创建http文件系统
	var staticFS = http.FS(staticFiles)
	fs := http.FileServer(staticFS) // 嵌入的静态文件
	// 服务静态文件
	http.Handle("/static/", fs)

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

	//go:embed static
	var staticFiles embed.FS

	// 使用http.FS创建http文件系统
	var staticFS = http.FS(staticFiles)
	fs := http.FileServer(staticFS) // 嵌入的静态文件
	// 服务静态文件
	http.Handle("/static/", fs)

	www := http.FileServer(http.Dir("./files/")) // 附加的静态文件,与二进制文件放在一起
	// 服务静态文件
	http.Handle("/files/", www)
英文:

In go, we can handle static files, by defining their directory as static directory as shown below:

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

Where static files folder static should be beside the binary.

Another way, is to use the new //go embed as:

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

But what if I want to have most of my static files embedded in the binary, and some are not, that can be used alongside the binary, I tried to mix both method defined above, but did not work, only the embedded ones run smoothly, below code failed, any thought?:

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

答案1

得分: 1

我找到了,缺少了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)
		}
}
英文:

I found it, was missing http.StripPrefix, below worked perfectly with me, and have multiple static folders:

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 can be used to create a http Filesystem
		var staticFS = http.FS(staticFiles)
		fs := http.FileServer(staticFS) // embeded static files
		// Serve static files, to be embedded in the binary
		http.Handle("/static/", fs)

		// Serve public files, to be beside binary
		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)
		}
}

huangapple
  • 本文由 发表于 2021年8月1日 03:38:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/68605305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定