Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 如何在post请求中删除静态中间件?_Go_Go Gin - Fatal编程技术网

Go 如何在post请求中删除静态中间件?

Go 如何在post请求中删除静态中间件?,go,go-gin,Go,Go Gin,我希望主页在post请求后停止可用。 这是我的密码。 提前谢谢 package main import( "github.com/gin-contrib/static" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.Use(static.Serve("/", static.LocalFile("./pages/home", true))) r.POST("/example", f

我希望主页在post请求后停止可用。 这是我的密码。 提前谢谢

package main
import(
    "github.com/gin-contrib/static"
    "github.com/gin-gonic/gin"
)

func main() {
   r := gin.Default()
   r.Use(static.Serve("/", static.LocalFile("./pages/home", true)))
   r.POST("/example", func(c *gin.Context) {
      //here I would like to stop serving the static files on a POST request
   })
   r.Run(":8080")
}
我的目录结构

-main.go
-pages
   -home
      -index.html

我不是
gin
方面的专家,但它类似于
echo
,因此我创建了一个片段供您检查它是否符合您的需要

看起来在连接中间件之后不可能分离中间件,所以我的方法是为每个请求检查一个全局变量,看看资源是否可用

主程序包
进口(
“net/http”
“同步/原子”
“github.com/gin contrib/static”
“github.com/gin gonic/gin”
)
//在这个范围内,使用原子包而不是互斥看起来更好
var noIndex int32
func indexMiddleware()gin.HandlerFunc{
hdl:=static.service(“/”,static.LocalFile(“./pages/home”,true))
返回函数(c*gin.Context){
如果原子.LoadInt32(&noIndex)==0{
高密度脂蛋白(c)
//如果你有额外的中间件,让它们运行
c、 下一个()
返回
}
c、 AbortWithStatus(http.StatusBadRequest)
}
}
func main(){
r:=gin.Default()
r、 使用(indexMiddleware())
r、 POST(“/example”,func(c*gin.Context){
原子比较WAPINT32(&noIndex,0,1)
c、 字符串(http.StatusOK,“OK”)
})
r、 运行(“:8080”)
}

你知道你的中间件(静态文件服务)导致无限重定向吗?你能更具体地解释一下你的意思吗。我运行你的代码,浏览器告诉我错误太多重定向对我有用,您是否已将文件创建并放置在pages目录中?我已添加了目录结构以使其更清晰。我已创建pages目录,并在其下创建了
home
文件,如您的代码所示。当我访问
http://localhost:8080
它将永远重定向。我
去获取最新版本的库。在您的目录结构中有index.html,但在您的代码中没有。这适用于所有请求,因此在第一篇文章之后,您无法连接和接收html文件。是的,所有请求都会得到满足,直到发送到/示例结束点的post请求。是否可以仅停止发送到特定结束点的请求users@JosephGlynn是的,如果你有一个身份验证机制,这当然是可能的。你应该知道是谁提出的请求。HTTP协议是无状态的,因此,您应该能够以某种方式了解客户端,例如标头、cookie、url参数等。请检查基本HTTP身份验证和会话管理以了解gin。