diff --git a/src/isnd/Controllers/HomeController.cs b/src/isnd/Controllers/HomeController.cs index a23cdc2..3f84696 100644 --- a/src/isnd/Controllers/HomeController.cs +++ b/src/isnd/Controllers/HomeController.cs @@ -8,34 +8,35 @@ using System.Linq; using isn.ViewModels; using Unleash.ClientFactory; using Unleash; +using isnd.Entities; +using System; +using System.Collections.Generic; +using isnd.Helpers; namespace isn.Controllers { public class HomeController : Controller { private readonly ILogger _logger; - readonly IHostingEnvironment _environment; - public SmtpSettings _smtpSettings { get; } //set only via Secret Manager - - private ApplicationDbContext _dbContext; + private readonly ApplicationDbContext _dbContext; + private readonly IUnleash _unleashĈlient; public HomeController( - IOptions smtpSettings, - IHostingEnvironment environment, ApplicationDbContext dbContext, + IOptions unleashClientSettings, + IHostingEnvironment env, ILogger logger) { - _environment = environment; _logger = logger; - _smtpSettings = smtpSettings.Value; _dbContext = dbContext; + _unleashĈlient = env.CreateUnleahClient(unleashClientSettings.Value); } public IActionResult Index() { return View(new HomeIndexViewModel{ PkgCount = _dbContext.Packages.Count(), - UnleashClient = Startup.UnleashĈlient + UnleashClient = _unleashĈlient }); } diff --git a/src/isnd/Controllers/PackagesController.cs b/src/isnd/Controllers/PackagesController.cs index a93df0c..8e19b0f 100644 --- a/src/isnd/Controllers/PackagesController.cs +++ b/src/isnd/Controllers/PackagesController.cs @@ -14,6 +14,9 @@ using Unleash.ClientFactory; using Unleash; using System.Collections.Generic; using isnd.Services; +using isnd.Entities; +using Microsoft.AspNetCore.Hosting; +using isnd.Helpers; namespace isn.Controllers { @@ -30,14 +33,17 @@ namespace isn.Controllers private readonly IDataProtector protector; private readonly NugetSettings nugetSettings; - ApplicationDbContext dbContext; - private PackageManager packageManager; + readonly ApplicationDbContext dbContext; + private readonly PackageManager packageManager; + private readonly IUnleash _unleashĈlient; public PackagesController( PackageManager packageManager, ILoggerFactory loggerFactory, IDataProtectionProvider provider, IOptions nugetOptions, + IOptions unleashClientSettings, + IHostingEnvironment env, ApplicationDbContext dbContext) { logger = loggerFactory.CreateLogger(); @@ -45,8 +51,8 @@ namespace isn.Controllers protector = provider.CreateProtector(nugetSettings.ProtectionTitle); this.dbContext = dbContext; this.packageManager = packageManager; - - ressources = packageManager.GetResources(Startup.UnleashĈlient).ToArray(); + _unleashĈlient = env.CreateUnleahClient(unleashClientSettings.Value); + ressources = packageManager.GetResources(_unleashĈlient).ToArray(); } // dotnet add . package -s http://localhost:5000/packages isn @@ -66,7 +72,7 @@ namespace isn.Controllers [HttpGet(_pkgRootPrefix + "/index.json")] public IActionResult Index( string q, - string semVerLevel = defaultSemVer, + string semVerLevel, bool prerelease = false, string packageType = null, int skip = 0, @@ -80,6 +86,10 @@ namespace isn.Controllers { ModelState.AddModelError("take", "Maximum exceeded"); } + if (semVerLevel != defaultSemVer) + { + ModelState.AddModelError("semVerLevel", defaultSemVer + " expected"); + } if (ModelState.IsValid) { return Ok(packageManager.SearchByName(q,skip,take,prerelease,packageType)); @@ -91,7 +101,7 @@ namespace isn.Controllers [HttpGet(_pkgRootPrefix + "/autocomplete")] public IActionResult AutoComplete( string id, - string semVerLevel = defaultSemVer, + string semVerLevel, bool prerelease = false, string packageType = null, int skip = 0, @@ -102,6 +112,10 @@ namespace isn.Controllers ModelState.AddModelError("take", "Maximum exceeded"); return BadRequest(ModelState); } + if (semVerLevel != defaultSemVer) + { + ModelState.AddModelError("semVerLevel", defaultSemVer + " expected"); + } return Ok(packageManager.AutoComplete(id,skip,take,prerelease,packageType)); } diff --git a/src/isnd/Helpers/UnleashHelpers.cs b/src/isnd/Helpers/UnleashHelpers.cs new file mode 100644 index 0000000..26af48f --- /dev/null +++ b/src/isnd/Helpers/UnleashHelpers.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using isnd.Entities; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Unleash; +using Unleash.ClientFactory; + +namespace isnd.Helpers +{ + public static class UnleashHelpers + { + + public static IUnleash CreateUnleahClient(this IHostingEnvironment env, + UnleashClientSettings unleashClientSettings) + { + var unleashSettings = new UnleashSettings + { + UnleashApi = new Uri(unleashClientSettings.ApiUrl), + AppName = "isnd", + Environment = env.EnvironmentName, + CustomHttpHeaders = new Dictionary + { + { "Authorization", unleashClientSettings.ClientApiKey } + } + }; + + UnleashClientFactory unleashClientFactory = new UnleashClientFactory(); + return unleashClientFactory.CreateClient(unleashSettings); + } + } +} \ No newline at end of file diff --git a/src/isnd/Startup.cs b/src/isnd/Startup.cs index 7990529..1a23bc6 100644 --- a/src/isnd/Startup.cs +++ b/src/isnd/Startup.cs @@ -83,7 +83,6 @@ namespace isn // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env, - IOptions unleashClientSettings, ApplicationDbContext dbContext) { if (env.IsDevelopment()) @@ -98,18 +97,7 @@ namespace isn dbContext.Database.Migrate(); } - var unleashSettings = new UnleashSettings - { - UnleashApi = new Uri(unleashClientSettings.Value.ApiUrl), - AppName = "isnd", - Environment = env.EnvironmentName, - CustomHttpHeaders = new Dictionary - { - { "Authorization", unleashClientSettings.Value.ClientApiKey } - } - }; - UnleashClientFactory unleashClientFactory = new UnleashClientFactory(); - UnleashĈlient = unleashClientFactory.CreateClient(unleashSettings); + app.UseStatusCodePages().UseStaticFiles().UseAuthentication().UseMvc(routes => {