Files
pushy/Pushy/Pushy/Program.cs
2024-12-13 18:04:00 +01:00

109 lines
2.9 KiB
C#

using Elastic.Extensions.Logging;
using Elastic.Extensions.Logging.Options;
using Elastic.Transport;
using Microsoft.AspNetCore.DataProtection;
using Pushy.Components;
using Pushy.Domain;
using StackExchange.Redis;
var builder = WebApplication.CreateBuilder(args);
string? connectionString = builder.Configuration.GetConnectionString("Valkey");
if (string.IsNullOrEmpty(connectionString))
throw new InvalidOperationException("Missing Valkey connection string");
builder.UseOrleansClient(silo =>
{
silo.UseRedisClustering(connectionString);
silo.AddActivityPropagation();
});
if (builder.Environment.IsProduction())
{
string? elasticRawUrl = builder.Configuration["Elasticsearch:Url"];
string? elasticApiKey = builder.Configuration["Elasticsearch:ApiKey"];
if (string.IsNullOrEmpty(elasticRawUrl))
throw new InvalidOperationException(
"Missing Elasticsearch URL for logging");
if(string.IsNullOrEmpty(elasticApiKey))
throw new InvalidOperationException("Missing Elasticsearch API key");
IConnectionMultiplexer multiplexer = ConnectionMultiplexer.Connect(connectionString);
builder.Services.AddDataProtection()
.PersistKeysToStackExchangeRedis(multiplexer);
var transport = new TransportConfiguration(new StaticNodePool([new Uri(elasticRawUrl)]))
{
Authentication = new ApiKey(elasticApiKey),
ServerCertificateValidationCallback = (_, _, _, _) => true
};
builder.Logging.AddElasticsearch(opt =>
{
opt.IsEnabled = true;
opt.DataStream = new DataStreamNameOptions()
{
Type = "logs", DataSet = "Pushy", Namespace = "pushy"
};
opt.Transport = new DistributedTransport(transport);
});
}
builder.Services.AddScoped(service =>
service.GetRequiredService<IClusterClient>().GetGrain<ILinkGenerator>(Guid.Empty)
);
builder.Services.AddAllElasticApm();
builder.Services.AddHealthChecks();
builder.Services.AddCors(policy =>
{
policy.AddDefaultPolicy(cors =>
{
cors.WithOrigins("https://henrikml.dk")
.AllowAnyHeader();
});
});
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents();
builder.Services.AddOutputCache();
builder.Services.AddResponseCompression();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
app.UseOutputCache();
}
app.UseHttpsRedirection();
app.UseResponseCompression();
app.UseCors();
app.MapStaticAssets();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(Pushy.Client._Imports).Assembly);
app.MapHealthChecks("/healthz");
app.Run();