GithubHelp home page GithubHelp logo

protoc-gen-go-http's Introduction

Description

protoc-gen-go-http generates HTTP-wrappers for gRPC servers generated by protoc-gen-go. It is implemented as a protoc plugin.

A typical use case is when you started using gRPC but there is legacy services that still use HTTP/1.1. protoc-gen-go-http generates fast wrappers that can be used as handler functions.

Suppose you have a simple service:

syntax = "proto3";

service Example {
    // Simple request/response.
    rpc GetPerson(Query) returns (Person) {}
    // Server streaming (ignored).
    rpc ListPeople(Query) returns (stream Person) {}
}

protoc-gen-go will generate the following interface:

type ExampleServer interface {
	// Simple request/response.
	GetPerson(context.Context, *Query) (*Person, error)
	// Server streaming (ignored).
	ListPeople(*Query, Example_ListPeopleServer) error
}

Then with protoc-gen-go-http you can get your HTTP handler functions:

type HTTPExampleServer struct {
	srv ExampleServer
	cdc codec.Codec
}

func NewHTTPExampleServer(srv ExampleServer, cdc codec.Codec) *HTTPExampleServer {
	return &HTTPExampleServer{
		srv: srv,
		cdc: cdc,
	}
}

func (wpr *HTTPExampleServer) GetPerson(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	arg := Query{}
	err := wpr.cdc.ReadRequest(r.Body, &arg)
	if err != nil {
		wpr.cdc.WriteError(w, err)
		return
	}
	grpcResp, err := wpr.srv.GetPerson(r.Context(), &arg)
	if err != nil {
		wpr.cdc.WriteError(w, err)
		return
	}
	wpr.cdc.WriteResponse(w, grpcResp)
}

A codec.Codec is used to read requests and write responses/errors. You can use the codec.DefaultCodec or implement a custom one.

protoc-gen-go-http relies heavily on grpc-gateway code, but it's faster because less time is spent on marshalling/unmarshalling. grpc-gateway first unmarshals POST body, then marshals it into a protobuf blob, then unmarshals a protobuf response, etc. protoc-gen-go-http just unmarshals POST body into a "native" gRPC struct, gets response struct and marshals it.

All stream-based gRPC methods are ignored.

Installation

go get -u github.com/lazada/protoc-gen-go-http

Usage

Add --go-http_out=. as a parameter to protoc, for example:

protoc --go_out=plugins=grpc:. --go-http_out=. example.proto

protoc-gen-go-http's People

Watchers

 avatar  avatar

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.