Unleash client init
This commit is contained in:
@ -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> smtpSettings,
|
||||
IHostingEnvironment environment,
|
||||
ApplicationDbContext dbContext,
|
||||
IOptions<UnleashClientSettings> unleashClientSettings,
|
||||
IHostingEnvironment env,
|
||||
ILogger<HomeController> 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
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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<NugetSettings> nugetOptions,
|
||||
IOptions<UnleashClientSettings> unleashClientSettings,
|
||||
IHostingEnvironment env,
|
||||
ApplicationDbContext dbContext)
|
||||
{
|
||||
logger = loggerFactory.CreateLogger<PackagesController>();
|
||||
@ -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));
|
||||
}
|
||||
|
32
src/isnd/Helpers/UnleashHelpers.cs
Normal file
32
src/isnd/Helpers/UnleashHelpers.cs
Normal file
@ -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.
|
||||
public void Configure(IApplicationBuilder app,
|
||||
Microsoft.AspNetCore.Hosting.IHostingEnvironment env,
|
||||
IOptions<UnleashClientSettings> 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<string, string>
|
||||
{
|
||||
{ "Authorization", unleashClientSettings.Value.ClientApiKey }
|
||||
}
|
||||
};
|
||||
UnleashClientFactory unleashClientFactory = new UnleashClientFactory();
|
||||
UnleashĈlient = unleashClientFactory.CreateClient(unleashSettings);
|
||||
|
||||
|
||||
app.UseStatusCodePages().UseStaticFiles().UseAuthentication().UseMvc(routes =>
|
||||
{
|
||||
|
Reference in New Issue
Block a user