...

Source file src/github.com/grpc-ecosystem/grpc-gateway/examples/internal/server/main.go

Documentation: github.com/grpc-ecosystem/grpc-gateway/examples/internal/server

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"net/http"
     7  
     8  	"github.com/golang/glog"
     9  	examples "github.com/grpc-ecosystem/grpc-gateway/examples/internal/proto/examplepb"
    10  	"github.com/grpc-ecosystem/grpc-gateway/runtime"
    11  	"google.golang.org/grpc"
    12  )
    13  
    14  // Run starts the example gRPC service.
    15  // "network" and "address" are passed to net.Listen.
    16  func Run(ctx context.Context, network, address string) error {
    17  	l, err := net.Listen(network, address)
    18  	if err != nil {
    19  		return err
    20  	}
    21  	defer func() {
    22  		if err := l.Close(); err != nil {
    23  			glog.Errorf("Failed to close %s %s: %v", network, address, err)
    24  		}
    25  	}()
    26  
    27  	s := grpc.NewServer()
    28  	examples.RegisterEchoServiceServer(s, newEchoServer())
    29  	examples.RegisterFlowCombinationServer(s, newFlowCombinationServer())
    30  	examples.RegisterNonStandardServiceServer(s, newNonStandardServer())
    31  
    32  	abe := newABitOfEverythingServer()
    33  	examples.RegisterABitOfEverythingServiceServer(s, abe)
    34  	examples.RegisterStreamServiceServer(s, abe)
    35  	examples.RegisterResponseBodyServiceServer(s, newResponseBodyServer())
    36  
    37  	go func() {
    38  		defer s.GracefulStop()
    39  		<-ctx.Done()
    40  	}()
    41  	return s.Serve(l)
    42  }
    43  
    44  // RunInProcessGateway starts the invoke in process http gateway.
    45  func RunInProcessGateway(ctx context.Context, addr string, opts ...runtime.ServeMuxOption) error {
    46  	mux := runtime.NewServeMux(opts...)
    47  
    48  	examples.RegisterEchoServiceHandlerServer(ctx, mux, newEchoServer())
    49  	examples.RegisterFlowCombinationHandlerServer(ctx, mux, newFlowCombinationServer())
    50  	examples.RegisterNonStandardServiceHandlerServer(ctx, mux, newNonStandardServer())
    51  
    52  	abe := newABitOfEverythingServer()
    53  	examples.RegisterABitOfEverythingServiceHandlerServer(ctx, mux, abe)
    54  	examples.RegisterStreamServiceHandlerServer(ctx, mux, abe)
    55  	examples.RegisterResponseBodyServiceHandlerServer(ctx, mux, newResponseBodyServer())
    56  
    57  	s := &http.Server{
    58  		Addr:    addr,
    59  		Handler: mux,
    60  	}
    61  
    62  	go func() {
    63  		<-ctx.Done()
    64  		glog.Infof("Shutting down the http gateway server")
    65  		if err := s.Shutdown(context.Background()); err != nil {
    66  			glog.Errorf("Failed to shutdown http gateway server: %v", err)
    67  		}
    68  	}()
    69  
    70  	if err := s.ListenAndServe(); err != http.ErrServerClosed {
    71  		glog.Errorf("Failed to listen and serve: %v", err)
    72  		return err
    73  	}
    74  	return nil
    75  
    76  }
    77  

View as plain text