gomicroservicesgrpcrpc

Unimplemented desc = unknown service pb.AuthService Error on my simple authentication server


I created a simple authentication server with Golang. This is my .proto file:

syntax = "proto3";

package auth;

option go_package = "./pkg/pb";

service AuthService {
    rpc Register(RegisterRequest) returns (RegisterResponse) {}
    rpc Login(LoginRequest) returns (LoginResponse) {}
    rpc Validate(ValidateRequest) returns (ValidateResponse) {}
}

// Register
message RegisterRequest {
    string name = 1;
    string surname = 2;
    string email = 3;
    string password = 4;
}
message RegisterResponse {
    int64 status = 1;
    string error = 2;
}

// Login
message LoginRequest {
    string email = 1;
    string password = 2;
}
message LoginResponse {
    int64 status = 1;
    string error = 2;
    string token = 3;
}

// Validate
message ValidateRequest {
    string token = 1;
}
message ValidateResponse {
    int64 status = 1;
    string error = 2;
    int64 userID = 3;
}

I have 2 file, auth_grpc.pb.go and auth.pb.go

I want register myself with my client but when I make this request this error comes. I looked everywhere for pb.AuthService but I don't have this function or instance.

This is my main.go file :

func main() {
    env, err := config.LoadConfig()
    if err != nil {
        log.Fatalln("FAILED AT CONFIG: ", err)
    }

    h := db.Init(env.DBUrl)

    jwt := utils.JwtWrapper{
        SecretKey:       env.JWTSecretKey,
        Issuer:          "go-grpc-auth-svc",
        ExpirationHours: 24 * 365,
    }

    s := services.Server{
        H:   h,
        Jwt: jwt,
    }

    grpcServer := grpc.NewServer()
    pb.RegisterAuthServiceServer(grpcServer, &s)

    lis, err := net.Listen("tcp", env.Port)
    if err != nil {
        log.Fatalln("Failed to listing:", err)
    }
    fmt.Printf("start gRPC server on %s", lis.Addr().String())

    if err := grpcServer.Serve(lis); err != nil {
        log.Fatalln("Failed to serve:", err)
    }
}

and auth.go file. I tryed to implement jwt and DB handler:

type Server struct {
    H   db.Handler
    Jwt utils.JwtWrapper
    pb.UnimplementedAuthServiceServer
}

func (s *Server) Register(ctx context.Context, req *pb.RegisterRequest) (*pb.RegisterResponse, error) {...}

I tried to register with my client and my client file is as simple as my server.

My service client struct :

type ServiceClient struct {
    Client pb.AuthServiceClient
}

My main.go file :


func main() {
    c, err := config.LoadConfig()
    if err != nil {
        log.Fatalln("Failed at config: ", err)
    }

    // ROUTER INSTANCE
    router := gin.Default()

    // MIDDLEWARES
    router.Use(middlewares.ErrorHandler)

    auth.RegisterRoutes(router, &c)

    router.Run(c.Port)
}

And register routes file :

func RegisterRoutes(r *gin.Engine, c *config.Config) *ServiceClient {
    svc := &ServiceClient{
        Client: InitServiceClient(c),
    }

    routes := r.Group("/auth")
    routes.POST("/register", svc.Register)
    routes.POST("/login", svc.Login)
    fmt.Println(svc)
    return svc
}

func (svc *ServiceClient) Register(ctx *gin.Context) {
    routes.Register(ctx, svc.Client)
}

func (svc *ServiceClient) Login(ctx *gin.Context) {
    routes.Login(ctx, svc.Client)
}

Solution

  • I took a look at your github repo (because there was no obvious issue in the code you posted).

    In the clientauth.proto you specify:

    package pb;
    

    But in the service it's:

    package auth;
    

    So the error unknown service pb.AuthService is due to the fact that your server is providing auth.AuthService (and not the pb.AuthService your client is expecting). Change your .proto files so they are consistent (or, ideally, reference the same files).