Giter Site home page Giter Site logo

grpc-gateway-file's Introduction

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

grpc-gateway-file's People

Contributors

black-06 avatar dependabot[bot] avatar n-r-w avatar anhnmt avatar

Stargazers

John R Patek Sr avatar  avatar  avatar  avatar Aleksei Rozhnov avatar subhasis maity avatar Andres Felipe Sierra Rojas  avatar Maxim avatar

Watchers

Maxim avatar  avatar  avatar

Forkers

max107 n-r-w anhnmt

grpc-gateway-file's Issues

Problem with processing two or more files

Hello. When processing two or more files I get an error: "parse multipart form failed multipart: NextPart: EOF". Could you please tell me if it is possible to process two or more files? In your MultipartFormHeader method I am not sure about the logic and the possibility of working with multiple files. This library is convenient, but if there is a possibility, please clarify this point. Thank you.

func MultipartFormHeader(server UploadServer, key string) (*multipart.FileHeader, error) {

   form, err := ParseMultipartForm(server)
   if err != nil {
   	return nil, errors.Wrapf(err, "parse multipart form failed")
   }
   if headers := form.File[key]; len(headers) > 0 {
   	return headers[0], nil
   }
   return nil, http.ErrMissingFile

}

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.