Golang Gin embed static静态文件嵌入问题
作者:LeoForBest
在使用Gin框架开发Web服务时,需解决静态资源嵌入问题,本文探讨了将static目录嵌入到Gin生成的应用程序中,通过直接使用Gin自带的http.FileServer方法,简化了开发流程,最终实现了一键部署的目标
需求
用Gin开发Web服务时, 编译生成的应用可能如下, 需提供static目录和web-app.exe给用户
如果将static文件夹到生成的exe中,分发单个EXE文件给用户使用,更加方便
# 改进前 ├── static │ └── js/jquery.min.js │ ├── favicon.ico │ ├── index.html ├── web-app.exe # 改进后 ├── web-app.exe (static内嵌进exe里)
改进思路
a). Gin文档 静态资源嵌入 方案
参考: https://learnku.com/docs/gin-gonic/1.7/examples-bind-single-binary-with-template/11403
需要使用额外工具go-assets,操作有点复杂,因此不考虑
b). Gin 自带方法
代码:
package main
import (
"embed"
"net/http"
"github.com/gin-gonic/gin"
)
//go:embed static/*
var fs embed.FS
func main() {
r := gin.Default()
r.StaticFS("/static", http.FS(fs))
}
效果:
以favicon.ico为例, 需要访问
http://localhost/static/static/favicon.ico
中间多了两个static, 而index.html中可能资源位置是
<link rel="icon" href="/static/favicon.ico" rel="external nofollow" >

c). 改进
1. 自带http库做法
http.StripPrefix("/static", http.FileServer(http.FS(fs)))
2. 查看gin staticfs源码
看下大致调用函数名,可以看到大概是这样调用
http.FileServer(fs).ServeHTTP(c.Writer, c.Request)
func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
absolutePath := group.calculateAbsolutePath(relativePath)
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
return func(c *Context) {
........ 读取fs中文件,判断是否存在有权限等错误,省略
fileServer.ServeHTTP(c.Writer, c.Request)
}
}
3. 最终解决方案
package main
import (
"embed"
"net/http"
"github.com/gin-gonic/gin"
)
//go:embed static/*
var fs embed.FS
func main() {
r := gin.Default()
/*
查看staticfs方案中gin启动日志可以看到,staticfs实际注册了GET、HEAD
[GIN-debug] GET /static/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
[GIN-debug] HEAD /static/*filepath --> github.com/gin-gonic/gin.(*RouterGroup).createStaticHandler.func1 (3 handlers)
因此直接用r.Any
*/
r.Any("/static/*filepath", func(c *gin.Context) {
staticServer := http.FileServer(http.FS(fs))
staticServer.ServeHTTP(c.Writer, c.Request)
})
r.Run("localhost:80")
}
查看效果达到预期

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
