50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
namespace HaCompanion;
|
|
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
ConfigureHttpClientFactory(builder);
|
|
|
|
builder.Services.AddHostedService<UptimePollerService>();
|
|
builder.Services.AddControllers();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
}
|
|
|
|
public static void ConfigureHttpClientFactory(WebApplicationBuilder builder)
|
|
{
|
|
IHttpClientBuilder? httpBuilder = null;
|
|
|
|
foreach (var config in builder.Configuration.GetRequiredSection("Uptime").GetRequiredSection("Websites").GetChildren())
|
|
{
|
|
httpBuilder = builder.Services.AddHttpClient(config.GetRequiredSection("Website").Value!,
|
|
(serviceProvider, client) =>
|
|
{
|
|
client.BaseAddress = new Uri(config.GetRequiredSection("BaseUrl").Value!);
|
|
var headers = config.GetSection("RequestHeaders").GetChildren();
|
|
foreach (var headerPair in headers)
|
|
client.DefaultRequestHeaders.Add(headerPair.Key, headerPair.Value);
|
|
});
|
|
}
|
|
|
|
httpBuilder?.ConfigurePrimaryHttpMessageHandler(() =>
|
|
{
|
|
return new SocketsHttpHandler
|
|
{
|
|
PooledConnectionLifetime = TimeSpan.FromMinutes(15),
|
|
};
|
|
}).SetHandlerLifetime(Timeout.InfiniteTimeSpan);
|
|
}
|
|
} |