gohost

package module
v2.1.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2018 License: MIT Imports: 13 Imported by: 0

README

gohost

Build Status Go Report Card GoDoc

A tool for hosting a Go service with gRPC and HTTP endpoints.

Installation

go get -u github.com/eleniums/gohost

Prerequisites

  • Requires Go 1.9 or later
  • Uses dep for dependencies
  • Uses grpc-go for gRPC endpoints
  • Uses grpc-gateway for HTTP endpoints
  • See the full list of imported packages here

Example

Sample service implementation:

// Service contains the implementation for the gRPC service.
type Service struct{}

// NewService creates a new instance of Service.
func NewService() *Service {
	return &Service{}
}

// Hello will return a personalized greeting.
func (s *Service) Hello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {
	// create greeting
	greeting := "Hello!"
	if in.Name != "" {
		greeting = fmt.Sprintf("Hello %v!", in.Name)
	}

	log.Printf("Received request from: %v", in.Name)

	// return response
	return &pb.HelloResponse{
		Greeting: greeting,
	}, nil
}

Use the Hoster struct to serve up gRPC and HTTP endpoints:

// create the service
service := hello.NewService()

// create the hoster
hoster := gohost.NewHoster()
hoster.GRPCAddr = *grpcAddr
hoster.HTTPAddr = *httpAddr
hoster.DebugAddr = *debugAddr
hoster.EnableDebug = *enableDebug
hoster.CertFile = *certFile
hoster.KeyFile = *keyFile
hoster.InsecureSkipVerify = *insecureSkipVerify
hoster.MaxSendMsgSize = *maxSendMsgSize
hoster.MaxRecvMsgSize = *maxRecvMsgSize

hoster.RegisterGRPCServer(func(s *grpc.Server) {
	pb.RegisterHelloServiceServer(s, service)
})

hoster.RegisterHTTPGateway(pb.RegisterHelloServiceHandlerFromEndpoint)

// start the server
err := hoster.ListenAndServe()
if err != nil {
	log.Fatalf("Unable to start the server: %v", err)
}

See the full example here.

Documentation

Index

Constants

View Source
const (
	// DefaultGRPCAddr is the default address for the gRPC endpoint.
	DefaultGRPCAddr = "127.0.0.1:50051"

	// DefaultHTTPAddr is the default address for the HTTP endpoint.
	DefaultHTTPAddr = "127.0.0.1:9090"

	// DefaultDebugAddr is the default address for the debug endpoint (/debug/pprof and /debug/vars).
	DefaultDebugAddr = "127.0.0.1:6060"

	// DefaultMaxSendMsgSize is the default max send message size, per gRPC
	DefaultMaxSendMsgSize = 1024 * 1024 * 4

	// DefaultMaxRecvMsgSize is the default max receive message size, per gRPC
	DefaultMaxRecvMsgSize = 1024 * 1024 * 4
)

Variables

This section is empty.

Functions

This section is empty.

Types

type GRPCServer

type GRPCServer func(s *grpc.Server)

GRPCServer is used to register a gRPC server.

type HTTPGateway

type HTTPGateway func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error)

HTTPGateway is used to register a HTTP gateway for forwarding requests to a gRPC endpoint.

type Hoster

type Hoster struct {
	// GRPCAddr is the endpoint (host and port) on which to host the gRPC service. Default is 127.0.0.1:50051. May be left blank if no gRPC servers have been registered.
	GRPCAddr string

	// HTTPAddr is the endpoint (host and port) on which to host the HTTP service. Default is 127.0.0.1:9090. May be left blank if no HTTP gateways have been registered.
	HTTPAddr string

	// DebugAddr is the endpoint (host and port) on which to host the debug endpoint (/debug/pprof and /debug/vars). Default is 127.0.0.1:6060. May be left blank if EnableDebug is false.
	DebugAddr string

	// CertFile is the certificate file for use with TLS. May be left blank if using insecure mode.
	CertFile string

	// KeyFile is the private key file for use with TLS. May be left blank if using insecure mode.
	KeyFile string

	// InsecureSkipVerify will cause verification of the host name during a TLS handshake to be skipped if set to true.
	InsecureSkipVerify bool

	// HTTPHandler is used to register a handler that can optionally be added to the HTTP endpoint. Leave blank to use default mux.
	HTTPHandler func(mux *runtime.ServeMux) http.Handler

	// EnableDebug will enable the debug endpoint (/debug/pprof and /debug/vars). The debug endpoint address is defined by DebugAddr.
	EnableDebug bool

	// MaxSendMsgSize will change the size of the message that can be sent from the service.
	MaxSendMsgSize int

	// MaxRecvMsgSize will change the size of the message that can be received by the service.
	MaxRecvMsgSize int

	// UnaryInterceptors is an array of unary interceptors to be used by the service. They will be executed in order, from first to last.
	UnaryInterceptors []grpc.UnaryServerInterceptor

	// StreamInterceptors is an array of stream interceptors to be used by the service. They will be executed in order, from first to last.
	StreamInterceptors []grpc.StreamServerInterceptor
	// contains filtered or unexported fields
}

Hoster is used to serve gRPC and HTTP endpoints.

func NewHoster

func NewHoster() *Hoster

NewHoster creates a new hoster instance with defaults set.

func (*Hoster) ListenAndServe

func (h *Hoster) ListenAndServe() error

ListenAndServe creates and starts the server.

func (*Hoster) RegisterGRPCServer

func (h *Hoster) RegisterGRPCServer(servers ...GRPCServer)

RegisterGRPCServer will add a function for registering a gRPC server. The function is invoked when ListenAndServe is called.

func (*Hoster) RegisterHTTPGateway

func (h *Hoster) RegisterHTTPGateway(gateways ...HTTPGateway)

RegisterHTTPGateway will add a function for registering a HTTP gateway. The function is invoked when ListenAndServe is called.

Directories

Path Synopsis
examples
hello/proto
Package hello is a generated protocol buffer package.
Package hello is a generated protocol buffer package.
test/proto
Package test is a generated protocol buffer package.
Package test is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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