Add gRPC server implementation and database integration for marketplace and products
This commit is contained in:
		
							
								
								
									
										225
									
								
								щдв
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										225
									
								
								щдв
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,225 @@
 | 
			
		||||
////// import (
 | 
			
		||||
//////
 | 
			
		||||
//////	"Sipro-Marketplaces/internal/marketplace"
 | 
			
		||||
//////	ozon_products "Sipro-Marketplaces/internal/ozon/products"
 | 
			
		||||
//////	"fmt"
 | 
			
		||||
//////	"google.golang.org/grpc"
 | 
			
		||||
//////	"net"
 | 
			
		||||
//////
 | 
			
		||||
////// )
 | 
			
		||||
//////
 | 
			
		||||
////// func createGrpcServer() {
 | 
			
		||||
//////
 | 
			
		||||
//////		lis, err := net.Listen("tcp", ":8080")
 | 
			
		||||
//////		if err != nil {
 | 
			
		||||
//////			fmt.Printf("failed to listen: %v\n", err)
 | 
			
		||||
//////			return
 | 
			
		||||
//////		}
 | 
			
		||||
//////		grpcServer := grpc.NewServer()
 | 
			
		||||
//////
 | 
			
		||||
//////		repo, err := marketplace.RegisterAdapterGRPC(grpcServer)
 | 
			
		||||
//////		if err != nil {
 | 
			
		||||
//////			fmt.Printf("failed to register gRPC server: %v\n", err)
 | 
			
		||||
//////			return
 | 
			
		||||
//////		}
 | 
			
		||||
//////		_, err = ozon_products.RegisterAdapterGRPC(grpcServer, *repo)
 | 
			
		||||
//////		if err != nil {
 | 
			
		||||
//////			fmt.Printf("failed to register Ozon Products gRPC server: %v\n", err)
 | 
			
		||||
//////			return
 | 
			
		||||
//////		}
 | 
			
		||||
//////
 | 
			
		||||
//////		fmt.Println("gRPC server registered successfully.")
 | 
			
		||||
//////		// Start serving gRPC requests
 | 
			
		||||
//////		fmt.Println("gRPC server is starting on port 8080...")
 | 
			
		||||
//////
 | 
			
		||||
//////		if err := grpcServer.Serve(lis); err != nil {
 | 
			
		||||
//////			fmt.Printf("failed to serve: %v\n", err)
 | 
			
		||||
//////		}
 | 
			
		||||
//////		fmt.Println("gRPC server created.")
 | 
			
		||||
//////	}
 | 
			
		||||
//////
 | 
			
		||||
//////	func main() {
 | 
			
		||||
//////		fmt.Println("Starting Sipro-Marketplaces server...")
 | 
			
		||||
//////		createGrpcServer()
 | 
			
		||||
//////	}
 | 
			
		||||
////package main
 | 
			
		||||
////
 | 
			
		||||
////import (
 | 
			
		||||
////	"Sipro-Marketplaces/internal/marketplace"
 | 
			
		||||
////	"Sipro-Marketplaces/internal/ozon"
 | 
			
		||||
////	"context"
 | 
			
		||||
////	"encoding/json"
 | 
			
		||||
////	"fmt"
 | 
			
		||||
////	api "git.denco.store/fakz9/ozon-api-client/ozon"
 | 
			
		||||
////	"github.com/jackc/pgx/v5"
 | 
			
		||||
////	"time"
 | 
			
		||||
////)
 | 
			
		||||
////
 | 
			
		||||
////func main() {
 | 
			
		||||
////
 | 
			
		||||
////	startTime := time.Now()
 | 
			
		||||
////
 | 
			
		||||
////	ctx := context.Background()
 | 
			
		||||
////
 | 
			
		||||
////	conn, err := pgx.Connect(ctx, "postgresql://postgres:GjitkeYf%5Beq@/sipro?host=/run/postgresql")
 | 
			
		||||
////	if err != nil {
 | 
			
		||||
////		panic(err)
 | 
			
		||||
////	}
 | 
			
		||||
////	defer conn.Close(ctx)
 | 
			
		||||
////	mpRepo := marketplace.NewDBRepository(conn)
 | 
			
		||||
////	mp, err := mpRepo.GetMarketplaceByID(ctx, 262)
 | 
			
		||||
////	if err != nil {
 | 
			
		||||
////		panic(err)
 | 
			
		||||
////	}
 | 
			
		||||
////	client, err := ozon.GetClientFromMarketplace(mp)
 | 
			
		||||
////	if err != nil {
 | 
			
		||||
////		panic(err)
 | 
			
		||||
////	}
 | 
			
		||||
////
 | 
			
		||||
////	productIdsChan := make(chan []int64)
 | 
			
		||||
////
 | 
			
		||||
////	go func() {
 | 
			
		||||
////		defer close(productIdsChan)
 | 
			
		||||
////
 | 
			
		||||
////		var lastId string
 | 
			
		||||
////
 | 
			
		||||
////		for {
 | 
			
		||||
////			rsp, err := client.Products().GetListOfProducts(
 | 
			
		||||
////				ctx,
 | 
			
		||||
////				&api.GetListOfProductsParams{
 | 
			
		||||
////					Filter: api.GetListOfProductsFilter{
 | 
			
		||||
////						Visibility: "ALL",
 | 
			
		||||
////					},
 | 
			
		||||
////					LastId: lastId,
 | 
			
		||||
////					Limit:  1000,
 | 
			
		||||
////				},
 | 
			
		||||
////			)
 | 
			
		||||
////			if err != nil {
 | 
			
		||||
////				// You may want to send error on another channel or handle differently
 | 
			
		||||
////				panic(err)
 | 
			
		||||
////			}
 | 
			
		||||
////
 | 
			
		||||
////			items := rsp.Result.Items
 | 
			
		||||
////			if len(items) == 0 {
 | 
			
		||||
////				break
 | 
			
		||||
////			}
 | 
			
		||||
////
 | 
			
		||||
////			productIds := make([]int64, len(items))
 | 
			
		||||
////			for i, item := range items {
 | 
			
		||||
////				productIds[i] = item.ProductId
 | 
			
		||||
////			}
 | 
			
		||||
////
 | 
			
		||||
////			productIdsChan <- productIds
 | 
			
		||||
////
 | 
			
		||||
////			lastId = rsp.Result.LastId
 | 
			
		||||
////			if lastId == "" {
 | 
			
		||||
////				break
 | 
			
		||||
////			}
 | 
			
		||||
////		}
 | 
			
		||||
////	}()
 | 
			
		||||
////
 | 
			
		||||
////	productsChan := make(chan []string)
 | 
			
		||||
////	go func() {
 | 
			
		||||
////		defer close(productsChan)
 | 
			
		||||
////		for productIds := range productIdsChan {
 | 
			
		||||
////			go func(ids []int64) {
 | 
			
		||||
////				products, err := client.Products().ListProductsByIDs(
 | 
			
		||||
////					ctx, &api.ListProductsByIDsParams{
 | 
			
		||||
////						OfferId:   nil,
 | 
			
		||||
////						ProductId: ids,
 | 
			
		||||
////						SKU:       nil,
 | 
			
		||||
////					})
 | 
			
		||||
////				if err != nil {
 | 
			
		||||
////					// Handle error appropriately, e.g., log it or send it to an error channel
 | 
			
		||||
////					panic(err)
 | 
			
		||||
////				}
 | 
			
		||||
////				productsEncoded := make([]string, len(products.Items))
 | 
			
		||||
////				for i, product := range products.Items {
 | 
			
		||||
////					jsonString, err := json.Marshal(product)
 | 
			
		||||
////					if err != nil {
 | 
			
		||||
////						// Handle error appropriately, e.g., log it or send it to an error channel
 | 
			
		||||
////						panic(err)
 | 
			
		||||
////					}
 | 
			
		||||
////					productsEncoded[i] = string(jsonString)
 | 
			
		||||
////				}
 | 
			
		||||
////				productsChan <- productsEncoded
 | 
			
		||||
////			}(productIds)
 | 
			
		||||
////		}
 | 
			
		||||
////
 | 
			
		||||
////	}()
 | 
			
		||||
////	total := 0
 | 
			
		||||
////	for products := range productsChan {
 | 
			
		||||
////		for _ = range products {
 | 
			
		||||
////			// Here you can process each product, e.g., save to database or print
 | 
			
		||||
////			// For demonstration, we will just print the product
 | 
			
		||||
////			total += 1
 | 
			
		||||
////		}
 | 
			
		||||
////		if len(products) == 0 {
 | 
			
		||||
////			fmt.Println("No more products to process.")
 | 
			
		||||
////			break
 | 
			
		||||
////		}
 | 
			
		||||
////	}
 | 
			
		||||
////	elapsedTime := time.Since(startTime)
 | 
			
		||||
////	println("Total time taken:", elapsedTime.String())
 | 
			
		||||
////	println("Total products processed:", total)
 | 
			
		||||
////
 | 
			
		||||
////}
 | 
			
		||||
//
 | 
			
		||||
//package main
 | 
			
		||||
//
 | 
			
		||||
//import (
 | 
			
		||||
//	"context"
 | 
			
		||||
//	"fmt"
 | 
			
		||||
//	"time"
 | 
			
		||||
//
 | 
			
		||||
//	"github.com/redis/rueidis"
 | 
			
		||||
//)
 | 
			
		||||
//
 | 
			
		||||
//func main() {
 | 
			
		||||
//	fmt.Println("Hello, World!")
 | 
			
		||||
//	client, err := rueidis.NewClient(rueidis.ClientOption{
 | 
			
		||||
//		InitAddress: []string{"localhost:6379"},
 | 
			
		||||
//		Password:    "ELdhsgqJt5QZUSWKU5vY3D9CXa1a0teIceeHqvCtoPkrDJ0Lge7XIe8187gFjd0qZLv9zwhGr62MqY",
 | 
			
		||||
//	})
 | 
			
		||||
//	if err != nil {
 | 
			
		||||
//		fmt.Printf("Failed to create Redis client: %v\n", err)
 | 
			
		||||
//		return
 | 
			
		||||
//	}
 | 
			
		||||
//	defer client.Close()
 | 
			
		||||
//	script := rueidis.NewLuaScript(`
 | 
			
		||||
//		local key = KEYS[1]
 | 
			
		||||
//		local now = tonumber(ARGV[1])
 | 
			
		||||
//		local window = tonumber(ARGV[2])
 | 
			
		||||
//		local limit = tonumber(ARGV[3])
 | 
			
		||||
//
 | 
			
		||||
//		redis.call('ZREMRANGEBYSCORE', key, '-inf', now - window)
 | 
			
		||||
//		local count = redis.call('ZCARD', key)
 | 
			
		||||
//
 | 
			
		||||
//		if count < limit then
 | 
			
		||||
//			redis.call('ZADD', key, now, now)
 | 
			
		||||
//			redis.call('EXPIRE', key, math.ceil(window / 1000000000))
 | 
			
		||||
//			return 1
 | 
			
		||||
//		end
 | 
			
		||||
//		return 0
 | 
			
		||||
//	`)
 | 
			
		||||
//	ctx := context.Background()
 | 
			
		||||
//	windowSize := time.Second
 | 
			
		||||
//	for i := 0; i < 51; i++ {
 | 
			
		||||
//		now := time.Now().UnixNano()
 | 
			
		||||
//		result, err := script.Exec(ctx, client, []string{"2282"}, []string{
 | 
			
		||||
//			fmt.Sprintf("%d", now),
 | 
			
		||||
//			fmt.Sprintf("%d", int64(windowSize)),
 | 
			
		||||
//			fmt.Sprintf("%d", 50),
 | 
			
		||||
//		}).ToInt64()
 | 
			
		||||
//		if err != nil {
 | 
			
		||||
//			fmt.Printf("Failed to execute script: %v\n", err)
 | 
			
		||||
//			return
 | 
			
		||||
//		}
 | 
			
		||||
//		if result == 1 {
 | 
			
		||||
//			//fmt.Println("Request allowed")
 | 
			
		||||
//		} else {
 | 
			
		||||
//			fmt.Println("Rate limit exceeded")
 | 
			
		||||
//		}
 | 
			
		||||
//	}
 | 
			
		||||
//
 | 
			
		||||
//}
 | 
			
		||||
		Reference in New Issue
	
	Block a user