ids-svc-product/cmd/main.go
BXY c258915f5b feat: introduce product category management
- Added SQL scripts to recreate product category and catalog tables, including necessary indexes and comments.
- Implemented repository for product category management, including CRUD operations and binding/unbinding products to categories.
- Developed gRPC server methods for creating, retrieving, listing, updating, and deleting product categories.
- Added error handling for various scenarios, including version conflicts and category constraints.
- Implemented helper functions for converting between internal representations and gRPC messages.
2026-09-24 18:28:18 +08:00

137 lines
4.6 KiB
Go

package main
import (
"context"
"database/sql"
"net"
"net/http"
"os/signal"
"syscall"
"time"
_ "github.com/jackc/pgx/v5/stdlib"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
productv1 "llms.idsbuybuybuy.com/ecommerce/ids-proto/gen/go/ids/product/v1"
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/config"
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/idgen"
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/observability"
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/repository"
"llms.idsbuybuybuy.com/ecommerce/ids-svc-product/internal/server"
)
func main() {
logger, err := newLogger()
if err != nil {
panic(err)
}
defer logger.Sync()
cfg, err := config.LoadConfig()
if err != nil {
logger.Fatal("load config failed", zap.Error(err))
}
idGenerator, err := idgen.NewFromEnv()
if err != nil {
logger.Fatal("init snowflake id generator failed", zap.Error(err))
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
observer, shutdownTelemetry, err := observability.New(ctx, logger, cfg.OTLPEndpoint, cfg.OTELInsecure, cfg.Environment)
if err != nil {
logger.Fatal("init observability failed", zap.Error(err))
}
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := shutdownTelemetry(shutdownCtx); err != nil {
logger.Error("shutdown telemetry failed", zap.Error(err))
}
}()
db, err := sql.Open("pgx", cfg.PostgresDSN)
if err != nil {
logger.Fatal("open postgres failed", zap.Error(err))
}
defer db.Close()
db.SetMaxOpenConns(20)
db.SetMaxIdleConns(5)
if err := db.PingContext(ctx); err != nil {
logger.Fatal("ping postgres failed", zap.Error(err))
}
repo := repository.NewProductRepository(db)
if err := repo.EnsureSchema(ctx); err != nil {
logger.Fatal("ensure product schema failed", zap.Error(err))
}
metricsServer := observability.MetricsServer(cfg.MetricsPort, db.PingContext)
go func() {
logger.Info("metrics server listening", zap.String("addr", cfg.MetricsPort))
if err := metricsServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error("metrics server failed", zap.Error(err))
stop()
}
}()
productServer := server.NewProductServer(repo, idGenerator, logger, observer)
grpcServer := grpc.NewServer(
grpc.StatsHandler(otelgrpc.NewServerHandler()),
grpc.UnaryInterceptor(observer.UnaryServerInterceptor()),
)
productv1.RegisterProductServiceServer(grpcServer, productServer)
productv1.RegisterProductCategoryServiceServer(grpcServer, productServer)
healthServer := health.NewServer()
healthServer.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
healthServer.SetServingStatus(productv1.ProductService_ServiceDesc.ServiceName, healthpb.HealthCheckResponse_SERVING)
healthServer.SetServingStatus(productv1.ProductCategoryService_ServiceDesc.ServiceName, healthpb.HealthCheckResponse_SERVING)
healthServer.SetServingStatus(observability.ServiceName, healthpb.HealthCheckResponse_SERVING)
healthpb.RegisterHealthServer(grpcServer, healthServer)
reflection.Register(grpcServer)
lis, err := net.Listen("tcp", cfg.ServerPort)
if err != nil {
logger.Fatal("listen grpc failed", zap.String("addr", cfg.ServerPort), zap.Error(err))
}
go func() {
logger.Info("grpc server listening", zap.String("addr", cfg.ServerPort))
if err := grpcServer.Serve(lis); err != nil {
logger.Fatal("grpc server failed", zap.Error(err))
}
}()
<-ctx.Done()
logger.Info("shutting down")
healthServer.SetServingStatus("", healthpb.HealthCheckResponse_NOT_SERVING)
healthServer.SetServingStatus(productv1.ProductService_ServiceDesc.ServiceName, healthpb.HealthCheckResponse_NOT_SERVING)
healthServer.SetServingStatus(productv1.ProductCategoryService_ServiceDesc.ServiceName, healthpb.HealthCheckResponse_NOT_SERVING)
healthServer.SetServingStatus(observability.ServiceName, healthpb.HealthCheckResponse_NOT_SERVING)
grpcServer.GracefulStop()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := metricsServer.Shutdown(shutdownCtx); err != nil {
logger.Error("shutdown metrics server failed", zap.Error(err))
}
}
func newLogger() (*zap.Logger, error) {
cfg := zap.NewProductionConfig()
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
return cfg.Build()
}