forked from JawherKl/api-gRPC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (68 loc) · 2.14 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"log"
"net"
"sync"
"google.golang.org/grpc"
"google.golang.org/grpc/codes" // Import grpc/codes for error codes
"google.golang.org/grpc/status" // Import grpc/status for creating error status
pb "api-grpc/path"
)
type server struct {
pb.UnimplementedApiServiceServer
users map[string]*pb.UserResponse
usersMutex sync.Mutex
}
// Implementation of GetUserInfo
func (s *server) GetUserInfo(ctx context.Context, req *pb.UserRequest) (*pb.UserResponse, error) {
s.usersMutex.Lock()
defer s.usersMutex.Unlock()
user, exists := s.users[req.UserId]
if !exists {
return nil, status.Errorf(codes.NotFound, "User not found") // Return a proper gRPC error status
}
return user, nil
}
// Implementation of CreateUser
func (s *server) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.UserResponse, error) {
s.usersMutex.Lock()
defer s.usersMutex.Unlock()
userId := generateUserId() // Implement this function to generate unique IDs
user := &pb.UserResponse{
UserId: userId,
UserName: req.UserName,
UserAge: req.UserAge,
}
s.users[userId] = user
return user, nil
}
// Implementation of ListUsers
func (s *server) ListUsers(ctx context.Context, req *pb.ListUsersRequest) (*pb.ListUsersResponse, error) {
s.usersMutex.Lock()
defer s.usersMutex.Unlock()
response := &pb.ListUsersResponse{}
for _, user := range s.users {
response.Users = append(response.Users, user)
}
return response, nil
}
// Generate a unique user ID (dummy implementation)
func generateUserId() string {
// Implement your logic for generating unique user IDs
return "unique_user_id"
}
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterApiServiceServer(s, &server{
users: make(map[string]*pb.UserResponse),
})
log.Println("gRPC server running on port 50051...")
if err := s.Serve(lis); err != nil {
log.Fatalf("Failed to serve: %v", err)
}
}