我想通过将/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
,则表明路径错误。
如何处理?
答案 0 :(得分:1)
嗨,我检查了这个问题已经在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()
}