使用 Gin 从组内提供静态文件
Serve static file from within a group with Gin
我想通过将 /fs
映射到磁盘中的 filesys
来服务器静态文件。我可以像这样服务器静态文件:
r := gin.New()
r.Use(static.Serve("/fs", static.LocalFile("./filesys", false)))
// followed by other routes definition such as R.GET()
我也想通过使用身份验证中间件来保护访问,而不影响其他路由。我想这是我需要像这样对 Gin 的小组做的事情:
r := gin.New()
g := r.Group("/fs")
{ // what is the purpose of this parenthesis BTW?
g.Use(authMiddleWare)
g.Use(static.Serve("/fs", static.LocalFile(fileUploadDir, false)))
}
但是,我无法让它工作。它没有被路由进来。如果我之后再做额外的g.GET
,结果路径是错误的。
如何处理?
您好,我检查过这个问题已经在 git 上开放了 3 年,而且 3 年都没有解决方案,静态包似乎不再维护了
这是可能对您有所帮助的替代解决方案
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
grp := r.Group("/static")
{
grp.StaticFS("", http.Dir("/your_directory"))
}
r.Run()
}
我想通过将 /fs
映射到磁盘中的 filesys
来服务器静态文件。我可以像这样服务器静态文件:
r := gin.New()
r.Use(static.Serve("/fs", static.LocalFile("./filesys", false)))
// followed by other routes definition such as R.GET()
我也想通过使用身份验证中间件来保护访问,而不影响其他路由。我想这是我需要像这样对 Gin 的小组做的事情:
r := gin.New()
g := r.Group("/fs")
{ // what is the purpose of this parenthesis BTW?
g.Use(authMiddleWare)
g.Use(static.Serve("/fs", static.LocalFile(fileUploadDir, false)))
}
但是,我无法让它工作。它没有被路由进来。如果我之后再做额外的g.GET
,结果路径是错误的。
如何处理?
您好,我检查过这个问题已经在 git 上开放了 3 年,而且 3 年都没有解决方案,静态包似乎不再维护了
这是可能对您有所帮助的替代解决方案
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
grp := r.Group("/static")
{
grp.StaticFS("", http.Dir("/your_directory"))
}
r.Run()
}