-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
107 lines (85 loc) · 3.23 KB
/
Copy pathProgram.cs
File metadata and controls
107 lines (85 loc) · 3.23 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
using System.Net.Http.Headers;
using BooleanApi.Services;
using BooleanApi.Validation;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers()
.AddJsonOptions(o =>
{
// Enables polymorphic JSON using attributes in the AST models
o.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<BooleanApi.Validation.FieldCardinality>();
builder.Services.AddSingleton<BooleanApi.Validation.ImpossibleQueryDetector>();
builder.Services.Configure<OpenSearchOptions>(builder.Configuration.GetSection("OpenSearch"));
// Field allow-list registry
builder.Services.AddSingleton<FieldRegistry>();
builder.Services.AddSingleton<ContainsFieldRegistry>();
// Query translation (Boolean AST -> OpenSearch DSL)
builder.Services.AddSingleton<QueryTranslator>();
// OpenSearch client service
builder.Services.AddHttpClient<OpenSearchService>((sp, client) =>
{
var opts = sp.GetRequiredService<IOptions<OpenSearchOptions>>().Value;
client.BaseAddress = new Uri(opts.BaseUrl.TrimEnd('/') + "/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Optional auth
if (!string.IsNullOrWhiteSpace(opts.ApiKey))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("ApiKey", opts.ApiKey);
}
else if (!string.IsNullOrWhiteSpace(opts.Username))
{
var raw = $"{opts.Username}:{opts.Password}";
var b64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(raw));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", b64);
}
});
builder.Services.AddControllers()
.AddJsonOptions(o =>
{
o.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
o.JsonSerializerOptions.Converters.Add(new BooleanApi.Models.JsonElementValueConverter());
});
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
app.UseCors("AllowAll");
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
/*
// This code can be commented out in production v
app.Use(async (context, next) =>
{
// Check if it's the specific port or path to avoid noise
if (context.Request.Method == "POST")
{
context.Request.EnableBuffering();
// LeaveOpen: true is critical so the stream isn't closed before the controller sees it
using (var reader = new StreamReader(context.Request.Body, leaveOpen: true))
{
var rawBody = await reader.ReadToEndAsync();
// --- SET BREAKPOINT HERE ---
// Inspect 'rawBody' in VS Code Variables window
context.Request.Body.Position = 0; // Rewind the stream for the next guy
}
}
await next();
});
// This code can be commented out in production ^
*/
app.Run();