gogo / letmegrpc

[maintainer wanted] generates a web form gui from a grpc specification
BSD 3-Clause "New" or "Revised" License
419 stars 48 forks source link

Call a HTTP server (which uses grpcServer.ServeHTTP through a ServeMux) #27

Closed tgulacsi closed 8 years ago

tgulacsi commented 8 years ago

That would be very nice, to be able to call one of several grpc servers listening on the same HTTPS port, but under different paths. grpc.Server.ServeHTTP allows us to create such a server, but I don't know yet how to call it with letmegrpc. I need to use grpc.Invoke(..., "/prefixpath/package.service/method", ...) (instead of "/package/service.method").

I don't see an easy solution, as all the client code is generated, with constants as paths.

awalterschulze commented 8 years ago

There is a new NewHandler function that calls all public functions

func NewHandler(grpcAddr string, stringer func(req, resp interface{}) ([]byte, error), opts ...google_golang_org_grpc.DialOption) (net_http.Handler, error) {
    conn, err := google_golang_org_grpc.Dial(grpcAddr, opts...)
    if err != nil {
        return nil, err
    }
    mux := net_http.NewServeMux()
    MyTestClient := NewMyTestClient(conn)
    MyTestServer := NewHTMLMyTestServer(MyTestClient, stringer)
    mux.HandleFunc("/MyTest/UnaryCall", MyTestServer.UnaryCall)
    mux.HandleFunc("/MyTest/Downstream", MyTestServer.Downstream)
    mux.HandleFunc("/MyTest/Upstream", MyTestServer.Upstream)
    mux.HandleFunc("/MyTest/Bidi", MyTestServer.Bidi)
    return mux, nil
}

So you can hook these up in any way you want.

tgulacsi commented 8 years ago

But that calls MyClient.UnaryCall, which is Generated to call "/my test.MyTest/UnaryCall". This seems to me a feature request against protoc-gen-go, don't you think? I'm asking first here 'cause you answer promptly and kindly.

Walter Schulze notifications@github.com ezt írta (időpont: 2016. szept. 24., Szo 11:49):

There is a new NewHandler function that calls all public functions

func NewHandler(grpcAddr string, stringer func(req, resp interface{}) ([]byte, error), opts ...google_golang_org_grpc.DialOption) (net_http.Handler, error) { conn, err := google_golang_org_grpc.Dial(grpcAddr, opts...) if err != nil { return nil, err } mux := net_http.NewServeMux() MyTestClient := NewMyTestClient(conn) MyTestServer := NewHTMLMyTestServer(MyTestClient, stringer) mux.HandleFunc("/MyTest/UnaryCall", MyTestServer.UnaryCall) mux.HandleFunc("/MyTest/Downstream", MyTestServer.Downstream) mux.HandleFunc("/MyTest/Upstream", MyTestServer.Upstream) mux.HandleFunc("/MyTest/Bidi", MyTestServer.Bidi) return mux, nil }

So you can hook these up in any way you want.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/gogo/letmegrpc/issues/27#issuecomment-249356187, or mute the thread https://github.com/notifications/unsubscribe-auth/AAPoSri1Ho-kLgTZKezlQWGatz5aIgDsks5qtPI2gaJpZM4KFmme .

awalterschulze commented 8 years ago

I am not quite understanding what you mean.

What code here is generated by protoc-gen-go?

tgulacsi commented 8 years ago

I'm generating code with protoc-gen-gofast, grpc plugin; then

const prefix = "/dealer/"

mux := http.NewServeMux()
grpcServer := grpc.NewServer()
myServer.RegisterDbDealerServer(grpcServer, db_dealer.NewServer(nil))
mux.Handle(prefix, http.StripPrefix(prefix, grpcServer)
http.ListenAndServeTLS(":8081", "server.crt", "server.key", mux)
grpc.Invoke(ctx, prefix+method, args, reply, cc)

where `method = "db_dealer.DbDealer/Login".

This works, I've tried it. Now the question is: can this prefix incorporated in protoc-gen-gogo and/or letmegrpc?

Ooops! Now I've found out that I do use the code generated by protoc-gen-gofast, without modification! And I use a WithUnaryInterceptor DialOption and in that I call the invoker with prefix+method as the method name - so after all maybe you're right, and nothing needs to be changed, I only need my own call of tmpprotos.Serve with this special DialOption!

awalterschulze commented 8 years ago

Ok so this issue can be closed?

tgulacsi commented 8 years ago

Think so. Thank you for your patience, to be my rubber duck debugger!

Walter Schulze notifications@github.com ezt írta (időpont: 2016. szept. 24., Szo 17:19):

Ok so this issue can be closed?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/gogo/letmegrpc/issues/27#issuecomment-249370041, or mute the thread https://github.com/notifications/unsubscribe-auth/AAPoSnVF7URGJzvOAVVeWHqzaV0dfasBks5qtT96gaJpZM4KFmme .