-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (93 loc) · 3.78 KB
/
Program.cs
File metadata and controls
110 lines (93 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using Elastic.Documentation.Api;
using Elastic.Documentation.Api.OpenTelemetry;
using Elastic.Documentation.Configuration;
using Elastic.Documentation.Configuration.Assembler;
using Elastic.Documentation.Search.Common;
using Elastic.Documentation.ServiceDefaults;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
try
{
var builder = WebApplication.CreateSlimBuilder(args);
_ = builder.AddDocumentationServiceDefaults((s, p) =>
{
_ = s.AddSingleton(AssemblyConfiguration.Create(p));
});
_ = builder.AddDefaultHealthChecks();
_ = builder.AddDocsApiOpenTelemetry();
// Only hardcode port 8080 when not running under Aspire/orchestration that sets ASPNETCORE_HTTP_PORTS
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ASPNETCORE_HTTP_PORTS")))
{
_ = builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.ListenAnyIP(8080);
});
}
var environment = Environment.GetEnvironmentVariable("ENVIRONMENT");
Console.WriteLine($"Docs Environment: {environment}");
builder.Services.AddElasticDocsApiServices(environment);
var app = builder.Build();
var logger = app.Services.GetRequiredService<ILogger<Program>>();
LogElasticsearchConfiguration(app, logger);
var lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
_ = lifetime.ApplicationStarted.Register(() => logger.LogInformation("Application started"));
_ = lifetime.ApplicationStopping.Register(() => logger.LogWarning("Application is shutting down"));
_ = lifetime.ApplicationStopped.Register(() => logger.LogWarning("Application has stopped"));
_ = app.Environment.IsDevelopment()
? app.UseDeveloperExceptionPage()
: app.UseExceptionHandler(err => err.Run(context =>
{
var ex = context.Features.Get<IExceptionHandlerFeature>()?.Error;
if (ex != null)
logger.LogError(ex, "Unhandled exception on {Method} {Path}", context.Request.Method, context.Request.Path);
context.Response.StatusCode = 500;
return Task.CompletedTask;
}));
var api = app.MapGroup(SystemEnvironmentVariables.Instance.ApiPrefix);
_ = api.MapHealthChecks("/health");
_ = api.MapHealthChecks("/alive", new HealthCheckOptions { Predicate = r => r.Tags.Contains("live") });
var v1 = api.MapGroup("/v1");
var mapOtlpEndpoints = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
v1.MapElasticDocsApiEndpoints(mapOtlpEndpoints);
Console.WriteLine("API endpoints mapped");
Console.WriteLine("Application startup completed successfully");
app.Run();
}
catch (Exception ex)
{
Console.WriteLine($"FATAL ERROR: {ex}");
Console.WriteLine($"Exception type: {ex.GetType().FullName}");
Console.WriteLine($"Message: {ex.Message}");
if (ex.InnerException != null)
Console.WriteLine($"Inner exception: {ex.InnerException.GetType().FullName}: {ex.InnerException.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
throw;
}
static void LogElasticsearchConfiguration(WebApplication app, ILogger logger)
{
try
{
var clientAccessor = app.Services.GetService<ElasticsearchClientAccessor>();
if (clientAccessor is not null)
{
logger.LogInformation(
"Elasticsearch configuration - Url: {Url}, SearchIndex: {SearchIndex}",
clientAccessor.Endpoint.Uri,
clientAccessor.SearchIndex
);
}
else
logger.LogWarning("ElasticsearchClientAccessor could not be resolved from DI");
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to resolve Elasticsearch configuration");
}
}
// Make the Program class accessible for integration testing
#pragma warning disable ASP0027
public partial class Program { }
#pragma warning restore ASP0027