How to serve file from go embed

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

How to serve file from go embed

问题

我有一个包含sign.html文件的静态目录:

//go:embed static
var static embed.FS

它以这种方式提供服务,并且工作正常:

fSys, err := fs.Sub(static, "static")
if err != nil {
    return err
}
mux.Handle("/", http.FileServer(http.FS(fSys)))

然而,在某些路由(例如:/sign)上,在提供页面之前,我想进行一些检查。这是我的处理程序:

func (h Handler) ServeSignPage(w http.ResponseWriter, r *http.Request) error {
    publicKey := r.URL.Query().Get("publicKey")
    err := h.Service.AuthorizeClientSigning(r.Context(), publicKey)
    if err != nil {
        return err
    }
    // 这是我想要在ServeSignPage中提供的嵌入文件
    // 来自静态目录的sign.html
    http.ServeFile(w, r, "sign.html")
    return nil
}

不幸的是,ServeFile显示文件未找到。我如何在ServeSignPage中从文件服务器提供文件?

英文:

I have a static directory, containing a sign.html file :

//go:embed static
var static embed.FS

It is served that way and works fine :

fSys, err := fs.Sub(static, "static")
if err != nil {
	return err
}
mux.Handle("/", http.FileServer(http.FS(fSys)))

On some routes though (for instance: /sign), I want to do some checks before serving the page. This is my handler :

func (h Handler) ServeSignPage(w http.ResponseWriter, r *http.Request) error {
	publicKey := r.URL.Query().Get("publicKey")
	err := h.Service.AuthorizeClientSigning(r.Context(), publicKey)
	if err != nil {
		return err
	}
    // this is where I'd like to serve the embed file
    // sign.html from the static directory
    http.ServeFile(w, r, "sign.html")
	return nil
}

Unfortunately, the ServeFile displays not found. How can I serve the file from the file server within that ServeSignPage ?

答案1

得分: 8

选项1

将文件读取为字节切片。将字节写入响应。

p, err := static.ReadFile("static/sign.html")
if err != nil {
    // TODO: 根据应用程序的需要处理错误。
}
w.Write(p)

选项2

如果ServeSignPage处理程序的路径与文件服务器中的静态文件相同,则委托给文件服务器。

将文件服务器存储在包级变量中。

var staticServer http.Handler

func init() {
    fSys, err := fs.Sub(static, "static")
    if err != nil {
        panic(err)
    }
    staticServer = http.FileServer(http.FS(fSys))
}

将静态服务器用作处理程序:

mux.Handle("/", staticServer)

ServeSignPage中委托给静态服务器:

func (h Handler) ServeSignPage(w http.ResponseWriter, r *http.Request) error {
    publicKey := r.URL.Query().Get("publicKey")
    err := h.Service.AuthorizeClientSigning(r.Context(), publicKey)
    if err != nil {
        return err
    }
    staticServer.ServeHTTP(w, r)
    return nil
}
英文:

Option 1

Read the file to a slice of bytes. Write the bytes to the response.

p, err := static.ReadFile("static/sign.html")
if err != nil {
    // TODO: Handle error as appropriate for the application.
}
w.Write(p)

Option 2

If the path for the ServeSignPage handler is the same as the static file in the file server, then delegate to the file server.

Store the file server in a package-level variable.

var staticServer http.Handler

func init() {
    fSys, err := fs.Sub(static, "static")
    if err != nil {
          panic(err)
    }
    staticServer = http.FileServer(http.FS(fSys)))
}

Use the static server as the handler:

 mux.Handle("/", staticServer)

Delegate to the static server in ServeSignPage:

func (h Handler) ServeSignPage(w http.ResponseWriter, r *http.Request) error {
    publicKey := r.URL.Query().Get("publicKey")
    err := h.Service.AuthorizeClientSigning(r.Context(), publicKey)
    if err != nil {
        return err
    }
    staticServer.ServeHTTP(w, r)
    return nil
}

huangapple
  • 本文由 发表于 2021年11月22日 23:30:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/70068302.html
匿名

发表评论

匿名网友

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

确定