h2g

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 7 Imported by: 0

README

H2G

contracted but not simple

h2g is a plugins to test grpc service when the testing tool does not support grpc in iris

How to use

Download in your project

go get github.com/culionbear/h2g@latest

//Use H2G in iris
func main() {
    m := iris.New()
    //New H2G Manager
    g := h2g.New(&h2g.Config{
        Handler: map[string]h2g.Func{
            //server name : GrpcClient
            "hello.Debug": func() interface{} {
                //New Grpc Connect
                conn, err := grpc.Dial(model.GrpcAddr, grpc.WithInsecure())
                if err != nil {
                    log.Fatalf("did not connect: %v", err)
                }
                //New Grpc Client with protobuf
                return pb.NewClient(conn)
            },
        },
        //get service name from url
        Service: "service",
        //get method name from url
        Method: "method",
    })
    //m.Post("/grpc/{service}/{method}", g.Service)
    m.Post(g.Path("/grpc"), g.Service)
    m.Run(iris.Addr("127.0.0.1:80"))
}
//Protobuf file
syntax = "proto3";

message Request{
  string msg = 1;
}

message Response{
    string msg = 1;
}

service Hello{
  rpc SayHello(Request) returns (Response);
}

Now we can use Postman or Curl or the other to test grpc server.

Post: http://127.0.0.1/grpc/hello.Debug/SayHello

with request data: {"msg": "hello"}

xpected output: {"msg": "xxx"}.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Handler map[string]Func
	Service string
	Method  string
}

type Func

type Func func() interface{}

type Manager

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

func New

func New(c *Config) *Manager

func (*Manager) Path

func (m *Manager) Path(uri string) string

func (*Manager) Service

func (m *Manager) Service(ctx iris.Context)

Jump to

Keyboard shortcuts

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