gmux

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: Apache-2.0 Imports: 13 Imported by: 2

README

gmux

gmux is a Go library for simultaneously serving net/http and gRPC requests on a single port.

gmux is derived from cmux, enabling mTLS at the expense of reduced flexibility. Whereas cmux operates at the transport layer and enables multiplexing arbitrary application layer protocols, gmux operates at the application layer (on top of HTTP with or without TLS), to enable better integration with net/http and crypto/tls.

Example

s := &http.Server{
	Addr:    ":8080",
	Handler: myHandler,
}

// Configure the server with gmux. The returned net.Listener will receive gRPC connections,
// while all other requests will be handled by s.Handler.
grpcListener, err := gmux.ConfigureServer(s, nil)
if err != nil {
	log.Fatal(err)
}

grpcServer := grpc.NewServer()
go grpcServer.Serve(grpcListener)
log.Fatal(s.ListenAndServeTLS("cert.pem", "key.pem"))

License

This software is licensed under the Apache License 2.0.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigureServer

func ConfigureServer(srv *http.Server, conf *http2.Server) (grpcListener net.Listener, _ error)

ConfigureServer configures srv to identify gRPC connections and send them to the returned net.Listener, suitable for passing to grpc.Server.Serve, while all other HTTP requests will be handled by srv.

ConfigureServer works with or without TLS enabled.

When TLS is enabled, ConfigureServer relies on ALPN. ConfigureServer internally calls http2.ConfigureServer(srv, conf) to configure HTTP/2 support, and defines an alternative srv.TLSNextProto "h2" handler. When using TLS, the gRPC listener returns secure connections; the gRPC server must not also be configured to wrap the connection with TLS.

When TLS is not enabled, ConfigureServer relies on h2c prior knowledge, wrapping srv.Handler. It is therefore necessary to set srv.Handler before calling ConfigureServer.

The returned listener will be closed when srv.Shutdown is called. The returned listener's Addr() method does not correspond to the configured HTTP server's listener(s) in any way, and cannot be relied upon for forming a connection URL.

Types

This section is empty.

Jump to

Keyboard shortcuts

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