用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里)
参考: https://learnku.com/docs/gin-gonic/1.7/examples-bind-single-binary-with-template/11403
需要使用额外工具go-assets,操作有点复杂,因此不考虑
代码:
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" >

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")
}
查看效果达到预期

以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。
您可能感兴趣的文章:Python工程实践之np.loadtxt()读取数据
python中的extend功能及用法
pythonJieba分词处理详解【模式词库的添加、删除自定义词库失败处理等】
pycharm没有找到manage repositories按钮的解决办法
Python+pandas数据分析实践总结
pycharm中报ModuleNotFoundErrorNo module named apostensorflowapos错误解决