-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
396 lines (334 loc) · 15.3 KB
/
Copy pathProgram.cs
File metadata and controls
396 lines (334 loc) · 15.3 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
using SharpClaw.Gateway.Abstractions;
using SharpClaw.Gateway.Configuration;
using SharpClaw.Gateway.Controllers;
using SharpClaw.Gateway.Infrastructure;
using SharpClaw.Gateway.Modules;
using SharpClaw.Gateway.Security;
using SharpClaw.Utils.Logging;
using SharpClaw.Utils.Instances;
using Serilog;
using Serilog.Events;
using SharpClaw.Gateway.Modules.Routing;
using SharpClaw.Gateway.Modules.Hosting;
var builder = WebApplication.CreateBuilder(args);
var gatewayPaths = new SharpClawInstancePaths(
SharpClawInstanceKind.Gateway,
Environment.GetEnvironmentVariable("SHARPCLAW_INSTANCE_ROOT"),
Environment.GetEnvironmentVariable("SHARPCLAW_SHARED_ROOT"));
gatewayPaths.EnsureDirectories();
gatewayPaths.CleanupStaleDiscoveryEntries(TimeSpan.FromMinutes(2));
using var gatewayInstanceLock = new SharpClawInstanceLock(gatewayPaths);
var gatewayManifest = gatewayPaths.Manifest;
var configuredGatewayUrl = builder.Configuration["ASPNETCORE_URLS"]
?? Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
var selectedBackendBaseUrl = builder.Configuration["SharpClawInstance:SelectedBackendBaseUrl"]
?? builder.Configuration[$"{InternalApiOptions.SectionName}:BaseUrl"];
var selectedBackendInstanceId = builder.Configuration["SharpClawInstance:SelectedBackendInstanceId"];
var selectedBackendBindingKind = builder.Configuration["SharpClawInstance:SelectedBackendBindingKind"];
var gatewayManifestChanged = false;
if (!string.Equals(gatewayManifest.BaseUrl, configuredGatewayUrl, StringComparison.OrdinalIgnoreCase))
{
gatewayManifest.BaseUrl = configuredGatewayUrl;
gatewayManifestChanged = true;
}
if (!string.Equals(gatewayManifest.SelectedBackendBaseUrl, selectedBackendBaseUrl, StringComparison.OrdinalIgnoreCase))
{
gatewayManifest.SelectedBackendBaseUrl = selectedBackendBaseUrl;
gatewayManifestChanged = true;
}
if (!string.Equals(gatewayManifest.SelectedBackendInstanceId, selectedBackendInstanceId, StringComparison.Ordinal))
{
gatewayManifest.SelectedBackendInstanceId = selectedBackendInstanceId;
gatewayManifestChanged = true;
}
if (!string.Equals(gatewayManifest.SelectedBackendBindingKind, selectedBackendBindingKind, StringComparison.Ordinal))
{
gatewayManifest.SelectedBackendBindingKind = selectedBackendBindingKind;
gatewayManifestChanged = true;
}
if (gatewayManifestChanged)
gatewayPaths.SaveManifest(gatewayManifest);
await using var sessionLogs = new SessionLogWriter("gateway", gatewayPaths.LogsDirectory);
using var sessionLogCapture = SessionLogCapture.Install(sessionLogs);
var publishedGatewayUrl = !string.IsNullOrWhiteSpace(configuredGatewayUrl)
? configuredGatewayUrl
: gatewayManifest.BaseUrl ?? "http://127.0.0.1:48924";
using var gatewayDiscoveryLease = new SharpClawDiscoveryLease(
gatewayPaths,
publishedGatewayUrl,
TimeSpan.FromSeconds(30));
gatewayDiscoveryLease.PublishNow();
AppDomain.CurrentDomain.UnhandledException += (_, eventArgs) =>
{
if (eventArgs.ExceptionObject is Exception exception)
sessionLogs.AppendException(exception, "Unhandled AppDomain exception in gateway.");
else
sessionLogs.AppendException($"Unhandled AppDomain exception payload: {eventArgs.ExceptionObject}");
};
TaskScheduler.UnobservedTaskException += (_, eventArgs) =>
{
sessionLogs.AppendException(eventArgs.Exception, "Unobserved task exception in gateway.");
};
builder.Logging.ClearProviders();
builder.Host.UseSerilog();
builder.Services.AddSingleton(sessionLogs);
builder.Logging.AddProvider(new SessionLogLoggerProvider(sessionLogs));
// ── Gateway .env (same pattern as Core / Interface) ──────────────
builder.Configuration.AddGatewayEnvironment(
isDevelopment: builder.Environment.IsDevelopment());
var serilogOptions = SerilogEnvironmentOptions.FromConfiguration(builder.Configuration);
if (serilogOptions.Enabled)
{
var loggerConfiguration = new LoggerConfiguration()
.MinimumLevel.Is(SerilogEnvironmentOptions.ParseEnum(
serilogOptions.MinimumLevel,
LogEventLevel.Information))
.MinimumLevel.Override("Microsoft", SerilogEnvironmentOptions.ParseEnum(
serilogOptions.MicrosoftMinimumLevel,
LogEventLevel.Warning))
.MinimumLevel.Override("Microsoft.AspNetCore", SerilogEnvironmentOptions.ParseEnum(
serilogOptions.AspNetCoreMinimumLevel,
LogEventLevel.Warning))
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", SerilogEnvironmentOptions.ParseEnum(
serilogOptions.EntityFrameworkCoreMinimumLevel,
LogEventLevel.Warning))
.Enrich.FromLogContext()
.WriteTo.Sink(new SessionLogSerilogSink(sessionLogs));
if (serilogOptions.ConsoleEnabled)
loggerConfiguration = loggerConfiguration.WriteTo.Console();
if (serilogOptions.FileEnabled)
loggerConfiguration = loggerConfiguration.WriteTo.File(
sessionLogs.SerilogFilePath,
rollingInterval: RollingInterval.Infinite);
Log.Logger = loggerConfiguration.CreateLogger();
}
else
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Fatal()
.CreateLogger();
}
// ── Internal API client ──────────────────────────────────────────
builder.Services.Configure<InternalApiOptions>(
builder.Configuration.GetSection(InternalApiOptions.SectionName));
builder.Services.AddHttpClient<InternalApiClient>(client =>
{
var section = builder.Configuration.GetSection(InternalApiOptions.SectionName);
client.BaseAddress = new Uri(section["BaseUrl"] ?? "http://127.0.0.1:48923");
client.Timeout = int.TryParse(section["TimeoutSeconds"], out var t) && t > 0
? TimeSpan.FromSeconds(t)
: TimeSpan.FromSeconds(300);
});
// ── Gateway endpoint configuration ──────────────────────────────
builder.Services.Configure<GatewayEndpointOptions>(
builder.Configuration.GetSection(GatewayEndpointOptions.SectionName));
// ── Gateway-side module discovery (Phase 2) ─────────────────────
// Loader runs here so DI can hand the catalog/loader to middleware,
// but MapEndpoints / ConfigureGatewayServices stays deferred to Phase 3.
builder.Services.Configure<GatewayModuleOptions>(
builder.Configuration.GetSection(GatewayModuleOptions.SectionName));
var moduleDiscoveryLogger = LoggerFactory
.Create(b => b.AddSerilog(Log.Logger))
.CreateLogger("SharpClaw.Gateway.Modules");
var gatewayModuleLoader = GatewayModuleLoader.DiscoverBundled(moduleDiscoveryLogger);
foreach (var ext in gatewayModuleLoader.All)
{
Log.Information(
"Gateway module discovered: {ModuleId} ({DisplayName})",
ext.ModuleId,
ext.DisplayName);
}
builder.Services.AddSingleton(gatewayModuleLoader);
builder.Services.AddSingleton<GatewayEndpointGroupCatalog>();
builder.Services.AddSingleton<ModuleEndpointDataSource>();
builder.Services.AddSingleton<GatewayModuleHostManager>();
// ── Gateway-side module service registration (Phase 3) ─────────
// Run ConfigureGatewayServices only for modules explicitly enabled in
// configuration so a disabled module's services don't leak into DI.
var gatewayModuleOptionsSnapshot = builder.Configuration
.GetSection(GatewayModuleOptions.SectionName)
.Get<GatewayModuleOptions>() ?? new GatewayModuleOptions();
foreach (var ext in gatewayModuleLoader.All)
{
if (!gatewayModuleOptionsSnapshot.IsModuleEnabled(ext.ModuleId))
continue;
try
{
ext.ConfigureGatewayServices(builder.Services);
Log.Information("Gateway module services configured: {ModuleId}", ext.ModuleId);
}
catch (Exception ex)
{
Log.Error(ex,
"Gateway module {ModuleId} threw during ConfigureGatewayServices; module will not be mapped.",
ext.ModuleId);
}
}
// ── Request queue (sequential forwarding to core API) ────────────
builder.Services.Configure<RequestQueueOptions>(
builder.Configuration.GetSection(RequestQueueOptions.SectionName));
builder.Services.AddSingleton<QueueMetrics>();
builder.Services.AddSingleton<RequestQueueService>();
builder.Services.AddHostedService<RequestQueueProcessor>();
builder.Services.AddScoped<GatewayRequestDispatcher>();
builder.Services.AddHttpContextAccessor();
// ── Security
builder.Services.AddSingleton<IpBanService>();
builder.Services.AddSharpClawRateLimiting();
// ── MVC & OpenAPI ────────────────────────────────────────────────
builder.Services.AddControllers(options =>
{
options.Filters.Add<ErrorEnvelopeFilter>();
})
.AddJsonOptions(o =>
{
o.JsonSerializerOptions.Converters.Add(
new System.Text.Json.Serialization.JsonStringEnumConverter());
});
builder.Services.AddOpenApi(options =>
{
options.AddDocumentTransformer((doc, _, _) =>
{
doc.Info.Title = "SharpClaw Gateway";
doc.Info.Version = "v1";
doc.Info.Description = "Public REST gateway for the SharpClaw Application API.";
return Task.CompletedTask;
});
});
// ── API key diagnostic (visible in Uno process output) ───────────
var configuredApiKey = builder.Configuration[$"{InternalApiOptions.SectionName}:ApiKey"];
if (!string.IsNullOrEmpty(configuredApiKey))
{
Console.WriteLine($"[gateway] API key resolved from config: {configuredApiKey.Length} chars, prefix={configuredApiKey[..Math.Min(6, configuredApiKey.Length)]}..");
sessionLogs.AppendDebug($"API key resolved from config: {configuredApiKey.Length} chars.");
}
else
{
Console.WriteLine("[gateway] ⚠ No API key found in configuration — will fall back to file read.");
sessionLogs.AppendDebug("No API key found in configuration; will fall back to file read.");
}
var app = builder.Build();
// ── Response telemetry headers ───────────────────────────────────
app.Use(async (context, next) =>
{
// Set RequestId early so error envelopes in downstream middleware can use it
var requestId = Guid.NewGuid().ToString("N");
context.Items["RequestId"] = requestId;
context.Response.OnStarting(() =>
{
var queueSvc = context.RequestServices.GetService<RequestQueueService>();
var meta = context.Items.TryGetValue("QueueMeta", out var m) && m is QueueResponseMeta qm
? qm : null;
// X-Request-Id — correlation ID on every response (prefer queue's if available)
context.Response.Headers["X-Request-Id"] = meta?.RequestId.ToString("N") ?? requestId;
// X-RateLimit-Limit — applicable rate limit for this path
var path = context.Request.Path.Value ?? string.Empty;
var rateCatalog = context.RequestServices.GetService<GatewayEndpointGroupCatalog>();
context.Response.Headers["X-RateLimit-Limit"] =
RateLimiterConfiguration.ResolveRateLimit(path, rateCatalog).ToString();
// Cache-Control — short cache for reads, no-store for mutations
if (!context.Response.Headers.ContainsKey("Cache-Control"))
{
context.Response.Headers.CacheControl = context.Request.Method == "GET"
? "private, max-age=5"
: "no-store";
}
// Queue load indicators — present when the queue is enabled
if (queueSvc?.Enabled == true)
{
context.Response.Headers["X-Queue-Pending"] = queueSvc.PendingCount.ToString();
var avg = queueSvc.Metrics.AverageProcessingMs;
if (avg > 0)
context.Response.Headers["X-Queue-Avg-Ms"] = avg.ToString("F0");
}
// Per-request queue metadata — queued mutations only
if (meta is not null)
{
context.Response.Headers["X-Queue-Position"] = meta.Position.ToString();
context.Response.Headers["X-Queue-Processing-Ms"] = meta.ProcessingMs.ToString("F0");
}
// Retry-After on 503 (queue full) — estimated wait in seconds
if (context.Response.StatusCode == 503 && context.Items.ContainsKey("QueueFull"))
{
var avgMs = queueSvc?.Metrics.AverageProcessingMs > 0
? queueSvc.Metrics.AverageProcessingMs : 5000;
var pending = queueSvc?.PendingCount ?? 0;
context.Response.Headers["Retry-After"] = Math.Max(5,
(int)Math.Ceiling(pending * avgMs / 1000.0)).ToString();
}
return Task.CompletedTask;
});
await next();
});
// ── Health probes (short-circuit before security) ────────────────
app.Use(async (context, next) =>
{
var path = context.Request.Path;
if (path.StartsWithSegments("/healthz"))
{
context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(new { status = "healthy" });
return;
}
if (path.StartsWithSegments("/readyz"))
{
var queueSvc = context.RequestServices.GetRequiredService<RequestQueueService>();
var coreApiClient = context.RequestServices.GetRequiredService<InternalApiClient>();
var checks = new Dictionary<string, string>
{
["queue"] = queueSvc.Enabled ? "ok" : "disabled"
};
try
{
using var probe = new HttpRequestMessage(HttpMethod.Get, "/health");
using var response = await coreApiClient.SendRawAsync(probe, CancellationToken.None);
checks["coreApi"] = response.IsSuccessStatusCode ? "ok" : $"status:{(int)response.StatusCode}";
}
catch
{
checks["coreApi"] = "unreachable";
}
var ready = checks.Values.All(v => v is "ok" or "disabled");
context.Response.StatusCode = ready ? 200 : 503;
await context.Response.WriteAsJsonAsync(new { status = ready ? "ready" : "not_ready", checks });
return;
}
await next();
});
// ── Middleware pipeline (order matters) ──────────────────────────
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/openapi/v1.json", "SharpClaw Gateway v1");
});
}
app.UseHttpsRedirection();
if (serilogOptions.Enabled && serilogOptions.RequestLoggingEnabled)
app.UseSerilogRequestLogging();
// 1. Endpoint gate — reject requests to disabled endpoint groups
app.UseMiddleware<EndpointGateMiddleware>();
// 2. IP ban check — reject banned IPs before any other processing
app.UseMiddleware<IpBanMiddleware>();
// 3. Anti-spam — body size, content-type validation
app.UseMiddleware<AntiSpamMiddleware>();
// 4. Rate limiting
app.UseRateLimiter();
((IApplicationBuilder)app).Properties[GatewayModuleEndpointMapping.RateLimiterReadyKey] = true;
app.UseAuthorization();
app.MapControllers();
app.MapChatStreamProxy();
// ── Module-contributed endpoint groups (Phase 3) ────────────────
// Must run AFTER UseRateLimiter so RequireRateLimiting on the route
// groups attaches the limiter middleware in the correct order.
app.MapGatewayModuleEndpoints();
try
{
app.Run();
}
finally
{
gatewayPaths.DeleteDiscoveryEntry();
}
await Log.CloseAndFlushAsync();