Help us shape the future of Go! Take the 2026 Go Developer Survey

ginboot

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2019 License: MIT Imports: 2 Imported by: 0

README

ginboot

GinBoot 是一个能够简化 Gin 框架下路由管理的库

核心思想是通过 GinBoot 注册 Gin 的实例和路由群组,然后在每个 Controller 的 init 中各自注册自己的路由回调函数,再统一由 GinBoot 控制群组和路由回调函数的加载顺序,减少路由间的耦合

GinBoot 是非线程安全的(因为 GinBoot 的业务需要在 Gin 服务启动前加载并运行完毕,所以无需考虑并发,也不建议在此期间出现并发问题)

Installation

$ go get -u github.com/niqingyang/ginboot

Quick start

package main

import (
	"ginboot"
	_ "ginboot/example/controller" // 加载 controller 包下的 init 函数,注册路由相关的回调函数
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	// 注册默认的 Gin
	ginboot.RegisterDefaultEngine(r)
	// 引导回调函数注册路由
	ginboot.Strap()

	r.Run()
}

controller

package controller

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

type IndexController struct{}

func init() {
	// 向默认的 Gin 中注册路由
	ginboot.InjectRouteByEngine(ginboot.GinDefault, new(IndexController).registerRoutes)
}

func (i *IndexController) registerRoutes(r *gin.Engine) {
	r.GET("/", i.index)
}

func (IndexController) index(ctx *gin.Context) {
	ctx.String(200, "hello world")
}

注册路由群组

package controller

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

const GroupUser = "/user"
const GroupAdmin = "/admin"

func init() {
	ginboot.InjectGroupByEngine(ginboot.GinDefault, func(engine *gin.Engine) {

		ginboot.RegisterGroup(GroupUser, engine.Group("/user"))
		ginboot.RegisterGroup(GroupAdmin, engine.Group("/admin"))

	})
}

更多使用方法请参考 example,或者阅读源码

Documentation

Index

Constants

View Source
const GinDefault = "default"

默认的 GIN 实例

Variables

This section is empty.

Functions

func GetDefaultEngine

func GetDefaultEngine() *gin.Engine

获取默认 Gin 实例

func GetEngine

func GetEngine(name string) *gin.Engine

获取指定的 Gin 实例

func GetGroup

func GetGroup(name string) *gin.RouterGroup

获取指定路由群组

func InjectGroup

func InjectGroup(callback func())

注入路由群组回调

func InjectGroupByEngine

func InjectGroupByEngine(engineName string, callback func(engine *gin.Engine))

向指定的 GIN 实例中注入路由群组回调

func InjectRoute

func InjectRoute(callback func())

注入路由回调

func InjectRouteByEngine

func InjectRouteByEngine(engineName string, callback func(engine *gin.Engine))

向指定的 GIN 实例中注入路由回调

func InjectRouteByGroup

func InjectRouteByGroup(groupName string, callback func(group *gin.RouterGroup))

向指定的路由群组中中注入路由回调

func RegisterDefaultEngine

func RegisterDefaultEngine(engine *gin.Engine)

注册默认 Gin 实例

func RegisterEngine

func RegisterEngine(name string, engine *gin.Engine)

注册 Gin 实例

func RegisterGroup

func RegisterGroup(name string, group *gin.RouterGroup)

注册路由群组

func Strap

func Strap()

在注册 Gin 实例后,启用引导程序引导回调

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL