plaingrpc

package module
v0.0.0-...-c8accf1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2021 License: MIT Imports: 11 Imported by: 0

README

go-plain-grpc

gRPC over HTTP and Websockets

NOTICE: The project is still in early stages of development, and not all gRPC functionality has been verified to work. Use at your own risk and contribute back if you find something that is missing.


Why

gRPC is an amazing framework. But sometimes it is impossible to use due to the underlying transport layer. This project allows to use your already existing codebase, and add interceptors and a proxy to make your gRPC work over normal HTTP and Websockets - no generation necessary!


Example

This example shows how to add go-plain-grpc to helloworld example from gRPC-go repository.

Client
package main

import (
    "net/url"

    "google.golang.org/grpc"
    "voy.technology/plain-grpc"

    pb "google.golang.org/grpc/examples/helloworld/helloworld"
)

var endpointFlag = flag.String("endpoint", "", "Endpoint to the serve")

func main() {
    flag.Parse()

    // Error parsing removed for brevity
    endpoint, _ := url.Parse(*endpointFlag)

    opts := []grpc.DialOption{
        grpc.WithInsecure(),
    }

    // gRPC doesn't like Scheme in front of the connection url. We can use it to
    // our advantage to select when we want to use go-plain-grpc.
    if endpoint.Scheme == "http" {
        i := plaingrpc.NewHijacker(endpoint.String())
        opts = append(opts,
            grpc.WithUnaryInterceptor(i.UnaryInterceptor),
            grpc.WithStreamInterceptor(i.StreamInterceptor))
    }
    
    conn, _ := grpc.Dial(entrypoint.String(), opts...)
    defer conn.Close()

    c := pb.NewGreeterClient(conn)

    // Proceed as normal
}
Server
package main

// These can be combined into a single service using cmux
var httpPortFlag = flag.Int("http-port", 8080, "HTTP port")
var grpcPortFlag = flag.Int("grpc-port", 8888, "gRPC port")

func main() {
    flag.Parse()
    lis, _ := net.Listen("tcp", fmt.Sprintf(":%d", *grpcPortFlag))

    s := grpc.NewServer()
    pb.RegisterGreeterServer(s, &server{})
    
    go s.Serve(lis)

    // Proxy forwards all requests from HTTP server to the gRPC server
    proxy, _ := plaingrpc.NewProxy(s, lis.Addr())

    // Register the endpoints
    http.Handle("/", proxy)
    http.ListenAndServe(fmt.Sprintf(":%d", *httpPortFlag), nil)
}

Special Thanks

Special Thanks to Mike Cheng for grpc-over-http on which this project is roughly based on.

Documentation

Overview

Package plaingrpc allows to use gRPC code without having to regenerate any code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientStream

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

ClientStream implements the grpc.ClientStream TODO: Add final implementation to a lot of them. This is so basic for now.

func (*ClientStream) CloseSend

func (cs *ClientStream) CloseSend() error

func (*ClientStream) Context

func (cs *ClientStream) Context() context.Context

func (*ClientStream) Header

func (cs *ClientStream) Header() (metadata.MD, error)

func (*ClientStream) RecvMsg

func (cs *ClientStream) RecvMsg(m interface{}) error

func (*ClientStream) SendMsg

func (cs *ClientStream) SendMsg(m interface{}) error

func (*ClientStream) Trailer

func (cs *ClientStream) Trailer() metadata.MD

type Hijacker

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

ClientInterceptor hijacks the gRPC

func NewHijacker

func NewHijacker(ep string) *Hijacker

func (*Hijacker) StreamInterceptor

func (h *Hijacker) StreamInterceptor(ctx context.Context, desc *grpc.StreamDesc, _ *grpc.ClientConn, method string, _ grpc.Streamer, _ ...grpc.CallOption) (grpc.ClientStream, error)

func (*Hijacker) UnaryInterceptor

func (h *Hijacker) UnaryInterceptor(ctx context.Context, method string, req, res interface{}, _ *grpc.ClientConn, _ grpc.UnaryInvoker, _ ...grpc.CallOption) error

type Proxy

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

func NewProxy

func NewProxy(s *grpc.Server, addr net.Addr) (*Proxy, error)

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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