Skip to content

Instantly share code, notes, and snippets.

@AryanGodara
Created July 17, 2023 18:23
Show Gist options
  • Save AryanGodara/e3ffccc9e2d5d0a47d85ed490c090ec0 to your computer and use it in GitHub Desktop.
Save AryanGodara/e3ffccc9e2d5d0a47d85ed490c090ec0 to your computer and use it in GitHub Desktop.
cmd/main.go
package main
import (
"fmt"
"net"
"os"
"os/signal"
"syscall"
"github.com/aryangodara/go-kit-grpc-example1/api/endpoints"
"github.com/aryangodara/go-kit-grpc-example1/api/service"
"github.com/aryangodara/go-kit-grpc-example1/api/transports"
"github.com/aryangodara/go-kit-grpc-example1/pb"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
func main() {
logger := log.NewJSONLogger(os.Stdout)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
mathService := service.NewService(logger)
mathEndpoints := endpoints.MakeEndpoints(mathService)
grpcServer := transports.NewGRPCServer(mathEndpoints, logger)
errs := make(chan error)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGALRM)
errs <- fmt.Errorf("%s", <-c)
}()
grpcListener, err := net.Listen("tcp", ":9085")
if err != nil {
logger.Log("during", "Listen", "err", err)
os.Exit(1)
}
reflection.Register(grpcServer)
go func() {
baseServer := grpc.NewServer()
pb.RegisterMathServiceServer(baseServer, grpcServer)
level.Info(logger).Log("msg", "Starting gRPC server at :9085")
baseServer.Serve(grpcListener)
}()
level.Error(logger).Log("exit", <-errs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment