> For the complete documentation index, see [llms.txt](https://65480539.gitbook.io/goft/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://65480539.gitbook.io/goft/lu-you-ji-zhong-jian-jian/ji-ben-ding-yi.md).

# 基本定义

原生gin的中间件无法定位到具体的URL。改造后目前支持路由级的中间件，支持绑定具体的URL进行中间件执行

路由级中间件的定义和全局中间没有任何不同。

区别在于不要在main函数注册，而是在控制器注册

## 一段控制器的Build函数

```
func(this *IndexController) Build(goft *goft.Goft){
   goft.Handle("GET","/",this.Index).
      HandleWithFairing("GET","/test",
      this.IndexTest,
      middlewares.NewIndexTest())
 }
```

这个build函数定义了两个路由

1、/ &#x20;

2、/test&#x20;

&#x20;其中 我们在/test 这个路由上定义个 单独的中间件。注意全局中间件优先级最高，如果定义了路由中间件。两者都会执行

```
type IndexTest struct {

}
func NewIndexTest() *IndexTest {
   return &IndexTest{}
}
func(this *IndexTest) OnRequest(ctx *gin.Context) error{
   return nil
}
func(this *IndexTest) OnResponse(result interface{}) (interface{}, error){
   if m,ok:=result.(gin.H);ok{
      m["metadata"]="index test"
      return m,nil
   }
   return result,nil
}
```
