gatewayfile

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 19 Imported by: 0

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SaveMultipartFile

func SaveMultipartFile(header *multipart.FileHeader, path string) error

SaveMultipartFile saves the provided multipart file to the given path.

func ServeContent

func ServeContent(
	server downloadServer, content io.ReadSeeker, contentType, name string, modTime time.Time, size int64,
) error

ServeContent comes from http.ServeContent, and made some adaptations for DownloadServer

func ServeFile

func ServeFile(server downloadServer, contentType, path string) error

ServeFile comes from http.ServeFile, and made some adaptations for DownloadServer

func WithFileForwardResponseOption

func WithFileForwardResponseOption() runtime.ServeMuxOption

WithFileForwardResponseOption - forwardResponseOption is an option that will be called on the relevant context.Context, http.ResponseWriter, and proto.Message before every forwarded response.

func WithFileIncomingHeaderMatcher

func WithFileIncomingHeaderMatcher() runtime.ServeMuxOption

WithFileIncomingHeaderMatcher returns a ServeMuxOption representing a headerMatcher for incoming request to gateway. This matcher will be called with each header in http.Request. If matcher returns true, that header will be passed to gRPC context. To transform the header before passing to gRPC context, matcher should return modified header.

func WithHTTPBodyMarshaler

func WithHTTPBodyMarshaler() runtime.ServeMuxOption

WithHTTPBodyMarshaler returns a ServeMuxOption which associates inbound and outbound Marshalers to a MIME type in mux.

Types

type FormData

type FormData struct {
	// contains filtered or unexported fields
}

FormData is a wrapper around multipart.Form.

func NewFormData

func NewFormData(server uploadServer) (*FormData, error)

NewFormData returns a new FormData.

func (*FormData) Files

func (f *FormData) Files(key string) []*multipart.FileHeader

Files returns the files for the provided form key

func (*FormData) FirstFile

func (f *FormData) FirstFile(key string) *multipart.FileHeader

FirstFile returns the first file for the provided form key

func (*FormData) FirstValue

func (f *FormData) FirstValue(key string) string

FirstValue returns the first value for the provided form key

func (*FormData) Values

func (f *FormData) Values(key string) []string

Values returns the values for the provided form key

Directories

Path Synopsis
proto
Package proto is a reverse proxy.
Package proto is a reverse proxy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL