-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (51 loc) · 1.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"context"
"log"
"net"
"github.com/azar-intelops/go-interceptors/configs"
"github.com/azar-intelops/go-interceptors/controllers"
pb "github.com/azar-intelops/go-interceptors/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
// Define a gRPC interceptor
func loggingInterceptor(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
log.Printf("Received request: %v", req)
resp, err := handler(ctx, req)
return resp, err
}
type Server struct {
pb.UnimplementedMyServiceServer
}
func (s *Server) DemoMethod(ctx context.Context, req *pb.DemoRequest) (*pb.DemoResponse, error) {
return &pb.DemoResponse{
Message: "Hello " + req.Message,
}, nil
}
func main() {
// Create a new gRPC server with the logging interceptor
s := grpc.NewServer(
grpc.UnaryInterceptor(loggingInterceptor),
)
db := configs.DB
// log.Println(db)
// Register your gRPC service with the server
myService := &Server{}
authService := controllers.NewAuthServer(db)
authserver := controllers.NewAuthServiceServer(db)
pb.RegisterMyServiceServer(s, myService)
pb.RegisterUserServiceServer(s, authService)
pb.RegisterAuthServiceServer(s, authserver)
reflection.Register(s)
// Listen on port 50051
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
log.Printf("Starting server in port :%d\n", 50051)
// Start the server
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}