使用Gin从组内提供静态文件

时间:2020-09-22 04:06:44

标签: go go-gin go-http

我想通过将/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,则表明路径错误。

如何处理?

1 个答案:

答案 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()
}
相关问题