-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cs
More file actions
455 lines (405 loc) · 17.6 KB
/
Copy pathapp.cs
File metadata and controls
455 lines (405 loc) · 17.6 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
using System.Linq;
using System.Runtime.CompilerServices;
using System.Reflection;
using CLOOPS.microservices.Readyz;
using CLOOPS.NATS;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Serilog;
using Serilog.Formatting.Compact;
using Serilog.Sinks.SystemConsole.Themes;
namespace CLOOPS.microservices;
/// <summary>
/// Coordinates dependency injection setup and application startup.
/// </summary>
public partial class App
{
/// <summary>
/// The application settings
/// </summary>
public BaseAppSettings appSettings;
/// <summary>
/// The host application builder
/// </summary>
public WebApplicationBuilder builder;
/// <summary>
/// The host application
/// </summary>
public WebApplication? host;
/// <summary>
/// Stores all the types in the assembly for faster startup.
/// </summary>
private Type[]? cachedTargetTypes;
/// <summary>
/// Creates the DI pipeline and starts the application.
/// </summary>
/// <param name="introMessageProvider">Optional function that takes BaseAppSettings and WebApplicationBuilder and returns a custom intro message. If not provided, a default message will be used.</param>
public App(Func<BaseAppSettings, WebApplicationBuilder, string>? introMessageProvider = null)
{
appSettings = new BaseAppSettings();
ConfigureThreadPool();
builder = WebApplication.CreateSlimBuilder();
ConfigureRestListener();
string introMessage = introMessageProvider != null
? introMessageProvider(appSettings, builder)
: $@"
_____ _ _ _
/ ____| | | (_) | |
| | ___ _ __ _ __ ___ ___| |_ _ ___ _ __ | | ___ ___ _ __ ___
| | / _ \| '_ \| '_ \ / _ \/ __| __| |/ _ \| '_ \ | | / _ \ / _ \| '_ \/ __|
| |___| (_) | | | | | | | __/ (__| |_| | (_) | | | | | |___| (_) | (_) | |_) \__ \
\_____\___/|_| |_|_| |_|\___|\___|\__|_|\___/|_| |_| |______\___/ \___/| .__/|___/
| |
|_|
╔╦╗┬┌─┐┬─┐┌─┐┌─┐┌─┐┬─┐┬ ┬┬┌─┐┌─┐┌─┐
║║║││ ├┬┘│ │└─┐├┤ ├┬┘└┐┌┘││ ├┤ └─┐
╩ ╩┴└─┘┴└─└─┘└─┘└─┘┴└─ └┘ ┴└─┘└─┘└─┘
App: {appSettings.AssemblyName}
Env: {builder.Environment.EnvironmentName}
NATS URL: {appSettings.NatsURL}
TB Addresses: {appSettings.TigerBeetleAddresses}
TB Cluster ID: {appSettings.TigerBeetleClusterId}
OTEL Endpoint: {appSettings.OtelEndpoint}
Cluster: {appSettings.Cluster}
Enable NATS Consumers: {appSettings.EnableNatsConsumers}
Snowflake ID: {(appSettings.EnableSnowflakeId ? $"enabled (generator-id {appSettings.SnowflakeGeneratorId})" : "disabled")}
";
Console.WriteLine(introMessage);
Console.WriteLine("Boostrapping app...");
ConfigureLogger();
Log.Information("✅ Configured Serilog");
// add singleton services
builder.Services.AddSingleton(appSettings);
Log.Information("✅ Mapped AppSettings");
if (!string.IsNullOrEmpty(appSettings.ConnectionString))
{
builder.Services.AddSingleton<IDB>(new DB(appSettings.ConnectionString));
Log.Information("✅ Configured DB");
}
if (!string.IsNullOrEmpty(appSettings.NatsURL))
{
var cnc = new CloopsNatsClient(
url: appSettings.NatsURL,
name: appSettings.AssemblyName,
creds: (!string.IsNullOrEmpty(appSettings.NatsCreds)) ? appSettings.NatsCreds : null
);
builder.Services.AddSingleton<ICloopsNatsClient>(cnc);
builder.Services.AddHostedService<NatsLifecycleService>();
builder.Services.AddSingleton<INatsMetricsService, NatsMetricsService>();
Log.Information("✅ Configured NATS Client, Lifecycle Service, and Metrics Service");
}
ConfigureOTEL();
ConfigureCaching();
RegisterControllers();
RegisterServices();
RegisterRestEndpoints();
RegisterHttpServices();
// Hosted services start in registration order. NATS is registered above so
// migrations can acquire a distributed lock before caches/background jobs run.
builder.Services.AddHostedService<DbMigrationHostedService>();
Log.Information("✅ Registered DB migration hosted service");
ConfigureTigerBeetle(appSettings);
ConfigureSnowflake();
// Cache services must register before background services so that hosted-service
// start order is: NATS → migrations → TigerBeetle/cache (incl. optional blocking startup hydration) → background jobs.
RegisterCacheServices();
RegisterBackgroundServices();
}
/// <summary>
/// Runs the application asynchronously
/// usage: await app.RunAsync().ConfigureAwait(false);
/// </summary>
/// <returns>A task that represents the asynchronous operation</returns>
public Task RunAsync()
{
// build it
host = builder.Build();
MapRestEndpoints(host);
return host.RunAsync();
}
private void RegisterControllers()
{
var controllerTypes = GetTargetTypes()
.Where(t =>
{
var ns = t.Namespace;
return !string.IsNullOrEmpty(ns) &&
ns.EndsWith("Controllers", StringComparison.OrdinalIgnoreCase);
})
.ToArray();
foreach (var controllerType in controllerTypes)
{
var interfaceType = FindInterface(controllerType);
if (interfaceType != null)
{
builder.Services.AddSingleton(interfaceType, controllerType);
Log.Information("✅ Registered controller: {InterfaceName} -> {ControllerName}", interfaceType.Name, controllerType.Name);
}
else
{
// Fallback: register concrete type if no interface found
builder.Services.AddSingleton(controllerType);
Log.Information("✅ Registered controller (no interface): {ControllerName}", controllerType.Name);
}
}
}
private void RegisterServices()
{
var serviceTypes = GetTargetTypes()
.Where(t =>
{
var ns = t.Namespace;
if (string.IsNullOrEmpty(ns))
{
return false;
}
var endsWithServices = ns.EndsWith("Services", StringComparison.OrdinalIgnoreCase);
var endsWithBackground = ns.EndsWith("Services.Background", StringComparison.OrdinalIgnoreCase);
var endsWithHttp = ns.EndsWith("Services.Http", StringComparison.OrdinalIgnoreCase);
return endsWithServices && !endsWithBackground && !endsWithHttp;
})
.ToArray();
foreach (var serviceType in serviceTypes)
{
var interfaceType = FindInterface(serviceType);
if (interfaceType != null)
{
builder.Services.AddSingleton(interfaceType, serviceType);
Log.Information("✅ Registered service: {InterfaceName} -> {ServiceName}", interfaceType.Name, serviceType.Name);
}
else
{
// Fallback: register concrete type if no interface found
builder.Services.AddSingleton(serviceType);
Log.Information("✅ Registered service (no interface): {ServiceName}", serviceType.Name);
}
}
}
/// <summary>
/// Finds the interface for a given type following the convention: interface starts with "I" and is in the same namespace.
/// </summary>
/// <param name="type">The concrete type to find an interface for</param>
/// <returns>The interface type if found, null otherwise</returns>
private Type? FindInterface(Type type)
{
var typeNamespace = type.Namespace;
if (string.IsNullOrEmpty(typeNamespace))
{
return null;
}
// Check all interfaces that the type implements
// Convention: Interface starts with "I" and is in the same namespace
var interfaceType = type
.GetInterfaces()
.FirstOrDefault(i => i.Name.StartsWith("I", StringComparison.Ordinal) &&
i.Namespace == typeNamespace);
return interfaceType;
}
private void RegisterBackgroundServices()
{
var backgroundServiceTypes = GetTargetTypes()
.Where(t =>
{
var ns = t.Namespace;
return !string.IsNullOrEmpty(ns) &&
ns.EndsWith("Services.Background", StringComparison.OrdinalIgnoreCase);
})
.Where(t => typeof(IHostedService).IsAssignableFrom(t))
.ToArray();
foreach (var backgroundServiceType in backgroundServiceTypes)
{
builder.Services.AddSingleton(typeof(IHostedService), backgroundServiceType);
Log.Information("✅ Registered background service: {BackgroundServiceName}", backgroundServiceType.Name);
}
}
private void RegisterHttpServices()
{
builder.Services.AddHttpClient();
var httpServiceTypes = GetTargetTypes()
.Where(t => typeof(BaseHttpService).IsAssignableFrom(t))
.ToArray();
foreach (var httpServiceType in httpServiceTypes)
{
var interfaceType = FindInterface(httpServiceType);
if (interfaceType != null)
{
builder.Services.AddSingleton(interfaceType, httpServiceType);
Log.Information("✅ Registered HTTP service: {InterfaceName} -> {HttpServiceName}", interfaceType.Name, httpServiceType.Name);
}
else
{
builder.Services.AddSingleton(httpServiceType);
Log.Information("✅ Registered HTTP service (no interface): {HttpServiceName}", httpServiceType.Name);
}
}
}
private Type[] GetTargetTypes()
{
if (cachedTargetTypes != null)
{
return cachedTargetTypes;
}
var targetAssembly = ResolveTargetAssembly();
cachedTargetTypes = targetAssembly
.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && !t.IsNested)
.Where(t => !t.IsDefined(typeof(CompilerGeneratedAttribute), inherit: false))
.ToArray();
return cachedTargetTypes;
}
private Assembly ResolveTargetAssembly()
{
var targetAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a =>
string.Equals(
a.GetName().Name,
appSettings.AssemblyName,
StringComparison.OrdinalIgnoreCase))
?? Assembly.GetEntryAssembly()
?? Assembly.GetExecutingAssembly();
if (targetAssembly == null)
{
Log.Error("❌ No assembly found for registration");
throw new Exception("No assembly found for registration.");
}
return targetAssembly;
}
private void ConfigureLogger()
{
var environment = builder.Environment.EnvironmentName;
var loggerConfig = new LoggerConfiguration()
// Minimum levels
.MinimumLevel.Information()
// Override noisy framework namespaces
.MinimumLevel.Override("System.Net.Http", Serilog.Events.LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
.MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Warning)
// Enrichers
.Enrich.FromLogContext()
.Enrich.WithThreadId()
.Enrich.WithThreadName()
.Enrich.WithProperty("Application", appSettings.AssemblyName);
if (appSettings.Debug)
{
loggerConfig = loggerConfig.MinimumLevel.Debug();
}
// Configure console sink based on environment
if (environment.Equals("Production", StringComparison.OrdinalIgnoreCase))
{
// Production: Use compact JSON for structured logging
loggerConfig = loggerConfig.WriteTo.Console(new CompactJsonFormatter());
}
else
{
// Non-production: Use human-friendly colorful console
loggerConfig = loggerConfig.WriteTo.Console(
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
theme: AnsiConsoleTheme.Code
);
}
Log.Logger = loggerConfig.CreateLogger();
builder.Logging.ClearProviders();
builder.Logging.AddSerilog(dispose: true);
}
private void ConfigureThreadPool()
{
// Give the ThreadPool headroom under bursty loads
ThreadPool.GetMinThreads(out var worker, out var io);
var cpu = Environment.ProcessorCount;
// bump min worker threads: enough to keep responders busy, not too high
ThreadPool.SetMinThreads(Math.Max(worker, cpu * 2), io);
}
private void ConfigureOTEL()
{
string otelServiceName = appSettings.AssemblyName;
string otelServiceEndpoint = appSettings.OtelEndpoint;
string otelHeaders = appSettings.OtelHeaders;
string clusterName = appSettings.Cluster;
string appName = otelServiceName;
ResourceBuilder resourceBuilder = ResourceBuilder.CreateDefault()
.AddService(
serviceName: otelServiceName,
serviceVersion: Assembly.GetEntryAssembly()?.GetName().Version?.ToString(),
serviceInstanceId: Environment.MachineName
)
.AddAttributes(new Dictionary<string, object>
{
["cluster"] = clusterName,
["app"] = appName,
["job"] = appName
});
builder.Services.AddOpenTelemetry()
.WithMetrics(meterProviderBuilder =>
{
meterProviderBuilder
.SetResourceBuilder(resourceBuilder: resourceBuilder)
.AddHttpClientInstrumentation()
.AddSqlClientInstrumentation()
.AddMeter("AppMetrics")
.AddMeter("NatsMetrics")
.AddRuntimeInstrumentation();
// Only add OTLP exporter if endpoint is configured
if (!string.IsNullOrEmpty(otelServiceEndpoint))
{
meterProviderBuilder.AddOtlpExporter(op =>
{
op.Endpoint = new Uri(otelServiceEndpoint);
op.Headers = otelHeaders;
op.Protocol = OtlpExportProtocol.Grpc;
});
}
})
.WithTracing(traceProviderBuilder =>
{
traceProviderBuilder
.AddSource(appSettings.AssemblyName)
.SetResourceBuilder(resourceBuilder)
.AddHttpClientInstrumentation()
.AddSqlClientInstrumentation();
// Only add OTLP exporter if endpoint is configured
if (!string.IsNullOrEmpty(otelServiceEndpoint))
{
traceProviderBuilder.AddOtlpExporter(op =>
{
op.Endpoint = new Uri(otelServiceEndpoint);
op.Headers = otelHeaders;
op.Protocol = OtlpExportProtocol.Grpc;
});
}
});
Log.Information("✅ Configured OpenTelemetry");
}
/// <summary>
/// Adds TigerBeetle Client to DI
/// </summary>
private void ConfigureTigerBeetle(BaseAppSettings appSettings)
{
if (String.IsNullOrWhiteSpace(appSettings.TigerBeetleAddresses))
{
Log.Information("ℹ️ No TigerBeetle database configured");
return;
}
var addresses = appSettings.TigerBeetleAddresses
.Split(",", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
if (addresses.Length == 0)
{
Log.Warning("⚠️ No valid TigerBeetle addresses configured");
return;
}
builder.Services.AddSingleton(_ =>
new TigerBeetle.Client(appSettings.TigerBeetleClusterId, addresses));
Log.Information("✅ Configured TigerBeetle client");
// Register the L1-only readiness cache so /readyz reads a cached probe result
// instead of hitting TigerBeetle on every request.
builder.Services.AddSingleton<TigerBeetleReadinessCacheService>();
builder.Services.AddSingleton<IHostedService>(sp =>
sp.GetRequiredService<TigerBeetleReadinessCacheService>());
Log.Information("✅ Registered TigerBeetle readiness cache (L1-only, 5m TTL, 4m refresh)");
}
}