|
14 | 14 |
|
15 | 15 | using Microsoft.AspNetCore.Hosting; |
16 | 16 | using Microsoft.Extensions.Hosting; |
17 | | -using cartservice; |
| 17 | +using Microsoft.AspNetCore.Builder; |
| 18 | +using cartservice.cartstore; |
| 19 | +using System; |
| 20 | +using Microsoft.Extensions.DependencyInjection; |
| 21 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 22 | +using OpenTelemetry.Metrics; |
| 23 | +using OpenTelemetry.Trace; |
| 24 | +using cartservice.services; |
| 25 | +using Microsoft.AspNetCore.Http; |
18 | 26 |
|
19 | | -CreateHostBuilder(args).Build().Run(); |
| 27 | +var builder = WebApplication.CreateBuilder(args); |
| 28 | +string redisAddress = builder.Configuration["REDIS_ADDR"]; |
| 29 | +RedisCartStore cartStore = null; |
| 30 | +if (string.IsNullOrEmpty(redisAddress)) |
| 31 | +{ |
| 32 | + Console.WriteLine("REDIS_ADDR environment variable is required."); |
| 33 | + System.Environment.Exit(1); |
| 34 | +} |
| 35 | +cartStore = new RedisCartStore(redisAddress); |
20 | 36 |
|
21 | | -static IHostBuilder CreateHostBuilder(string[] args) => |
22 | | - Host.CreateDefaultBuilder(args) |
23 | | - .ConfigureWebHostDefaults(webBuilder => |
| 37 | +// Initialize the redis store |
| 38 | +cartStore.InitializeAsync().GetAwaiter().GetResult(); |
| 39 | +Console.WriteLine("Initialization completed"); |
| 40 | + |
| 41 | +builder.Services.AddSingleton<ICartStore>(cartStore); |
| 42 | + |
| 43 | +builder.Services.AddOpenTelemetryTracing((builder) => builder |
| 44 | + .AddRedisInstrumentation( |
| 45 | + cartStore.GetConnection(), |
| 46 | + options => options.SetVerboseDatabaseStatements = true) |
| 47 | + .AddAspNetCoreInstrumentation() |
| 48 | + .AddGrpcClientInstrumentation() |
| 49 | + .AddHttpClientInstrumentation() |
| 50 | + .AddOtlpExporter()); |
| 51 | + |
| 52 | +builder.Services.AddOpenTelemetryMetrics(builder => |
| 53 | + builder.AddRuntimeInstrumentation() |
| 54 | + .AddAspNetCoreInstrumentation() |
| 55 | + .AddOtlpExporter()); |
| 56 | + |
| 57 | +builder.Services.AddGrpc(); |
| 58 | +builder.Services.AddGrpcHealthChecks() |
| 59 | + .AddCheck("Sample", () => HealthCheckResult.Healthy()); |
| 60 | + |
| 61 | +var app = builder.Build(); |
| 62 | + |
| 63 | +if (app.Environment.IsDevelopment()) |
24 | 64 | { |
25 | | - webBuilder.UseStartup<Startup>(); |
26 | | - }); |
| 65 | + app.UseDeveloperExceptionPage(); |
| 66 | + } |
| 67 | + |
| 68 | +app.UseRouting(); |
| 69 | + |
| 70 | +app.UseEndpoints(endpoints => |
| 71 | +{ |
| 72 | + endpoints.MapGrpcService<CartService>(); |
| 73 | + endpoints.MapGrpcHealthChecksService(); |
| 74 | + |
| 75 | + endpoints.MapGet("/", async context => |
| 76 | + { |
| 77 | + await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +app.Run(); |
0 commit comments