GO 语言 gin 第4篇 分层实现 路由分发

// 路由器 1 app router

package app

import (
	"github.com/gin-gonic/gin"
)

func Router(router *gin.RouterGroup) {
	cluster := router.Group("apps")
	{
		cluster.GET("/get",getCluster )
		//cluster.POST("/cluster",addCluster )
		//cluster.PUT("/cluster",updateCluster )
	}
}

func getCluster(c *gin.Context){
	c.String(200,"APP Get")
}
// 路由器 2 clusterrouter

package cluster

import (
	"github.com/gin-gonic/gin"
)

func Router(router *gin.RouterGroup) {
	cluster := router.Group("clusters")
	{
		cluster.GET("/get",getcluster )
		//cluster.POST("app/get",postAPP )
		//cluster.PUT("app/get",putAPP )

	}
}

func getcluster(c *gin.Context){
	c.String(200,"Cluster GET")
}

 

// main 代码


package main

import (
	"github.com/gin-gonic/gin"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"testgo/router/rest/cluster"
	"testgo/router/rest/app"
)

//实现单例模式传递(可能理解的不对)
var Router *gin.Engine

func main() {

	Router := gin.Default()

	setRouter()
	Router.Run(":3000")

}

func setRouter(){
	apiv2 := Router.Group("/api/v1")
	rest := apiv2.Group("/rest")
	cluster.Router(rest)
	app.Router(rest)

}



你可能感兴趣的:(go)