grpc-api-example/main.go

46 lines
1.2 KiB
Go
Raw Normal View History

2024-02-26 10:12:30 +00:00
package main
import (
"errors"
"github.com/sundowndev/grpc-api-example/server"
2024-02-26 13:57:19 +00:00
"google.golang.org/grpc/credentials/insecure"
2024-02-26 10:12:30 +00:00
"google.golang.org/grpc/grpclog"
"io"
"net"
"os"
"os/signal"
"syscall"
)
func main() {
// Adds gRPC internal logs. This is quite verbose, so adjust as desired!
log := grpclog.NewLoggerV2(os.Stdout, io.Discard, io.Discard)
grpclog.SetLoggerV2(log)
2024-02-26 13:57:19 +00:00
addr := "0.0.0.0:10000" // TODO: handle this with CLI flags
srv := server.NewServer(insecure.NewCredentials()) // TODO: Replace with your own certificate!
2024-02-26 10:12:30 +00:00
// Serve gRPC Server
log.Info("Serving gRPC on https://", addr)
go func() {
if err := srv.Listen(addr); err != nil && !errors.Is(err, net.ErrClosed) {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shut down the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can't be caught, so don't need to add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
if err := srv.Close(); err != nil {
log.Fatal(err)
}
2024-02-26 13:57:19 +00:00
log.Info("graceful shut down succeeded")
2024-02-26 10:12:30 +00:00
}