Unleash client init

broken/ef
Paul Schneider 3 years ago
parent d33a19d35f
commit 355b2d4c7c
4 changed files with 63 additions and 28 deletions

@ -8,34 +8,35 @@ using System.Linq;
using isn.ViewModels; using isn.ViewModels;
using Unleash.ClientFactory; using Unleash.ClientFactory;
using Unleash; using Unleash;
using isnd.Entities;
using System;
using System.Collections.Generic;
using isnd.Helpers;
namespace isn.Controllers namespace isn.Controllers
{ {
public class HomeController : Controller public class HomeController : Controller
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
readonly IHostingEnvironment _environment; private readonly ApplicationDbContext _dbContext;
public SmtpSettings _smtpSettings { get; } //set only via Secret Manager private readonly IUnleash _unleashĈlient;
private ApplicationDbContext _dbContext;
public HomeController( public HomeController(
IOptions<SmtpSettings> smtpSettings,
IHostingEnvironment environment,
ApplicationDbContext dbContext, ApplicationDbContext dbContext,
IOptions<UnleashClientSettings> unleashClientSettings,
IHostingEnvironment env,
ILogger<HomeController> logger) ILogger<HomeController> logger)
{ {
_environment = environment;
_logger = logger; _logger = logger;
_smtpSettings = smtpSettings.Value;
_dbContext = dbContext; _dbContext = dbContext;
_unleashĈlient = env.CreateUnleahClient(unleashClientSettings.Value);
} }
public IActionResult Index() public IActionResult Index()
{ {
return View(new HomeIndexViewModel{ return View(new HomeIndexViewModel{
PkgCount = _dbContext.Packages.Count(), PkgCount = _dbContext.Packages.Count(),
UnleashClient = Startup.UnleashĈlient UnleashClient = _unleashĈlient
}); });
} }

@ -14,6 +14,9 @@ using Unleash.ClientFactory;
using Unleash; using Unleash;
using System.Collections.Generic; using System.Collections.Generic;
using isnd.Services; using isnd.Services;
using isnd.Entities;
using Microsoft.AspNetCore.Hosting;
using isnd.Helpers;
namespace isn.Controllers namespace isn.Controllers
{ {
@ -30,14 +33,17 @@ namespace isn.Controllers
private readonly IDataProtector protector; private readonly IDataProtector protector;
private readonly NugetSettings nugetSettings; private readonly NugetSettings nugetSettings;
ApplicationDbContext dbContext; readonly ApplicationDbContext dbContext;
private PackageManager packageManager; private readonly PackageManager packageManager;
private readonly IUnleash _unleashĈlient;
public PackagesController( public PackagesController(
PackageManager packageManager, PackageManager packageManager,
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
IDataProtectionProvider provider, IDataProtectionProvider provider,
IOptions<NugetSettings> nugetOptions, IOptions<NugetSettings> nugetOptions,
IOptions<UnleashClientSettings> unleashClientSettings,
IHostingEnvironment env,
ApplicationDbContext dbContext) ApplicationDbContext dbContext)
{ {
logger = loggerFactory.CreateLogger<PackagesController>(); logger = loggerFactory.CreateLogger<PackagesController>();
@ -45,8 +51,8 @@ namespace isn.Controllers
protector = provider.CreateProtector(nugetSettings.ProtectionTitle); protector = provider.CreateProtector(nugetSettings.ProtectionTitle);
this.dbContext = dbContext; this.dbContext = dbContext;
this.packageManager = packageManager; this.packageManager = packageManager;
_unleashĈlient = env.CreateUnleahClient(unleashClientSettings.Value);
ressources = packageManager.GetResources(Startup.UnleashĈlient).ToArray(); ressources = packageManager.GetResources(_unleashĈlient).ToArray();
} }
// dotnet add . package -s http://localhost:5000/packages isn // dotnet add . package -s http://localhost:5000/packages isn
@ -66,7 +72,7 @@ namespace isn.Controllers
[HttpGet(_pkgRootPrefix + "/index.json")] [HttpGet(_pkgRootPrefix + "/index.json")]
public IActionResult Index( public IActionResult Index(
string q, string q,
string semVerLevel = defaultSemVer, string semVerLevel,
bool prerelease = false, bool prerelease = false,
string packageType = null, string packageType = null,
int skip = 0, int skip = 0,
@ -80,6 +86,10 @@ namespace isn.Controllers
{ {
ModelState.AddModelError("take", "Maximum exceeded"); ModelState.AddModelError("take", "Maximum exceeded");
} }
if (semVerLevel != defaultSemVer)
{
ModelState.AddModelError("semVerLevel", defaultSemVer + " expected");
}
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
return Ok(packageManager.SearchByName(q,skip,take,prerelease,packageType)); return Ok(packageManager.SearchByName(q,skip,take,prerelease,packageType));
@ -91,7 +101,7 @@ namespace isn.Controllers
[HttpGet(_pkgRootPrefix + "/autocomplete")] [HttpGet(_pkgRootPrefix + "/autocomplete")]
public IActionResult AutoComplete( public IActionResult AutoComplete(
string id, string id,
string semVerLevel = defaultSemVer, string semVerLevel,
bool prerelease = false, bool prerelease = false,
string packageType = null, string packageType = null,
int skip = 0, int skip = 0,
@ -102,6 +112,10 @@ namespace isn.Controllers
ModelState.AddModelError("take", "Maximum exceeded"); ModelState.AddModelError("take", "Maximum exceeded");
return BadRequest(ModelState); return BadRequest(ModelState);
} }
if (semVerLevel != defaultSemVer)
{
ModelState.AddModelError("semVerLevel", defaultSemVer + " expected");
}
return Ok(packageManager.AutoComplete(id,skip,take,prerelease,packageType)); return Ok(packageManager.AutoComplete(id,skip,take,prerelease,packageType));
} }

@ -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<string, string>
{
{ "Authorization", unleashClientSettings.ClientApiKey }
}
};
UnleashClientFactory unleashClientFactory = new UnleashClientFactory();
return unleashClientFactory.CreateClient(unleashSettings);
}
}
}

@ -83,7 +83,6 @@ namespace isn
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, public void Configure(IApplicationBuilder app,
Microsoft.AspNetCore.Hosting.IHostingEnvironment env, Microsoft.AspNetCore.Hosting.IHostingEnvironment env,
IOptions<UnleashClientSettings> unleashClientSettings,
ApplicationDbContext dbContext) ApplicationDbContext dbContext)
{ {
if (env.IsDevelopment()) if (env.IsDevelopment())
@ -98,18 +97,7 @@ namespace isn
dbContext.Database.Migrate(); dbContext.Database.Migrate();
} }
var unleashSettings = new UnleashSettings
{
UnleashApi = new Uri(unleashClientSettings.Value.ApiUrl),
AppName = "isnd",
Environment = env.EnvironmentName,
CustomHttpHeaders = new Dictionary<string, string>
{
{ "Authorization", unleashClientSettings.Value.ClientApiKey }
}
};
UnleashClientFactory unleashClientFactory = new UnleashClientFactory();
UnleashĈlient = unleashClientFactory.CreateClient(unleashSettings);
app.UseStatusCodePages().UseStaticFiles().UseAuthentication().UseMvc(routes => app.UseStatusCodePages().UseStaticFiles().UseAuthentication().UseMvc(routes =>
{ {

Loading…