XGin

package
v1.2.8 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2021 License: MIT Imports: 13 Imported by: 0

README

Gin Web Engine Starter

基于 https://github.com/gin-gonic/gin 包构建

Gin Documentation

Documentation https://gin-gonic.com/zh-cn/docs/

Starter Usage

1、实现IApi接口,以模块方式注册实现的API

模块接口需实现 IApi interface

// 每个模块服务应该实现的接口
type IApi interface {
	SetRoutes() // 模块服务应该实现的方法,各模块启动器设置相应路由
}

以下为API模块简单示例:

// 包初始化时注册API模块
func init() {
	// 初始化时自动注册该API到Gin Engine
	XGin.RegisterApi(new(SimpleApi))
}

/*定义一个简单的API实现IApi接口,注册到gin引擎*/
type SimpleApi struct {
	// TODO binding service
}

// SetRouter由Gin Engine 启动时调用
func (s *SimpleApi) SetRoutes() {
	// TODO set api routes

	XEngine().GET("simple/foo", s.Foo)
	XEngine().GET("simple/bar", s.Bar)
}

func (s *SimpleApi) Foo(ctx *gin.Context) {
	// TODO call service's method to doing biz logic
	fmt.Println("Call Foo service's method to complete the biz implementation")
	ctx.JSON(200, gin.H{
		"status":  "ok",
		"message": "Call Foo service's method to complete the biz implementation",
	})
}

func (s *SimpleApi) Bar(ctx *gin.Context) {
	// TODO call service's method to doing biz logic
	fmt.Println("Call Bar service's method to complete the biz implementation")
	ctx.JSON(200, gin.H{
		"status":  "ok",
		"message": "Call Bar service's method to complete the biz implementation",
	})
}

2、应用层中定义需要的中间件,并注册启动器

...

middlewares := make([]gin.HandlerFunc, 0)
// TODO add your gin middlewares
// ...
// ...
goinfras.RegisterStarter(XGin.NewStarter(middlewares...))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDefaultEngine

func CreateDefaultEngine(config *Config)

func NewGinEngine

func NewGinEngine(cfg *Config, middlewares ...gin.HandlerFunc) *gin.Engine

func NewStarter

func NewStarter(middlewares ...gin.HandlerFunc) *starter

func RegisterApi

func RegisterApi(module IApi)

注册WEB API初始化对象

func XEngine

func XEngine() *gin.Engine

资源组件实例调用

func XFEngine

func XFEngine(f func(c *gin.Engine) error) error

资源组件闭包执行

func ZapLoggerMiddleware

func ZapLoggerMiddleware() gin.HandlerFunc

func ZapRecoveryMiddleware

func ZapRecoveryMiddleware(stack bool) gin.HandlerFunc

Types

type ApiRegister

type ApiRegister struct {
	// contains filtered or unexported fields
}

API模块注册器

func (*ApiRegister) Register

func (register *ApiRegister) Register(module IApi)

注册API模块

type Config

type Config struct {
	Mode               string // 模式选择:debug
	ListenHost         string // 服务运行ip
	ListenPort         int    // 服务运行端口
	Tls                bool   // HTTPS相关配置,开关
	CertFile           string // HTTPS相关配置,证书文件
	KeyFile            string // HTTPS相关配置,私匙文件
	RecoveryDebugStack bool   // RecoveryStack中间件日志记录开启debug.Stack()
}

func DefaultConfig

func DefaultConfig() *Config

默认最小启动配置

type IApi

type IApi interface {
	SetRoutes() // 模块服务应该实现的方法,各模块启动器设置相应路由
}

每个模块服务应该实现的接口

func GetApis

func GetApis() []IApi

获取注册的web api初始化对象

Jump to

Keyboard shortcuts

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