• Stars
    star
    287
  • Rank 138,917 (Top 3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

gin auto binding,grpc, and annotated route,gin 注解路由, grpc,自动参数绑定工具

Build Status Go Report Card codecov GoDoc Mentioned in Awesome Go

Automatic parameter binding base on go-gin

img

doc

doc

Golang gin automatic parameter binding

  • Support for RPC automatic mapping

  • Support object registration

  • Support annotation routing

  • base on go-gin on json restful style

  • implementation of parameter filtering and binding with request

  • code registration simple and supports multiple ways of registration

  • grpc-go bind support

  • Support swagger MORE

  • Support markdown/mindoc MORE

  • Support call before and after deal(ginrpc.WithBeforeAfter)

  • DEMO

Installing

  • go mod:
go get -u github.com/xxjwxc/ginrpc@master

API details

Three interface modes are supported

  • func(*gin.Context) // go-gin Raw interface

    func(*api.Context) // Custom context type

  • func(*api.Context,req) // Custom context type,with request

    func(*api.Context,*req)

  • func(*gin.Context,*req) // go-gin context,with request

    func(*gin.Context,req)

  • func(*gin.Context,*req)(*resp,error) // go-gin context,with request,return parameter and error ==> grpc-go

    func(*gin.Context,req)(resp,error)

一. Parameter auto binding,Object registration (annotation routing)

Initialization project (this project is named after gmsec)

``` go mod init gmsec ```

coding more>>

package main

import (
	"fmt"
	"net/http"

	_ "gmsec/routers" // Debug mode requires adding [mod] / routes to register annotation routes.debug模式需要添加[mod]/routers 注册注解路由
	"github.com/xxjwxc/public/mydoc/myswagger" // swagger 支持

	"github.com/gin-gonic/gin"
	"github.com/xxjwxc/ginrpc"
	"github.com/xxjwxc/ginrpc/api"
)

type ReqTest struct {
	Access_token string `json:"access_token"`
	UserName     string `json:"user_name" binding:"required"` // With verification mode
	Password     string `json:"password"`
}

// Hello ...
type Hello struct {
}

// Hello Annotated route (bese on beego way)
// @Router /block [post,get]
func (s *Hello) Hello(c *api.Context, req *ReqTest) {
	fmt.Println(req)
	c.JSON(http.StatusOK, "ok")
}

// Hello2 Route without annotation (the parameter is 2 default post)
func (s *Hello) Hello2(c *gin.Context, req ReqTest) {
	fmt.Println(req)
	c.JSON(http.StatusOK, "ok")
}

// [grpc-go](https://github.com/grpc/grpc-go)
// with request,return parameter and error
// TestFun6 Route without annotation (the parameter is 2 default post)
func TestFun6(c *gin.Context, req ReqTest) (*ReqTest, error) {
	fmt.Println(req)
	//c.JSON(http.StatusOK, req)
	return &req, nil
}

func main() {

	// swagger
	myswagger.SetHost("https://localhost:8080")
	myswagger.SetBasePath("gmsec")
	myswagger.SetSchemes(true, false)
	// -----end --
	base := ginrpc.New()
	router := gin.Default() // or router :=  gin.Default().Group("/xxjwxc")
	base.Register(router, new(Hello)) // object register like(go-micro)
	router.POST("/test6", base.HandlerFunc(TestFun6))                            // function register
	base.RegisterHandlerFunc(router, []string{"post", "get"}, "/test", TestFun6) 
	router.Run(":8080")
}

- Annotation routing related instructions


// @Router /block [post,get]
@Router tag  
/block router 
[post,get] method 

  • @Router - [-] indicates ignore

Note: if there is no annotation route in the object function, the system will add annotation route by default. Post mode: with req (2 parameters (CTX, req)), get mode is a parameter (CTX)

1. Annotation route will automatically create [mod]/routes/gen_router.go file, which needs to be added when calling:

```
_ "[mod]/routers" // Debug mode requires adding [mod] / routes to register annotation routes

```

By default, the [gen_router. Data] file will also be generated in the root directory of the project (keep this file, and you can embed it without adding the above code)

2. way of annotation route :

more to saying  [gmsec](https://github.com/gmsec/gmsec)

3. Parameter description

ginrpc.WithCtx : Set custom context

ginrpc.WithDebug(true) : Set debug mode

ginrpc.WithOutDoc(true) : output markdown/swagger api doc

ginrpc.WithBigCamel(true) : Set big camel standard (false is web mode, _, lowercase)

ginrpc.WithBeforeAfter(&ginrpc.DefaultGinBeforeAfter{}) : Before After call

[more>>](https://godoc.org/github.com/xxjwxc/ginrpc)

4. Execute curl to automatically bind parameters. See the results directly

curl 'http://127.0.0.1:8080/xxjwxc/block' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}'
curl 'http://127.0.0.1:8080/xxjwxc/hello.hello2' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}'

二. swagger/markdown/mindoc Document generation description

	ginrpc.WithOutDoc(true) : output markdown/swagger

1.For object registration 'ginrpc. Register' mode, document export is supported

2.Export supports annotation routing, Parameter annotation and default value (tag '. default')

3.Default export path:(/docs/swagger/swagger.json,/docs/markdown)

4 struct demo

type ReqTest struct {
	AccessToken string `json:"access_token"`
	UserName    string `json:"user_name" binding:"required"` // 带校验方式
	Password    string `json:"password"`
}

三. Support to call Middleware

  • using ginrpc.WithBeforeAfter(&ginrpc.DefaultGinBeforeAfter{})
  • You can also implement functions (single types) on objects
	// GinBeforeAfter Execute middleware before and after the object call (support adding the object separately from the object in total)
	type GinBeforeAfter interface {
		GinBefore(req *GinBeforeAfterInfo) bool
		GinAfter(req *GinBeforeAfterInfo) bool
	}

Stargazers over time

Stargazers over time

coding address:ginprc Please give star support

More Repositories

1

uber_go_guide_cn

Uber Go 语言编码规范中文版. The Uber Go Style Guide .
7,286
star
2

gormt

database to golang struct
Go
2,307
star
3

caoguo

golang,微信小程序,电商系统
Vue
598
star
4

gowp

golang worker pool , Concurrency limiting goroutine pool
Go
488
star
5

shares

A-share quantitative system. A股量化系统
PLpgSQL
345
star
6

public

util toolkit for go.golang 通用函数包
Go
165
star
7

gohanlp

Golang RESTful Client for HanLP.中文分词 词性标注 命名实体识别 依存句法分析 语义依存分析 新词发现 关键词短语提取 自动摘要 文本分类聚类 拼音简繁转换 自然语言处理
Go
35
star
8

gofal

fractional api base on golang . golang math tools fractional molecular denominator 分数计算 分子 分母 运算
Go
18
star
9

esLog

elasticsearch log golang 的elasticsearch 日志封装,包括搜索,查询,添加等
Go
18
star
10

xxjServer

C++ general tools,Cross-platform Tool Library
C
12
star
11

openai

openai interface on golang
Go
11
star
12

PoetryRhyme

Poetry and rhyme,诗古,诗,诗歌
10
star
13

go-service

golang service,golang 服务创建库,跨平台支持,golang 系统服务注册库
Go
10
star
14

RTMP

rtmp online, 直播
C
10
star
15

consult

golang consul tools
Go
7
star
16

xxjwxc.github.io

个人博客地址,欢迎访问
HTML
6
star
17

oauth2

golang oauth2 authorization tools , oauth2通用授权系统
C
6
star
18

SQLserverCppConnection

SQLserver Connection for cpp ,c++ sqlserver 链接库
C++
5
star
19

jump

remote monitoring . 远程监控工具
Go
5
star
20

ginrest

restful api base on go gin
Go
4
star
21

GoSparkApi

golang 讯飞星火大模型 Go SparkApi
Go
4
star
22

douyin

golang 抖音弹幕消息
HTML
3
star
23

gomp3

mp3 decode mp3编解码器 golang版本 (pcm wav mp3)
C
2
star
24

xxjwxc

1
star
25

gocharts

golang echarts tools
Go
1
star