Skip to content

black-06/grpc-gateway-file

Repository files navigation

gRPC-Gateway-file

Are you confused about how to integrate file upload and download into gRPC-Gateway?

Try gRPC-Gateway-file, it is a plugin of the gRPC-Gateway.

It allows you to define upload and download api in the gRPC service proto file.

Feat

  • For upload, you no longer have to manually add routes to the mux
  • For download, It supports Resume Transfer Protocol.
  • Best of all, you can implement them directly in gRPC service.

Usage

  1. Get it.
go get -u github.com/black-06/grpc-gateway-file
  1. Defining gRPC proto file.
    The result of download api, and the request of upload api, must be "stream google.api.HttpBody"
import "google/api/annotations.proto";
import "google/api/httpbody.proto";

service Service {
  rpc DownloadFile (XXX) returns (stream google.api.HttpBody) {
    option (google.api.http) = { get: "/api/file/download" };
  };

  rpc UploadFile (stream google.api.HttpBody) returns (XXX) {
    option (google.api.http) = { post: "/api/file/upload", body: "*" };
  };
}
  1. Generate golang code as usual.
  2. Using gRPC-Gateway-file in gRPC-Gateway.
import gatewayfile "github.com/black-06/grpc-gateway-file"

mux := runtime.NewServeMux(
	gatewayfile.WithFileIncomingHeaderMatcher(),
	gatewayfile.WithFileForwardResponseOption(),
	gatewayfile.WithHTTPBodyMarshaler(),
)
  1. Done, enjoy it

A more complete example is here

Known issues

HTTPBodyMarshaler will change the Delimiter of all server-stream to empty.

Be careful if you have other server streams api.

More context see grpc-ecosystem/grpc-gateway#2557

About

file upload and download in gRPC-Gateway

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages