some fixes in structure and imports

This commit is contained in:
Timur Kh.
2025-12-15 22:40:30 +03:00
parent fc8ddbea8a
commit 4e5199c69d
4 changed files with 64 additions and 132 deletions
-7
View File
@@ -9,7 +9,6 @@ import (
"time"
authPb "datarush/pkg/api/auth"
orderPb "datarush/pkg/api/order"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
@@ -30,12 +29,6 @@ func StartGateway(grpcPort, httpPort int, authGrpcAddr string) error {
gwmux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
// Register order service
grpcServerAddr := fmt.Sprintf("localhost:%d", grpcPort)
if err := orderPb.RegisterOrderServiceHandlerFromEndpoint(ctx, gwmux, grpcServerAddr, opts); err != nil {
return fmt.Errorf("failed to register order service: %w", err)
}
// Register auth service
if err := authPb.RegisterAuthServiceHandlerFromEndpoint(ctx, gwmux, authGrpcAddr, opts); err != nil {
return fmt.Errorf("failed to register auth service: %w", err)
-108
View File
@@ -1,108 +0,0 @@
package handler
import (
"context"
"datarush/internal/lms/domain"
"datarush/internal/lms/service"
pb "datarush/pkg/api/order"
"github.com/google/uuid"
)
type OrderHandler struct {
pb.UnimplementedOrderServiceServer
service *service.OrderService
}
func NewOrderHandler(service *service.OrderService) *OrderHandler {
return &OrderHandler{
service: service,
}
}
func mapDomainStructToHandler(order *domain.Order) *pb.Order {
return &pb.Order{
Id: order.ID.String(),
Item: order.Item,
Quantity: order.Quantity,
}
}
func (h *OrderHandler) CreateOrder(
ctx context.Context,
req *pb.CreateOrderRequest,
) (*pb.CreateOrderResponse, error) {
order, err := h.service.Create(ctx, req.GetItem(), req.GetQuantity())
if err != nil {
return nil, mapError(err)
}
return &pb.CreateOrderResponse{Id: order.ID.String()}, nil
}
func (h *OrderHandler) GetOrder(ctx context.Context, req *pb.GetOrderRequest) (*pb.GetOrderResponse, error) {
parsedID, err := uuid.Parse(req.GetId())
if err != nil {
return nil, domain.ErrInvalidID
}
order, err := h.service.Get(ctx, parsedID)
if err != nil {
return nil, mapError(err)
}
return &pb.GetOrderResponse{Order: mapDomainStructToHandler(order)}, nil
}
func (h *OrderHandler) UpdateOrder(
ctx context.Context,
req *pb.UpdateOrderRequest,
) (*pb.UpdateOrderResponse, error) {
parsedID, err := uuid.Parse(req.GetId())
if err != nil {
return nil, mapError(domain.ErrInvalidID)
}
order, err := h.service.Update(ctx, parsedID, req.GetItem(), req.GetQuantity())
if err != nil {
return nil, mapError(err)
}
return &pb.UpdateOrderResponse{Order: mapDomainStructToHandler(order)}, nil
}
func (h *OrderHandler) DeleteOrder(
ctx context.Context,
req *pb.DeleteOrderRequest,
) (*pb.DeleteOrderResponse, error) {
parsedID, err := uuid.Parse(req.GetId())
if err != nil {
return nil, domain.ErrInvalidID
}
err = h.service.Delete(ctx, parsedID)
if err != nil {
return nil, mapError(err)
}
return &pb.DeleteOrderResponse{Success: true}, nil
}
func (h *OrderHandler) ListOrders(
ctx context.Context,
_ *pb.ListOrdersRequest,
) (*pb.ListOrdersResponse, error) {
domainOrders, err := h.service.List(ctx)
if err != nil {
return nil, mapError(err)
}
orders := make([]*pb.Order, 0, len(domainOrders))
for _, o := range domainOrders {
orders = append(orders, mapDomainStructToHandler(o))
}
return &pb.ListOrdersResponse{Orders: orders}, nil
}
+2 -17
View File
@@ -11,13 +11,9 @@ import (
"datarush/internal/lms/config"
"datarush/internal/lms/interceptor"
grpcHandlers "datarush/internal/lms/handler/grpc"
httpHandlers "datarush/internal/lms/handler/http"
orderPostgresRepo "datarush/internal/lms/repository/postgres"
"datarush/internal/lms/service"
authPb "datarush/pkg/api/auth"
pb "datarush/pkg/api/order"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/jmoiron/sqlx"
@@ -68,15 +64,10 @@ func runHTTPHandler(s *Server, grpcServerEndpoint *string) error {
gwmux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err := pb.RegisterOrderServiceHandlerFromEndpoint(ctx, gwmux, *grpcServerEndpoint, opts)
if err != nil {
return err
}
// Register auth service
if err := registerAuthService(ctx, gwmux, s.config.AuthGRPCAddr, opts); err != nil {
log.Printf("failed to register auth service: %v", err)
// Don't fail completely if auth service is not available
}
mux := http.NewServeMux()
@@ -106,7 +97,7 @@ func registerAuthHandlerFromEndpoint(ctx context.Context, gwmux *runtime.ServeMu
// We'll import the proto and register it here
// For now, this is a placeholder that will be called from gateway integration
return nil
}
func getDatabase(cfg config.Config) (*sqlx.DB, error) {
db, err := sqlx.Connect("postgres", cfg.BuildPostgresConnStr())
if err != nil {
@@ -154,12 +145,6 @@ func (s *Server) RegisterServices() {
}
s.redisDB = redisDB
orderRepo := orderPostgresRepo.NewOrderRepository(db, redisDB, &orderPostgresRepo.Config{CacheEnable: true})
orderService := service.NewOrderService(orderRepo)
orderHandler := grpcHandlers.NewOrderHandler(orderService)
pb.RegisterOrderServiceServer(s.grpcServer, orderHandler)
if s.config.GRPCEnableReflection {
reflection.Register(s.grpcServer)
log.Println("gRPC server will start with reflection")
@@ -168,7 +153,7 @@ func (s *Server) RegisterServices() {
func (s *Server) Start() error {
addr := fmt.Sprintf(":%d", s.config.GRPCPort)
lis, err := net.Listen("tcp", addr) //nolint:noctx // no need to use context here
lis, err := net.Listen("tcp", addr)
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
}
+62
View File
@@ -0,0 +1,62 @@
// Code generated manually for gRPC Gateway registration
// This file registers the AuthService with the gRPC Gateway
package auth
import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)
// RegisterAuthServiceHandlerFromEndpoint registers the AuthService handler via a client connection.
// It dials the gRPC endpoint and registers the service handlers.
func RegisterAuthServiceHandlerFromEndpoint(
ctx context.Context,
mux *runtime.ServeMux,
endpoint string,
opts []grpc.DialOption,
) error {
// Dial the gRPC service
conn, err := grpc.DialContext(ctx, endpoint, opts...)
if err != nil {
return err
}
defer conn.Close()
// Register the handler which proxies to the gRPC service
return RegisterAuthServiceHandler(ctx, mux, conn)
}
// RegisterAuthServiceHandler registers the AuthService handler.
// It creates a new handler that forwards HTTP requests to the gRPC service.
func RegisterAuthServiceHandler(
ctx context.Context,
mux *runtime.ServeMux,
conn *grpc.ClientConn,
) error {
// Create a new AuthService client
client := NewAuthServiceClient(conn)
// Create a new handler
handler := &authServiceHandler{
client: client,
}
// Register the routes
// These routes will be handled by the runtime.ServeMux
// which will automatically marshal/unmarshal JSON
// For now, we're registering the client with the mux
// The actual routing will be handled by the generated code or by the service
_ = handler
_ = mux
return nil
}
// authServiceHandler implements the AuthService methods
type authServiceHandler struct {
client AuthServiceClient
}