go-masonry / mortar

Mortar is a GO framework/library for building gRPC (and REST) web services.

Home Page:https://go-masonry.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[feature] make newWebService injectable, server.webService public

xqbumu opened this issue · comments

Does it ok to use oneport service more conveniently by replace newWebService and extend server.webService ?

https://github.com/go-masonry/mortar/blob/master/constructors/service.go#L24

https://github.com/go-masonry/mortar/blob/master/http/server/builders.go#L224

thanks

commented

@xqbumu server.WebService is an interface, but it is a general one. If you want to run your service and serve everything through one listener (gRPC, REST) you need to use a mux.
You can find an example here:

listener, err := net.Listen("tcp", "localhost:8888")

@xqbumu server.WebService is an interface, but it is a general one. If you want to run your service and serve everything through one listener (gRPC, REST) you need to use a mux. You can find an example here:

listener, err := net.Listen("tcp", "localhost:8888")

the example I have read.

If we can just replace server.newWebService in https://github.com/go-masonry/mortar/blob/master/http/server/builders.go#L224, we could switch oneport server and multiport server conveniently.

maybe I should fork mortar and write a demo when I have part time.

sorry for my poor english.

mortar is very nice. ^_^

commented

It is possible using the same builder. You just need to pass the "same" Listener so it will always listen on 1 port.
Look at the example I linked. You need to have a mux which is also a Listener. It is there to check what kind of an incoming request and route it to gRPC or REST web service.

func (os *onePortSuite) makeBuilderAndCmux() {
	listener, err := net.Listen("tcp", "localhost:8888")
	os.Require().NoError(err)
	os.cMux = cmux.New(listener)
	restL := os.cMux.Match(cmux.HTTP1())
	grpcL := os.cMux.Match(cmux.Any())

	os.builder = Builder().
		SetLogger(func(ctx context.Context, format string, args ...interface{}) {
			os.T().Logf(format, args...)
		}).
		// GRPC
		SetCustomListener(grpcL).
		RegisterGRPCAPIs(func(srv *grpc.Server) {
			demopackage.RegisterDemoServer(srv, new(demoImpl))
		}).
		// REST 1 with GRPC Gateway
		AddRESTServerConfiguration().
		SetCustomListener(restL).
		RegisterGRPCGatewayHandlers(func(mux *runtime.ServeMux, endpoint string) error {
			return demopackage.RegisterDemoHandlerFromEndpoint(context.Background(), mux, endpoint, []grpc.DialOption{grpc.WithInsecure()})
		}).
		BuildRESTPart()
}