【问题标题】:Serve static file from within a group with Gin使用 Gin 从组内提供静态文件
【发布时间】:2020-09-22 04:06:44
【问题描述】:

我想通过将/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,路径就会出错。

如何解决这个问题?

【问题讨论】:

标签: go go-gin go-http


【解决方案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()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2013-05-28
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    相关资源
    最近更新 更多