Introduce configuration classes and environment-specific settings files. Add DI extensions for registering API/app settings and HttpClient. Implement ITuneRipperApiService with robust error handling and logging. Update Program.cs for config loading and startup logging. Add usage documentation and update imports for new services and extensions. Improves maintainability and developer experience.
47 lines
1.6 KiB
C#
47 lines
1.6 KiB
C#
using TuneRipper.Services;
|
|
using TuneRipper.Shared.Configuration;
|
|
|
|
namespace TuneRipper.Extensions;
|
|
|
|
public static class ServiceCollectionExtensions
|
|
{
|
|
public static IServiceCollection AddTuneRipperServices(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
// Configure settings
|
|
services.Configure<ApiSettings>(configuration.GetSection(ApiSettings.SectionName));
|
|
services.Configure<AppSettings>(configuration.GetSection(AppSettings.SectionName));
|
|
|
|
// Configure HttpClient for API calls
|
|
services.AddScoped(sp =>
|
|
{
|
|
var apiSettings = configuration.GetSection(ApiSettings.SectionName).Get<ApiSettings>();
|
|
var client = new HttpClient();
|
|
|
|
// Try HTTPS first, fallback to HTTP if configured
|
|
if (!string.IsNullOrEmpty(apiSettings?.BaseUrl))
|
|
{
|
|
client.BaseAddress = new Uri(apiSettings.BaseUrl);
|
|
}
|
|
else if (!string.IsNullOrEmpty(apiSettings?.HttpBaseUrl))
|
|
{
|
|
client.BaseAddress = new Uri(apiSettings.HttpBaseUrl);
|
|
}
|
|
else
|
|
{
|
|
// Default fallback
|
|
client.BaseAddress = new Uri("https://localhost:7160");
|
|
}
|
|
|
|
client.Timeout = TimeSpan.FromSeconds(apiSettings?.TimeoutSeconds ?? 30);
|
|
|
|
return client;
|
|
});
|
|
|
|
// Register API service
|
|
services.AddScoped<ITuneRipperApiService, TuneRipperApiService>();
|
|
|
|
return services;
|
|
}
|
|
} |