dotnet-cqrs/Svrnty.CQRS.Grpc.Sample/Program.cs

61 lines
2.1 KiB
C#

using Microsoft.AspNetCore.Server.Kestrel.Core;
using Svrnty.CQRS;
using Svrnty.CQRS.Abstractions;
using Svrnty.CQRS.FluentValidation;
using Svrnty.CQRS.Grpc.Sample;
using Svrnty.CQRS.Grpc.Sample.Grpc.Extensions;
using Svrnty.CQRS.MinimalApi;
var builder = WebApplication.CreateBuilder(args);
// Configure Kestrel to support both HTTP/1.1 (for REST APIs) and HTTP/2 (for gRPC)
builder.WebHost.ConfigureKestrel(options =>
{
// Port 6000: HTTP/2 for gRPC
options.ListenLocalhost(6000, o => o.Protocols = HttpProtocols.Http2);
// Port 6001: HTTP/1.1 for REST API
options.ListenLocalhost(6001, o => o.Protocols = HttpProtocols.Http1);
});
// Register command handlers with CQRS and FluentValidation
builder.Services.AddCommand<AddUserCommand, int, AddUserCommandHandler, AddUserCommandValidator>();
builder.Services.AddCommand<RemoveUserCommand, RemoveUserCommandHandler>();
// Register query handlers with CQRS
builder.Services.AddQuery<FetchUserQuery, User, FetchUserQueryHandler>();
// Register discovery services for MinimalApi
builder.Services.AddDefaultCommandDiscovery();
builder.Services.AddDefaultQueryDiscovery();
// Auto-generated: Register gRPC services for both commands and queries (includes reflection)
builder.Services.AddGrpcCommandsAndQueries();
// Add Swagger/OpenAPI support
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Auto-generated: Map gRPC endpoints for both commands and queries
app.MapGrpcCommandsAndQueries();
// Map gRPC reflection service
app.MapGrpcReflectionService();
// Enable Swagger middleware
app.UseSwagger();
app.UseSwaggerUI();
// Map MinimalApi endpoints for commands and queries
app.MapSvrntyCommands();
app.MapSvrntyQueries();
Console.WriteLine("Auto-Generated gRPC Server with Reflection, Validation, MinimalApi and Swagger");
Console.WriteLine("gRPC (HTTP/2): http://localhost:6000");
Console.WriteLine("HTTP API (HTTP/1.1): http://localhost:6001/api/command/* and http://localhost:6001/api/query/*");
Console.WriteLine("Swagger UI: http://localhost:6001/swagger");
app.Run();