I have a question before I had a springboot api for android with grpcs calls but as it had the same directory both the server and the client run normally now that I migrated the api to golang it stopped working with the following error message:
unimplemented unknown service com.example.categoryproductquery.src.CategoryProductQuery
My CategoryProductQuery.proto:
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.example.categoryproductquery.grpc";
option go_package = "internal/infra/grpc/pb";
message starQueryRequest{
int64 id=1;
}
message startQueryResponse{
string star=1;
int32 total=2;
}
message CategoryQueryRequest{
string data=1;
int32 pageNum = 2;
}
message CategoryQueryWithoutRequest{
int32 pageNum = 2;
}
message CategoriesQueryRequest{
string active=1;
}
message CategorySearchRequest{
string title=1;
int32 pageNum = 2;
}
message CategoryEntity{
string category_id=1;
string category_active = 2;
string category_name=3;
string category_slug=4;
}
message CategoriesResponse{
repeated CategoryEntity categories=1;
}
message PhotoEntity{
string photo_photo=1;
string photo_type=2;
string photo_title=3;
string photo_active= 4;
}
message CategoryProductQueryRequest{
string product_id=1;
repeated CategoryEntity categories=2;
string product_title=3;
int64 product_quant=4;
string product_slug=5;
string product_active = 6;
string product_description = 7;
string product_price_un=8;
string product_price_kg=9;
int32 star=10;
repeated PhotoEntity photos=11;
string createdAt=12;
string updateAt=13;
}
message CategoryQueryResponse{
int32 count=1;
int32 pages=2;
string nextPage=3;
string prevPage=4;
repeated CategoryProductQueryRequest results=5;
}
message StarRatingRequest{
string ip = 1;
string product_id=2;
}
message StarRatingList{
string star_rating_id=1;
string product_id=2;
string ip=3;
int32 star=4;
string createdAt=5;
string updateAt=6;
}
message StarRatingRespose{
repeated StarRatingList starrating=1;
}
message ProductSingle{
string product_id=1;
}
service CategoryProductQuery {
rpc getCategoryProduct(stream CategoryQueryRequest) returns (stream CategoryQueryResponse);
rpc getCategoryProductWhithout(stream CategoryQueryWithoutRequest) returns (stream CategoryQueryResponse);
rpc getCategoryProductSearch(stream CategorySearchRequest) returns (stream CategoryQueryResponse);
rpc getCategories(CategoriesQueryRequest) returns (CategoriesResponse);
rpc getStarProducts(starQueryRequest) returns (startQueryResponse);
rpc getStar(StarRatingRequest) returns (StarRatingRespose);
rpc getProductSingle(ProductSingle)returns(CategoryProductQueryRequest);
}
My categoryproduct_service.go:
package service
import (
"context"
"io"
"math"
"strconv"
"github.com/rafaelsouzaribeiro/category-product-query/internal/infra/grpc/pb"
"github.com/rafaelsouzaribeiro/category-product-query/internal/usecase"
)
type CategoryProductService struct {
pb.UnimplementedCategoryProductQueryServer
CategoryProductFind usecase.CategoryProductFindUseCase
CategoryProductFindAll usecase.CategoryProductFindAllUseCase
Categories usecase.CategoriesFindUseCase
}
func NewCategoryProductService(find usecase.CategoryProductFindUseCase,
findall usecase.CategoryProductFindAllUseCase,
categories usecase.CategoriesFindUseCase) *CategoryProductService {
return &CategoryProductService{
CategoryProductFind: find,
CategoryProductFindAll: findall,
Categories: categories,
}
}
func (c *CategoryProductService) GetCategoryProduct(stream pb.CategoryProductQuery_GetCategoryProductServer) error {
for {
CategoryProduct, err := stream.Recv()
/// se chegou no final
if err == io.EOF {
return nil
}
if err != nil {
return err
}
counts, err := c.CategoryProductFindAll.GetProductsAll(string(CategoryProduct.Data))
if err != nil {
return err
}
pageSize := 10
skip := 0
pageNum := int(CategoryProduct.PageNum)
if pageNum > 0 {
skip = (pageNum * pageSize)
}
products, err := c.CategoryProductFind.FindCategoriesByCategoryID(string(CategoryProduct.Data), skip, pageSize)
if err != nil {
return err
}
var allProducts []usecase.CategoryProductDto
for _, product := range *products {
allProducts = append(allProducts, product)
}
println(len(allProducts))
totalPagina := int(math.Ceil(float64(counts.Count) / float64(pageSize)))
next := 0
if pageNum+1 >= totalPagina {
next = 0
} else {
next = pageNum + 1
}
prev := 0
if pageNum > 0 {
prev = pageNum - 1
}
err = stream.Send(&pb.CategoryQueryResponse{
Count: int32(counts.Count),
Pages: int32(totalPagina),
NextPage: strconv.Itoa(next),
PrevPage: strconv.Itoa(prev),
Results: ConvertProducts(allProducts),
})
if err != nil {
return err
}
}
}
func (c *CategoryProductService) GetCategories(ctx context.Context, in *pb.CategoriesQueryRequest) (*pb.CategoriesResponse, error) {
categories, err := c.Categories.GetCategories(in.Active)
if err != nil {
return nil, err
}
var pbCategories []*pb.CategoryEntity
for _, category := range *categories {
pbCategory := &pb.CategoryEntity{
CategoryId: category.CategoryID,
CategoryName: category.CategoryName,
CategoryActive: category.CategoryActive,
CategorySlug: category.CategorySlug,
}
pbCategories = append(pbCategories, pbCategory)
}
return &pb.CategoriesResponse{
Categories: pbCategories,
}, nil
}
func ConvertProducts(allProducts []usecase.CategoryProductDto) []*pb.CategoryProductQueryRequest {
var pbProducts []*pb.CategoryProductQueryRequest
for _, product := range allProducts {
var pbPhotos []*pb.PhotoEntity
for _, photo := range product.Photos {
pbPhotos = append(pbPhotos, &pb.PhotoEntity{
PhotoPhoto: photo.PhotoPhoto,
PhotoType: photo.PhotoType,
PhotoTitle: photo.PhotoTitle,
PhotoActive: photo.PhotoActive,
})
}
var pbCategories []*pb.CategoryEntity
for _, category := range product.Categories {
pbCategories = append(pbCategories, &pb.CategoryEntity{
CategoryId: category.CategoryID,
CategoryName: category.CategoryName,
CategoryActive: category.CategoryActive,
})
}
pbProducts = append(pbProducts, &pb.CategoryProductQueryRequest{
ProductId: product.ProductID,
ProductTitle: product.ProductTitle,
ProductQuant: product.ProductQuant,
ProductSlug: product.ProductSlug,
ProductActive: product.ProductActive,
ProductDescription: product.ProductDescription,
ProductPriceUn: product.ProductPriceUn,
ProductPriceKg: product.ProductPriceKg,
Star: int32(product.Star),
Photos: pbPhotos,
Categories: pbCategories,
CreatedAt: product.CreatedAt.Format(("02/01/2006 15:04:05 ")),
UpdateAt: product.UpdateAt.Format(("02/01/2006 15:04:05 ")),
})
}
return pbProducts
}
My ManagedChannel.tk:
package com.example.quitandagrpc.classes
import io.grpc.ManagedChannel
import io.grpc.ManagedChannelBuilder
import io.grpc.Metadata
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor
class ManagedGrpc(private val token: String="") {
private var port:Int =0
private var host:String=""
lateinit var canal:ManagedChannel
fun inicializar(cond:String):Metadata {
host="x.x.x.x"
when(cond){
"categoryproduct"->{
port=30007
}
"user"->{
port=30015
}
"usercommand"->{
port=30004
}
"categoryproductcommand"->{
port=31533
}
"cartpurchasequery"->{
port=30010
}
"cartpurchascommand"->{
port=30012
}
}
canal = ManagedChannelBuilder.forAddress(host, port)
.executor(Dispatchers.IO.asExecutor())
.usePlaintext()
.build()
val header = Metadata()
val key = Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER)
header.put(key, "Bearer $token")
return header
}
fun closeChannel() {
// Fecha o canal se estiver aberto
canal.shutdownNow()
}
fun getChannel(): ManagedChannel? {
return canal
}
}
Does anyone know if I have to have the same directory for both the server and the client?
Thank you very much to anyone who can help.
[SOLVED]
I just created the CategoryProductQuery.proto with protoc again and put it on android and it worked maybe it's because it has the "go_package" now