mirror of
https://github.com/linka-cloud/grpc.git
synced 2024-11-22 19:06:26 +00:00
55 lines
866 B
Go
55 lines
866 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
"syscall"
|
||
|
|
||
|
"google.golang.org/grpc"
|
||
|
|
||
|
"go.linka.cloud/grpc-toolkit/proxy/testservice"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
port = flag.Uint("port", 8080, "Port to listen to")
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
srv := grpc.NewServer()
|
||
|
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *port))
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
testservice.RegisterTestServiceServer(srv, testservice.DefaultTestServiceServer)
|
||
|
|
||
|
errs := make(chan error)
|
||
|
|
||
|
go func() {
|
||
|
log.Printf("listening on %s", lis.Addr().String())
|
||
|
errs <- srv.Serve(lis)
|
||
|
}()
|
||
|
|
||
|
sigs := make(chan os.Signal, 1)
|
||
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||
|
|
||
|
go func() {
|
||
|
sig := <-sigs
|
||
|
log.Printf("shutdown due to %s", sig)
|
||
|
srv.GracefulStop()
|
||
|
}()
|
||
|
|
||
|
if err := <-errs; err != nil {
|
||
|
log.Println(err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
flag.Parse()
|
||
|
}
|