go - 有没有办法将新的 go embed 功能与 echo 一起使用?
问题描述
有没有办法将新的 go embed 功能与 echo 一起使用?我想从嵌入 go embed 的文件中提供带有 echo 静态中间件的文件。 https://tip.golang.org/pkg/embed/ https://echo.labstack.com/guide/static-files
解决方案
此问题链接到Mark Wolfe 的要点,该要点演示了一种非常直接的方法。该方法的核心是:
// content holds our static web server content.
//go:embed static/*
var content embed.FS
var contentHandler = echo.WrapHandler(http.FileServer(http.FS(content)))
// The embedded files will all be in the '/static' folder so need to rewrite the request (could also do this with fs.Sub)
var contentRewrite = middleware.Rewrite(map[string]string{"/*": "/static/$1"})
func SetupRoutes(...) {
e.GET("/*", contentHandler, contentRewrite)
}
推荐阅读
- elasticsearch - 将 updateByQuery 与 ElasticSearch 一起使用时无法解析符号 [字符串]
- maps - 在 Bubble 上重新创建此地图
- javascript - 提交表单将所有空输入及其关联的隐藏输入字段设置为禁用
- loops - Pyspark数据框循环聚合不同条件的列
- sql - SQL,计数语句
- python - 使用Python获取空元组索引的有效方法
- azure - 有没有办法增加 Azure Devops Services Wiki 上的附件文件大小
- excel - Excel VBA Range.Find 格式匹配问题
- javascript - 烧瓶网络应用程序会话无法正常工作
- javascript - 如何在图像中动态查找主矩形图像?