[英]Serve static file from within a group with Gin
I want to server static file by mapping /fs
to filesys
in the disk.我想通过将
/fs
映射到磁盘中的filesys
来服务器 static 文件。 I can server static file like this:我可以像这样服务器 static 文件:
r := gin.New()
r.Use(static.Serve("/fs", static.LocalFile("./filesys", false)))
// followed by other routes definition such as R.GET()
I also want to guard access by using a authentication middleware, without affecting other routes.我还想通过使用身份验证中间件来保护访问,而不影响其他路由。 I imagine it's something I need to do with Gin's group like this:
我想这是我需要像这样与 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)))
}
However, I can't get it to work.但是,我无法让它工作。 It is not routed in. If I do additional
g.GET
afterward, the path came out to be wrong.它没有被路由进来。如果我之后再做额外的
g.GET
,路径就会出错。
How to go about this?这个怎么go一下?
Hi I checked this issue has been open for 3 years on git with no solution for 3 years and the static package seems not being maintained anymore嗨,我检查过这个问题已经在 git 上开放了 3 年,3 年没有解决方案,static package 似乎不再维护
This is an alternate solution that might help you这是一个可能对您有帮助的替代解决方案
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()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.