Giter VIP home page Giter VIP logo

protoc-gen-go-gin's Introduction

protoc-gen-go-gin

修改自 kratos v2

从 protobuf 文件中生成使用 gin 的 http rpc 服务

安装

请确保安装了以下依赖:

注意由于使用 embed 特性,Go 版本必须大于 1.16

go install github.com/liwanmin/protoc-gen-go-gin@latest

使用说明

例子见: example

proto 文件约定

默认情况下 rpc method 命名为 方法+资源,使用驼峰方式命名,生成代码时会进行映射

方法映射方式如下所示:

  • "GET", "FIND", "QUERY", "LIST", "SEARCH" --> GET
  • "POST", "CREATE" --> POST
  • "PUT", "UPDATE" --> PUT
  • "DELETE" --> DELETE
service BlogService {
  rpc CreateArticle(Article) returns (Article) {}
  // 生成 http 路由为 post: /article
}

除此之外还可以使用 google.api.http option 指定路由,可以通过添加 additional_bindings 使一个 rpc 方法对应多个路由

// blog service is a blog demo
service BlogService {
  rpc GetArticles(GetArticlesReq) returns (GetArticlesResp) {
    // 
    // 可以通过添加 additional_bindings 使一个 rpc 方法对应多个路由
    option (google.api.http) = {
      get: "/v1/articles"
      additional_bindings {
        get: "/v1/author/{author_id}/articles"
      }
    };
  }
}

文件生成

 	protoc -I ./example/api \
    --openapi_out=fq_schema_naming=true,default_response=false:. \
	--go_out ./example/api --go_opt=paths=source_relative \
	--go-gin_out ./example/api --go-gin_opt=paths=source_relative \
	--go-errors_out=paths=source_relative:./example/api \
	--validate_out=paths=source_relative,lang=go:./example/api \
	example/api/product/app/v1/v1.proto
	protoc-go-inject-tag -input=./example/api/product/app/v1/v1.pb.go

相关介绍

待发布

  • Go工程化(四) API 设计上: 项目结构 & 设计
  • Go工程化(五) API 设计下: 基于 protobuf 自动生成 gin 代码

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.