This commit is contained in:
Timur Kh.
2025-12-15 22:28:01 +03:00
parent 4a17c75d6f
commit f7a7b19275
25 changed files with 1383 additions and 3 deletions
+2
View File
@@ -22,6 +22,7 @@ type Config struct {
DBPassword string
DBName string
RedisURI string
AuthGRPCAddr string
}
func Load() (*Config, error) {
@@ -39,6 +40,7 @@ func Load() (*Config, error) {
DBPassword: getEnv("POSTGRES_PASSWORD", "postgres"),
DBName: getEnv("POSTGRES_DATABASE", "postgres"),
RedisURI: getEnv("REDIS_URI", "redis://localhost:6379"),
AuthGRPCAddr: getEnv("AUTH_GRPC_ADDR", "localhost:50052"),
}, nil
}
+58
View File
@@ -0,0 +1,58 @@
package gateway
import (
"context"
"fmt"
"log"
"net"
"net/http"
"time"
authPb "datarush/pkg/api/auth"
orderPb "datarush/pkg/api/order"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
httpReadTimeout = 10 * time.Second
httpWriteTimeout = 10 * time.Second
httpIdleTimeout = 60 * time.Second
)
func StartGateway(grpcPort, httpPort int, authGrpcAddr string) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
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)
}
srv := &http.Server{
Addr: fmt.Sprintf(":%d", httpPort),
Handler: gwmux,
ReadTimeout: httpReadTimeout,
WriteTimeout: httpWriteTimeout,
IdleTimeout: httpIdleTimeout,
}
log.Printf("starting gRPC-Gateway on port %d", httpPort)
return srv.ListenAndServe()
}
func GetGRPCListener(port int) (net.Listener, error) {
return net.Listen("tcp", fmt.Sprintf(":%d", port))
}
+20
View File
@@ -16,6 +16,7 @@ import (
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"
@@ -72,6 +73,12 @@ func runHTTPHandler(s *Server, grpcServerEndpoint *string) error {
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()
mux.Handle("/healthz", httpHandlers.NewHealthHandler(s.db, s.redisDB))
mux.Handle("/", gwmux)
@@ -87,6 +94,19 @@ func runHTTPHandler(s *Server, grpcServerEndpoint *string) error {
return srv.ListenAndServe()
}
func registerAuthService(ctx context.Context, gwmux *runtime.ServeMux, authAddr string, opts []grpc.DialOption) error {
if err := authPb.RegisterAuthServiceHandlerFromEndpoint(ctx, gwmux, authAddr, opts); err != nil {
return fmt.Errorf("register auth service handler: %w", err)
}
log.Printf("registered auth service from %s", authAddr)
return nil
}
func registerAuthHandlerFromEndpoint(ctx context.Context, gwmux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error {
// 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 {