From 4d7bd85490dd3ff7b42c621cf301d24200700011 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 9 Oct 2017 14:21:20 -0400 Subject: [PATCH] domain mapping service and more cleanup --- src/Icons/Controllers/IconsController.cs | 20 ++++++++--------- src/Icons/IconsSettings.cs | 1 + src/Icons/Services/DomainMappingService.cs | 24 +++++++++++++++++++++ src/Icons/Services/IDomainMappingService.cs | 7 ++++++ src/Icons/Startup.cs | 2 ++ src/Icons/appsettings.json | 3 ++- 6 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 src/Icons/Services/DomainMappingService.cs create mode 100644 src/Icons/Services/IDomainMappingService.cs diff --git a/src/Icons/Controllers/IconsController.cs b/src/Icons/Controllers/IconsController.cs index 205eb565b..1f1ec5514 100644 --- a/src/Icons/Controllers/IconsController.cs +++ b/src/Icons/Controllers/IconsController.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Threading.Tasks; using Bit.Icons.Models; +using Bit.Icons.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; @@ -11,14 +12,18 @@ namespace Bit.Icons.Controllers [Route("")] public class IconsController : Controller { + private static readonly HttpClient _httpClient = new HttpClient(); private readonly IMemoryCache _memoryCache; + private readonly IDomainMappingService _domainMappingService; private readonly IconsSettings _iconsSettings; public IconsController( IMemoryCache memoryCache, + IDomainMappingService domainMappingService, IOptions iconsSettingsOptions) { _memoryCache = memoryCache; + _domainMappingService = domainMappingService; _iconsSettings = iconsSettingsOptions.Value; } @@ -35,13 +40,13 @@ namespace Bit.Icons.Controllers return new BadRequestResult(); } - var iconUrl = BuildIconUrl(uri); - var icon = await _memoryCache.GetOrCreateAsync(domain, async entry => + var mappedDomain = _domainMappingService.MapDomain(uri.Host); + var icon = await _memoryCache.GetOrCreateAsync(mappedDomain, async entry => { - entry.AbsoluteExpiration = DateTime.Now.AddDays(1); + entry.AbsoluteExpiration = DateTime.UtcNow.AddHours(_iconsSettings.CacheHours); - var httpClient = new HttpClient(); - var response = await httpClient.GetAsync(iconUrl); + var iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size=16..24..200"; + var response = await _httpClient.GetAsync(iconUrl); if(!response.IsSuccessStatusCode) { return null; @@ -61,10 +66,5 @@ namespace Bit.Icons.Controllers return new FileContentResult(icon.Image, icon.Format); } - - private string BuildIconUrl(Uri uri) - { - return $"{_iconsSettings.BestIconBaseUrl}/icon?url={uri.Host}&size=16..24..200"; - } } } diff --git a/src/Icons/IconsSettings.cs b/src/Icons/IconsSettings.cs index f550853bf..e4e6ee59f 100644 --- a/src/Icons/IconsSettings.cs +++ b/src/Icons/IconsSettings.cs @@ -3,5 +3,6 @@ public class IconsSettings { public virtual string BestIconBaseUrl { get; set; } + public virtual int CacheHours { get; set; } } } diff --git a/src/Icons/Services/DomainMappingService.cs b/src/Icons/Services/DomainMappingService.cs new file mode 100644 index 000000000..30e8271d6 --- /dev/null +++ b/src/Icons/Services/DomainMappingService.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace Bit.Icons.Services +{ + public class DomainMappingService : IDomainMappingService + { + private readonly Dictionary _map = new Dictionary + { + ["vault.bitwarden.com"] = "bitwarden.com", + ["accounts.google.com"] = "google.com", + // TODO: Add others here + }; + + public string MapDomain(string hostname) + { + if(_map.ContainsKey(hostname)) + { + return _map[hostname]; + } + + return hostname; + } + } +} diff --git a/src/Icons/Services/IDomainMappingService.cs b/src/Icons/Services/IDomainMappingService.cs new file mode 100644 index 000000000..194ee8f64 --- /dev/null +++ b/src/Icons/Services/IDomainMappingService.cs @@ -0,0 +1,7 @@ +namespace Bit.Icons.Services +{ + public interface IDomainMappingService + { + string MapDomain(string hostname); + } +} diff --git a/src/Icons/Startup.cs b/src/Icons/Startup.cs index 3746f2dc9..7d4e443cb 100644 --- a/src/Icons/Startup.cs +++ b/src/Icons/Startup.cs @@ -1,4 +1,5 @@ using System; +using Bit.Icons.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -20,6 +21,7 @@ namespace Bit.Icons services.AddOptions(); services.Configure(Configuration.GetSection("IconsSettings")); services.AddMemoryCache(); + services.AddSingleton(); services.AddMvc(); } diff --git a/src/Icons/appsettings.json b/src/Icons/appsettings.json index 8fe2f8c79..e65db766a 100644 --- a/src/Icons/appsettings.json +++ b/src/Icons/appsettings.json @@ -13,6 +13,7 @@ } }, "iconsSettings": { - "BestIconBaseUrl": "https://icons.better-idea.org" + "BestIconBaseUrl": "https://icons.better-idea.org", + "CacheHours": 24 } }