Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,182 changes: 456 additions & 726 deletions NuciSearch.UnitTests/Services/SearchServiceTests.cs

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions NuciSearch/Localisation/IpCultureProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

namespace NuciSearch.Localisation
{
public sealed class IpCultureProvider(IGeolocationService geolocationService) : IRequestCultureProvider
public sealed class IpCultureProvider(
IGeolocationService geolocationService) : IRequestCultureProvider
{
public async Task<ProviderCultureResult?> DetermineProviderCultureResult(HttpContext httpContext)
public async Task<ProviderCultureResult?> DetermineProviderCultureResult(
HttpContext httpContext)
{
string ipAddress = string.Empty;

Expand All @@ -17,7 +19,8 @@ public sealed class IpCultureProvider(IGeolocationService geolocationService) :
ipAddress = httpContext.Connection.RemoteIpAddress.ToString();
}

if (httpContext.Request.Headers.TryGetValue("X-Forwarded-For", out StringValues forwardedFor))
if (httpContext.Request.Headers.TryGetValue(
"X-Forwarded-For", out StringValues forwardedFor))
{
string firstIp = forwardedFor.ToString().Split(',')[0].Trim();

Expand All @@ -30,7 +33,7 @@ public sealed class IpCultureProvider(IGeolocationService geolocationService) :
string countryCode = await geolocationService.GetCountryCodeAsync(ipAddress);
string culture = "en-GB";

if (countryCode.Equals("RO"))
if (string.Equals(countryCode, "RO"))
{
culture = "ro-RO";
}
Expand Down
3 changes: 2 additions & 1 deletion NuciSearch/Logging/NuciSearchOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace NuciSearch.Logging
{
public sealed class NuciSearchOperation : Operation
{
public static Operation GetCountryCode => new NuciSearchOperation(nameof(GetCountryCode));
public static Operation GetCountryCode
=> new NuciSearchOperation(nameof(GetCountryCode));

public static Operation Search => new NuciSearchOperation(nameof(Search));

Expand Down
11 changes: 8 additions & 3 deletions NuciSearch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public static void Main(string[] args)
options.DefaultRequestCulture = new RequestCulture("en-GB");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = [app.Services.GetRequiredService<IpCultureProvider>()];
options.RequestCultureProviders =
[app.Services.GetRequiredService<IpCultureProvider>()];
});

app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
app.UseStatusCodePagesWithReExecute(
"/not-found", createScopeForStatusCodePages: true);
app.UseAntiforgery();

app.MapGet("/opensearch.xml", (IStringLocalizer<SharedResources> L) =>
Expand All @@ -54,7 +56,10 @@ public static void Main(string[] args)
<Url type="text/html" method="get" template="https://search.nuilandia.ro?q={searchTerms}" />
</OpenSearchDescription>
""";
return Results.Content(xml, "application/opensearchdescription+xml", System.Text.Encoding.UTF8);
return Results.Content(
xml,
"application/opensearchdescription+xml",
Encoding.UTF8);
});

app.MapStaticAssets();
Expand Down
3 changes: 2 additions & 1 deletion NuciSearch/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace NuciSearch
{
internal static class ServiceCollectionExtensions
{
internal static IServiceCollection AddNuciSearchServices(this IServiceCollection services, IConfiguration configuration)
internal static IServiceCollection AddNuciSearchServices(
this IServiceCollection services, IConfiguration configuration)
{
NuciLoggerSettings loggingSettings = new();
configuration.Bind(nameof(NuciLoggerSettings), loggingSettings);
Expand Down
12 changes: 9 additions & 3 deletions NuciSearch/Services/GeolocationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

namespace NuciSearch.Services
{
public sealed class GeolocationService(IHttpClientFactory httpClientFactory, IMemoryCache cache, ILogger logger) : IGeolocationService
public sealed class GeolocationService(
IHttpClientFactory httpClientFactory,
IMemoryCache cache,
ILogger logger) : IGeolocationService
{
public async Task<string> GetCountryCodeAsync(string ipAddress)
{
Expand Down Expand Up @@ -43,7 +46,10 @@ public async Task<string> GetCountryCodeAsync(string ipAddress)
}
catch (Exception exception)
{
logger.Error(NuciSearchOperation.GetCountryCode, OperationStatus.Failure, exception,
logger.Error(
NuciSearchOperation.GetCountryCode,
OperationStatus.Failure,
exception,
[new(NuciSearchLogInfoKey.IpAddress, ipAddress)]);

return "GB";
Expand All @@ -57,7 +63,7 @@ private static bool IsPrivateOrLoopback(string ipAddress)
return true;
}

if (ipAddress.Equals("::1") || ipAddress.Equals("127.0.0.1"))
if (string.Equals(ipAddress, "::1") || string.Equals(ipAddress, "127.0.0.1"))
{
return true;
}
Expand Down
Loading