# 路由

# 如何定义路由

如果需要定义一个访问的路由,get请求,访问结果返回一个 json 格式。代码如果


//路由定义总出口 
//文件:service/router/reouter.go
func RouteInit() {

 // 这里定以的是无需授权的接口
	global.GAD_R.GET("/test", func(context *gin.Context) {
		context.JSON(http.StatusOK,gin.H{
			"messages":"ok",
		})
	})

	r := global.GAD_R.Group("api")
	{
		SystemApiInit(r)
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

上图代码定义了一个get 请求 "/test",返回结果如下

{
 "messages":"ok"
}
1
2
3

其中global.GAD_R 为路全局对象。SystemApiInit方法是调用service/router/systemApi.go;这里面注册的路由都是需要授权和认证的,因为使用了两个中间件。正如文件名字那样"systemApi"为系统级的api。

func SystemApiInit(r *gin.RouterGroup) {


	r.Use(middelware.JWTAuth(), middelware.Permission())

//定义需要授权的api
}
1
2
3
4
5
6
7

其它路由定义请参考
路由参数 (opens new window)
路由分组 (opens new window)