Categorygithub.com/black-06/grpc-gateway-file
modulepackage
0.1.2
Repository: https://github.com/black-06/grpc-gateway-file.git
Documentation: pkg.go.dev

# README

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 https://github.com/grpc-ecosystem/grpc-gateway/issues/2557

# Packages

No description provided by the author

# Functions

NewFormData returns a new FormData.
ParseBoundary parses the boundary parameter from the given metadata.
ProcessMultipartUpload processes the provided multipart upload.
SaveMultipartFile saves the provided multipart file to the given path.
ServeContent comes from http.ServeContent, and made some adaptations for DownloadServer.
ServeFile comes from http.ServeFile, and made some adaptations for DownloadServer.
WithFileForwardResponseOption - forwardResponseOption is an option that will be called on the relevant context.Context, http.ResponseWriter, and proto.Message before every forwarded response.
WithFileIncomingHeaderMatcher returns a ServeMuxOption representing a headerMatcher for incoming request to gateway.
WithHTTPBodyMarshaler returns a ServeMuxOption which associates inbound and outbound Marshalers to a MIME type in mux.

# Variables

ErrInvalidRange - invalid range.
ErrSizeLimitExceeded - message too large.

# Structs

FormData is a wrapper around multipart.Form.