anchorIntercept

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 9, 2020 License: BSD-3-Clause Imports: 0 Imported by: 0

README

Go support for Protocol Buffers - Google's data interchange format

Build Status GoDoc

Google's data interchange format. Copyright 2010 The Go Authors. https://github.com/golang/protobuf

This package and the code it generates requires at least Go 1.9.

This software implements Go bindings for protocol buffers. For information about protocol buffers themselves, see https://developers.google.com/protocol-buffers/

介绍

在原本proto-gen-go的基础上魔改,衍生出proto-gen-lightbrother, 添加了gin插件和apidoc, 使用gin插件后,会根据proto自动生成对应的gin路由;使用apidoc插件后会自动生成proto对应的接口文档。

用法

安装
    git clone https://github.com/lightbrotherV/gin-protobuf
    cd gin-protobuf/protoc-gen-lightbrother
    go install
使用命令
    protoc --lightbrother_out=plugins=gin:. *.proto
    // 同时生成grpc
    protoc --lightbrother_out=plugins=grpc+gin:. *.proto
    // 同时生成接口文档
    protoc --lightbrother_out=plugins=grpc+gin+apidoc:. *.proto
使用gin

使用插件生成gin代码后,使用下方函数即可自动绑定gin路由

    func Register<packageName>GinServer(e *gin.Engine, server <packageName>GinServer)
使用中间件
  • 申明

    可以在service或者rpc上方添加,多个中间件用逗号","分割。 中间件调用时分先后, 先调用service上的中间件,再调用rpc的,申明多个中间件,按照申明先后排序调用。

    // `middleware:"auth,cors"`
  • 注册中间处理函数处理函数

    对于每个中间件,插件会生成对应的注册函数,函数名为Register<中间名>Middleware,对于上方例子,则会生成如下注册函数:

    func RegisterAuthMiddleware(f gin.HandlerFunc);
    func RegisterCorsMiddleware(f gin.HandlerFunc);
指定http请求方法

不指定http请求方法,就会默认使用GRPC方法,用method来指定http请求方法

    // `method:"GET"`

    // 同时指定中间件和请求方法如下
    // `middleware:"auth" method:"GET"`
忽略输入输出

为了能够在websocket以及formData中适用,所以需要忽略输出以及输出,不将body中的数据映射成对应proto对象,使用方法如下:

    // 忽略输入
    // `hasOutput:"false"`

    // 忽略输入
    // `hasInput:"false"`
返回结果

通过gin框架的上下文gin.Context的Get,Set方法来传递参数。 返回结构为:

{
    code: 0,
    message: "",
    data: {}
}

data为grpc的返回结构体,code为接口返回码,默认为0,message为错误信息,默认为空字符串。

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package descriptor provides functions for obtaining protocol buffer descriptors for generated Go types.
Package descriptor provides functions for obtaining protocol buffer descriptors for generated Go types.
Package jsonpb provides marshaling and unmarshaling between protocol buffers and JSON.
Package jsonpb provides marshaling and unmarshaling between protocol buffers and JSON.
Package proto converts data structures to and from the wire format of protocol buffers.
Package proto converts data structures to and from the wire format of protocol buffers.
protoc-gen-lightbrother
generator
The code generator for the plugin for the Google protocol buffer compiler.
The code generator for the plugin for the Google protocol buffer compiler.
generator/internal/remap
Package remap handles tracking the locations of Go tokens in a source text across a rewrite by the Go formatter.
Package remap handles tracking the locations of Go tokens in a source text across a rewrite by the Go formatter.
gin
grpc
Package grpc outputs gRPC service descriptions in Go code.
Package grpc outputs gRPC service descriptions in Go code.
plugin
Package plugin_go is a generated protocol buffer package.
Package plugin_go is a generated protocol buffer package.
Package ptypes contains code for interacting with well-known types.
Package ptypes contains code for interacting with well-known types.
any

Jump to

Keyboard shortcuts

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