refactorisation de la configuration

broken/ef
Paul Schneider 3 years ago
parent 838de379fd
commit 749eb645d5
7 changed files with 43 additions and 35 deletions

@ -13,17 +13,16 @@ namespace nuget_host.Controllers
{
private readonly ILogger _logger;
readonly IHostingEnvironment _environment;
public IDataProtector DataProtector { get; }
public SmtpSettings Options { get; } //set only via Secret Manager
public SmtpSettings _smtpSettings { get; } //set only via Secret Manager
public HomeController(
IOptions<SmtpSettings> smtpSettings,
IDataProtectionProvider provider, Microsoft.AspNetCore.Hosting.IHostingEnvironment environment, ILogger<HomeController> logger)
IHostingEnvironment environment,
ILogger<HomeController> logger)
{
_environment = environment;
_logger = logger;
Options = smtpSettings.Value;
DataProtector = provider.CreateProtector(Options.ProtectionTitle);
_smtpSettings = smtpSettings.Value;
}
public IActionResult Index()

@ -7,7 +7,9 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NuGet.Packaging;
using nuget_host.Entities;
namespace nuget_host.Controllers
{
@ -16,10 +18,16 @@ namespace nuget_host.Controllers
private readonly ILogger<PackagesController> logger;
private readonly IDataProtector protector;
public PackagesController(ILoggerFactory loggerFactory, IDataProtectionProvider provider)
private readonly NugetSettings nugetSettings;
public PackagesController(
ILoggerFactory loggerFactory,
IDataProtectionProvider provider,
IOptions<NugetSettings> nugetOptions)
{
logger = loggerFactory.CreateLogger<PackagesController>();
protector = provider.CreateProtector("Packages.v1");
nugetSettings = nugetOptions.Value;
protector = provider.CreateProtector(nugetSettings.ProtectionTitle);
}
[HttpPut("packages/{*spec}")]
@ -61,7 +69,7 @@ namespace nuget_host.Controllers
var version = reader.GetVersion();
path = Path.Combine(Startup.SourceDir,
path = Path.Combine(nugetSettings.PackagesRootDir,
Path.Combine(pkgid,
Path.Combine(version.ToFullString(),
$"{pkgid}-{version}.nupkg")));
@ -85,12 +93,7 @@ namespace nuget_host.Controllers
}
else
{
ViewData["spec"] = spec;
// TODO Assert valid sem ver spec
var filelst = new DirectoryInfo(Startup.SourceDir);
var fi = new FileInfo(spec);
var lst = filelst.GetFiles(fi.Name + "*.nupkg");
ViewData["lst"] = lst.Select(entry => entry.Name);
return BadRequest();
}
return Ok(ViewData);
}
@ -101,18 +104,12 @@ namespace nuget_host.Controllers
if (string.IsNullOrEmpty(spec))
{
ViewData["warn"] = "no spec";
/*
usr/lib/mono/msbuild/Current/bin/NuGet.targets(128,5): error : Failed to retrieve information about 'Microsoft.VisualStudio.Web.CodeGeneration.Tools' from remote source 'http://localhost:5000/Packages/FindPackagesById()?id='Microsoft.VisualStudio.Web.CodeGeneration.Tools'&semVerLevel=2.0.0'. [/home/paul/workspace/nuget-host/nuget-host.csproj]
*/
}
else
{
ViewData["spec"] = spec;
// TODO Assert valid sem ver spec
var filelst = new DirectoryInfo(Startup.SourceDir);
var filelst = new DirectoryInfo(nugetSettings.PackagesRootDir);
var fi = new FileInfo(spec);
var lst = filelst.GetDirectories(spec);
ViewData["lst"] = lst.Select(entry => entry.Name);
@ -120,6 +117,8 @@ namespace nuget_host.Controllers
return Ok(ViewData);
}
[Authorize]
[HttpGet("api/get-key/{*apikey}")]
public IActionResult GetApiKey(string apiKey)

@ -0,0 +1,9 @@
namespace nuget_host.Entities
{
public class NugetSettings
{
public string ProtectionTitle {get; set;}
public string PackagesRootDir {get; set;}
}
}

@ -8,6 +8,5 @@ namespace nuget_host.Entities
public string SenderEMail {get; set;}
public string UserName {get; set;}
public string Password {get; set;}
public string ProtectionTitle {get; set;}
}
}

@ -29,8 +29,6 @@ namespace nuget_host
}
public IConfiguration Configuration { get; }
public static string ExternalUrl { get; private set; }
public static string SourceDir { get; private set; }
public static string RootApiKeySecret { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
@ -55,6 +53,8 @@ namespace nuget_host
var smtpSettingsconf = Configuration.GetSection("SmtpSettings");
services.Configure<SmtpSettings>(smtpSettingsconf);
var nugetSettingsconf = Configuration.GetSection("NugetSettings");
services.Configure<NugetSettings>(nugetSettingsconf);
}
@ -71,9 +71,6 @@ namespace nuget_host
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
ExternalUrl = Configuration["NuGet:ExternalUrl"];
SourceDir = Configuration["NuGet:SourceDir"];
RootApiKeySecret = Configuration["RootApiKeySecret"];
app.UseStaticFiles();

@ -1,7 +1,12 @@
{
"NuGet": {
"ExternalUrl" : "<http://localhost:5000/packages",
"SourceDir" : "packages"
"PackagesRootDir" : "packages"
},
"RootApiKeySecret": "secret-key"
"Smtp": {
"Server": "localhost",
"Port": 25,
"SenderName": "Paul Schneider",
"SenderEmail": "paul@pschneider.fr"
}
}

@ -2,18 +2,18 @@
"RootApiKeySecret": "<your-root-api-clear-key>",
"NuGet": {
"ExternalUrl" : "<http://your-external.url",
"SourceDir" : "<your-Source-dir>"
"PackagesRootDir" : "<your-Source-dir>",
"ProtectionTitle": "protected-data-v1"
},
"ConnectionStrings": {
"DefaultConnection": "Server=<pqserver>;Port=<pqport>;Database=<dbname>;Username=<dbusername>;Password=<dbpass>;"
},
"AllowedHosts": "*",
"SmtpSettings": {
"Server": "localhost",
"Smtp": {
"Server": "<smtp.server.address>",
"Port": 25,
"SenderName": "Paul Schneider",
"SenderEmail": "paul@pschneider.fr",
"ProtectionTitle": "protected-data-v1"
"SenderName": "<from-name>",
"SenderEmail": "<from-email>"
},
"Logging": {
"LogLevel": {

Loading…