package
0.3.7
Repository: https://github.com/goto/salt.git
Documentation: pkg.go.dev

# README

server

server package helps in setting up grpc server, http server or a mux server that runs both http and grpc on same port on a host It exposes multiple options to configure each of the servers.

Usage

HTTP server

    // context to be Done when SIGINT or SIGTERM is received
	ctx, cancelFunc := context.WithCancel(server.HandleSignals(context.Background()))
	defer cancelFunc()

    // create server.HTTPServer
	s, err := server.NewHTTP(server.Config{
		Port: httpPort,
	})
	if err != nil {
		panic(err)
	}

    // add a handler
	s.RegisterHandler("/ping", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "pong")
	}))

    // start serving
	go s.Serve()
    // wait for ctx context to be set done, by SIGINT or SIGTERM
	<-ctx.Done()

    // set a timer for graceful shutdown, if expired will force kill the server
	shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*10)
	defer shutdownCancel()

	s.Shutdown(shutdownCtx)

GRPC server

    // grpc middlewares
    var GRPCMiddlewaresInterceptor = grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
	    grpc_recovery.UnaryServerInterceptor(),
	    grpc_ctxtags.UnaryServerInterceptor(),
	    grpc_prometheus.UnaryServerInterceptor,
	    grpc_zap.UnaryServerInterceptor(zap.NewExample()),
    ))

    // context to be Done when SIGINT or SIGTERM is received
	ctx, cancelFunc := context.WithCancel(server.HandleSignals(context.Background()))
	defer cancelFunc()

    // create server.GRPCServer with middlewares
	s, err := server.NewGRPC(server.Config{
		Port: grpcPort,
	}, server.WithGRPCServerOptions(GRPCMiddlewaresInterceptor))
	if err != nil {
		panic(err)
	}

    // register a grpc service desc and server implementation instance
	s.RegisterService(&commonv1.CommonService_ServiceDesc,
		common.New(Server),
	)

    // start serving
	go s.Serve()
    // wait for ctx context to be set done, by SIGINT or SIGTERM
	<-ctx.Done()

    // set a timer for graceful shutdown, if expired will force kill the server
	shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*10)
	defer shutdownCancel()

	s.Shutdown(shutdownCtx)

GRPC gateway

GRPCGateway can be used to add GRPC Gateway generated proxy handlers to HTTPServer

    gw, err := server.NewGateway("", grpcClientPort)
	if err != nil {
		panic(err)
	}
    // Use the grpc gateway generated function to register http handlers
	gw.RegisterHandler(ctx, commonv1.RegisterCommonServiceHandlerFromEndpoint)

    // set gateway on HTTPServer with /api prefix
	s.SetGateway("/api", gw)

Mux Server

MuxServer can be used to run GRPC and HTTP servers on same port on a host. Internally it uses cmux to route requests to specific server

    // grpc middlewares
    var GRPCMiddlewaresInterceptor = grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
	    grpc_recovery.UnaryServerInterceptor(),
	    grpc_ctxtags.UnaryServerInterceptor(),
	    grpc_prometheus.UnaryServerInterceptor,
	    grpc_zap.UnaryServerInterceptor(zap.NewExample()),
    ))

    // context to be Done when SIGINT or SIGTERM is received
	ctx, cancelFunc := context.WithCancel(server.HandleSignals(context.Background()))
	defer cancelFunc()

    // create server.MuxServer with grpc middlewares
	s, err := server.NewMux(server.Config{
		Port: muxPort,
	}, server.WithMuxGRPCServerOptions(GRPCMiddlewaresInterceptor))
	if err != nil {
		panic(err)
	}

    // use same port for grpc-gateway grpc client to proxy requests to
	grpcClientPort := muxPort
    // create, set handlers and set gateway on MuxServer
	gw, err := server.NewGateway("", grpcClientPort)
	if err != nil {
		panic(err)
	}
	gw.RegisterHandler(ctx, commonv1.RegisterCommonServiceHandlerFromEndpoint)
	s.SetGateway("/api", gw)

    // add additional http handlers on MuxServer
	s.RegisterHandler("/ping", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "pong")
	}))

    // register grpc service on MuxServer
	s.RegisterService(&commonv1.CommonService_ServiceDesc,
		common.New(Server),
	)

    // start serving
	go s.Serve()
    // wait for ctx context to be set done, by SIGINT or SIGTERM
	<-ctx.Done()
	// clean anything that needs to be closed etc like common server implementation etc
    // set a timer for graceful shutdown, if expired will force kill the server
	shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*10)
	defer shutdownCancel()

	s.Shutdown(shutdownCtx)

Example

For usage example have a look at this - example.

# Packages

No description provided by the author

# Functions

HandleSignals wraps context so that it is marked done when one of SIGINT or SIGTERM is received.
NewGateway creates a new server.GRPCGateway to proxy grpc requests to specified host and port.
NewGRPC creates a new server.GRPCServer instance with given config and server.GRPCOption Deprecated: Prefer `mux` package instead of this.
NewHTTP creates a new server.HTTPServer instance with given config and server.HTTPOption Deprecated: Prefer `mux` package instead of this.
NewMux creates a new server.MuxServer instance with given config and server.MuxOption Deprecated: Prefer `mux` package instead of this.
WithGatewayMuxOptions sets []runtime.ServeMuxOption for server.GRPCGateway.
WithGRPCGateway sets runtime.ServeMux instance for server.GRPCGateway.
WithGRPCServer sets grpc.Server instance for server.GRPCServer.
WithGRPCServerOptions sets []grpc.ServerOption for server.GRPCServer.
WithHTTPServer sets http.Server instance for server.HTTPServer.
WithMuxGRPCServer sets grpc.Server instance for the internal grpc server of server.MuxServer.
WithMuxGRPCServerOptions sets []grpc.ServerOption for the internal grpc server of server.MuxServer.
WithMuxHTTPServer sets http.Server instance for the internal http server of server.MuxServer.

# Structs

Config to set the host and port for different servers.
GRPCGateway helps in registering grpc-gateway proxy handlers for a grpc service on server.HTTPServer.
GRPCServer is an server to serve grpc requests Deprecated: Prefer `mux` package instead of this.
HTTPServer is an server to serve http requests Deprecated: Prefer `mux` package instead of this.
MuxServer is an server to serve grpc requests and http requests on same host and port Deprecated: Prefer `mux` package instead of this.

# Type aliases

GatewayOption sets configs, properties or other parameters for the server.GRPCGateway.
GRPCOption sets configs, properties or other parameters for the server.GRPCServer.
HTTPOption sets configs, properties or other parameters for the server.HTTPServer.
MuxOption sets configs, properties or other parameters for the server.MuxServer.