Skip to content

go-mixed/kratos-protoc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

protoc plugin for kratos v2

The main code forked from kratos/protoc-gen-go-http, So you can build xxx_http.pb.go via this package

The version is always following kratos/protoc-gen-go-http

Usage

  • Named middleware
    • Call the middleware by name in the api/xxx.proto file
    • Multiple middleware
    • Middleware arguments
  • Custom Request/Response for HTTP
    • Custom request:Parse the request body with your own logic
    • Custom response:Write custom HEAD/BODY to http response, Or SSE/Chunked/Download etc.
    • Not for gRPC
  • File upload
    • RAW data of the request body
    • multipart/form-data request

Prerequisites

  1. Install protoc
  2. Install protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
  1. Build & Install protoc-gen-go-http
go install github.com/go-mixed/kratos-protoc/protoc-gen-go-http@latest

NO NEED to install official kratos/protoc-gen-go-http.

Generate xxx_http.pb.go

It's no different from the official kratos/protoc-gen-go-http

kratos proto client ./your_project/api/v1/examples/test.proto

or

protoc --proto_path=./ \
   --proto_path=./ \
   --proto_path=./third_party \
   --go_out=paths=source_relative:. \
   --go-grpc_out=paths=source_relative:. \
   --go-http_out=paths=source_relative:. \
   --openapi_out=paths=source_relative:. \
   ./your_project/api/v1/examples/test.proto

Proto file example

syntax = "proto3";

import "google/api/annotations.proto";
import "middleware/middleware.proto";
import "http/options.proto";

// middleware.caller example
rpc User(UserRequest) returns (UserResponse) {
    option (google.api.http) = {
      post: "/v1/user",
      body: "*",
    };
    option (middleware.caller) = {
      name: "auth",
      arguments: "arg1"
      arguments: "arg2"
    };
}

// stream response example
rpc Download(DownloadRequest) returns (EmptyResponse) {
    option (google.api.http) = {
      post: "/v1/download",
      body: "*",
    };
    option (http.options) = {
      custom_response: true,
    };
}

// upload request example
rpc Upload(EmptyResponse) returns (UploadResponse)
    option (google.api.http) = {
      post: "/v1/upload",
      body: "*",
    };
    option (http.options) = {
      custom_request: true,
    };
}

Development

If you modified the middleware/middleware.proto or http/options.proto, you MUST recompile it.

cd protoc-gen-go-http

protoc --proto_path=./ \
  --proto_path=./protoc-gen-go-http/pb \
  --go_out=paths=source_relative:. \
  pb/middleware/middleware.proto
  
protoc --proto_path=./ \
  --proto_path=./protoc-gen-go-http/pb \
  --go_out=paths=source_relative:. \
  pb/http/options.proto

Then, manually install the protoc-gen-go-http

go install .

About

Supported named middleware for kratos

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published