Catalog
This commit is contained in:
@ -10,15 +10,11 @@ using Microsoft.Extensions.Options;
|
||||
using NuGet.Versioning;
|
||||
using isnd.Data;
|
||||
using isnd.Entities;
|
||||
using Unleash.ClientFactory;
|
||||
using Unleash;
|
||||
using System.Collections.Generic;
|
||||
using isnd.Services;
|
||||
using isnd.Entities;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using isnd.Helpers;
|
||||
using isnd.ViewModels;
|
||||
using System.Threading.Tasks;
|
||||
using isnd.Interfaces;
|
||||
|
||||
namespace isnd.Controllers
|
||||
{
|
||||
@ -36,7 +32,7 @@ namespace isnd.Controllers
|
||||
|
||||
private readonly IsndSettings _isndSettings;
|
||||
readonly ApplicationDbContext _dbContext;
|
||||
private readonly PackageManager _packageManager;
|
||||
private readonly IPackageManager _packageManager;
|
||||
private readonly IUnleash _unleashĈlient;
|
||||
|
||||
public PackagesController(
|
||||
@ -44,13 +40,14 @@ namespace isnd.Controllers
|
||||
IDataProtectionProvider provider,
|
||||
IOptions<IsndSettings> isndOptions,
|
||||
IUnleash unleashĈlient,
|
||||
ApplicationDbContext dbContext)
|
||||
ApplicationDbContext dbContext,
|
||||
IPackageManager pm)
|
||||
{
|
||||
_logger = loggerFactory.CreateLogger<PackagesController>();
|
||||
_isndSettings = isndOptions.Value;
|
||||
_protector = provider.CreateProtector(_isndSettings.ProtectionTitle);
|
||||
_dbContext = dbContext;
|
||||
_packageManager = new PackageManager(dbContext);
|
||||
_packageManager = pm;
|
||||
_unleashĈlient = unleashĈlient;
|
||||
_ressources = _packageManager.GetResources(_unleashĈlient).ToArray();
|
||||
}
|
||||
@ -104,33 +101,12 @@ namespace isnd.Controllers
|
||||
return Ok(_ressources);
|
||||
}
|
||||
|
||||
|
||||
[HttpGet(_pkgRootPrefix + "/index.json")]
|
||||
public IActionResult Index(
|
||||
string q,
|
||||
string semVerLevel,
|
||||
bool prerelease = false,
|
||||
string packageType = null,
|
||||
int skip = 0,
|
||||
int take = 25)
|
||||
//
|
||||
[HttpGet(_pkgRootPrefix)]
|
||||
public IActionResult Index()
|
||||
{
|
||||
if (string.IsNullOrEmpty(q))
|
||||
{
|
||||
ModelState.AddModelError("q", "no value");
|
||||
}
|
||||
if (take > maxTake)
|
||||
{
|
||||
ModelState.AddModelError("take", "Maximum exceeded");
|
||||
}
|
||||
if (semVerLevel != defaultSemVer)
|
||||
{
|
||||
_logger.LogWarning("Unexpected sementic version : "+semVerLevel);
|
||||
}
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
return Ok(_packageManager.SearchByName(q,skip,take,prerelease,packageType));
|
||||
}
|
||||
return BadRequest(new { error = ModelState });
|
||||
// https://docs.microsoft.com/en-us/nuget/api/catalog-resource#versioning
|
||||
return Ok(PackageManager.CurrentCatalogIndex);
|
||||
}
|
||||
|
||||
// GET /autocomplete?id=isn.protocol&prerelease=true
|
||||
|
@ -1,6 +1,6 @@
|
||||
namespace isnd.Controllers
|
||||
{
|
||||
internal class Resource
|
||||
public class Resource
|
||||
{
|
||||
public string id {get; set; }
|
||||
public string type {get; set; }
|
||||
|
@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using isnd.Data;
|
||||
using isnd.Data.ApiKeys;
|
||||
using isnd.Data.Catalog;
|
||||
|
||||
namespace isnd.Data
|
||||
{
|
||||
@ -28,5 +29,6 @@ namespace isnd.Data
|
||||
public DbSet<ApiKey> ApiKeys { get; set; }
|
||||
public DbSet<Package> Packages { get; set; }
|
||||
public DbSet<PackageVersion> PackageVersions { get; set; }
|
||||
public DbSet<Commit> Commits { get; set; }
|
||||
}
|
||||
}
|
||||
|
24
src/isnd/Data/Catalog/CatalogIndex.cs
Normal file
24
src/isnd/Data/Catalog/CatalogIndex.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace isnd.Data.Catalog
|
||||
{
|
||||
public class CatalogIndex : IObject
|
||||
{
|
||||
[JsonProperty("@id")]
|
||||
public string Id { get; set ; }
|
||||
|
||||
[JsonProperty("items")]
|
||||
public List<PageRef> Items { get; set; }
|
||||
|
||||
|
||||
[JsonProperty("count")]
|
||||
public int Count { get => Items.Count; }
|
||||
public string CommitId { get; set; }
|
||||
public DateTime CommitTimeStamp { get; set; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
32
src/isnd/Data/Catalog/Commit.cs
Normal file
32
src/isnd/Data/Catalog/Commit.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace isnd.Data.Catalog
|
||||
{
|
||||
public enum PackageAction
|
||||
{
|
||||
DeletePackage,
|
||||
PublishPackage
|
||||
}
|
||||
|
||||
public class Commit : IObject
|
||||
{
|
||||
[Key][Required,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public long Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public DateTime TimeStamp{ get; set; }
|
||||
|
||||
[Required]
|
||||
public PackageAction Action { get; set; }
|
||||
|
||||
[Required]
|
||||
public string PackageId { get; set; }
|
||||
[ForeignKey("PackageId")]
|
||||
|
||||
public virtual Package Package{ get; set; }
|
||||
public string CommitId { get => Id.ToString(); }
|
||||
public DateTime CommitTimeStamp { get => TimeStamp; }
|
||||
}
|
||||
}
|
14
src/isnd/Data/Catalog/IObject.cs
Normal file
14
src/isnd/Data/Catalog/IObject.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace isnd.Data.Catalog
|
||||
{
|
||||
public interface IObject
|
||||
{
|
||||
[JsonProperty("commitId")]
|
||||
string CommitId { get; }
|
||||
|
||||
[JsonProperty("commitTimeStamp")]
|
||||
DateTime CommitTimeStamp { get; }
|
||||
}
|
||||
}
|
34
src/isnd/Data/Catalog/PackageRef.cs
Normal file
34
src/isnd/Data/Catalog/PackageRef.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace isnd.Data.Catalog
|
||||
{
|
||||
/// <summary>
|
||||
/// An presence of package in a catalog,
|
||||
/// for availability, or deletion,
|
||||
///
|
||||
/// </summary>
|
||||
public class PackageRef : IObject
|
||||
{
|
||||
|
||||
|
||||
[JsonProperty("@id")]
|
||||
public string RefId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Reference type :
|
||||
/// nuget:PackageDetails vs nuget:PackageDelete
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
[JsonProperty("@type")]
|
||||
public string RefType { get; set; }
|
||||
|
||||
[JsonProperty("nuget:id")]
|
||||
public string Id { get; set; }
|
||||
|
||||
[JsonProperty("nuget:version")]
|
||||
public string Version { get; set; }
|
||||
public string CommitId { get; set; }
|
||||
public DateTime CommitTimeStamp { get; set; }
|
||||
}
|
||||
}
|
20
src/isnd/Data/Catalog/Page.cs
Normal file
20
src/isnd/Data/Catalog/Page.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace isnd.Data.Catalog
|
||||
{
|
||||
public class Page : IObject
|
||||
{
|
||||
[JsonProperty("@id")]
|
||||
public string Id { get; set; }
|
||||
|
||||
[JsonProperty("parent")]
|
||||
public string Parent { get; set; }
|
||||
|
||||
[JsonProperty("items")]
|
||||
public virtual List<PackageRef> Items { get; set; }
|
||||
public string CommitId { get; set; }
|
||||
public DateTime CommitTimeStamp { get; set; }
|
||||
}
|
||||
}
|
26
src/isnd/Data/Catalog/PageRef.cs
Normal file
26
src/isnd/Data/Catalog/PageRef.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace isnd.Data.Catalog
|
||||
{
|
||||
public class PageRef : IObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Page Url
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
[JsonProperty("@id")]
|
||||
public string Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Page entry count
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
[JsonProperty("count")]
|
||||
public int Count { get; set; }
|
||||
public string CommitId { get; set; }
|
||||
public DateTime CommitTimeStamp { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,11 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using isnd.Data.Catalog;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace isnd.Data
|
||||
{
|
||||
public class Package
|
||||
public class Package : IObject
|
||||
{
|
||||
[Key][Required]
|
||||
public string Id { get; set; }
|
||||
@ -17,11 +19,15 @@ namespace isnd.Data
|
||||
[StringLength(1024)]
|
||||
public string Description { get; set; }
|
||||
|
||||
public bool Public { get ; set;}
|
||||
|
||||
[JsonIgnore]
|
||||
virtual public ApplicationUser Owner { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
|
||||
public virtual List<PackageVersion> Versions { get; set; }
|
||||
public string CommitId { get; set; }
|
||||
public DateTime CommitTimeStamp { get; set; }
|
||||
}
|
||||
}
|
@ -2,8 +2,11 @@ namespace isnd.Entities
|
||||
{
|
||||
public class IsndSettings
|
||||
{
|
||||
public string ExternalUrl { get; set; }
|
||||
|
||||
public string ProtectionTitle {get; set;}
|
||||
public string PackagesRootDir {get; set;}
|
||||
public string CatalogDir {get; set;}
|
||||
public int MaxUserKeyCount {get; set;}
|
||||
|
||||
}
|
23
src/isnd/Interfaces/IPackageManager.cs
Normal file
23
src/isnd/Interfaces/IPackageManager.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
using isnd.Controllers;
|
||||
using isnd.Data.Catalog;
|
||||
using isnd.Services;
|
||||
using isnd.ViewModels;
|
||||
using NuGet.Versioning;
|
||||
using Unleash;
|
||||
|
||||
namespace isnd.Interfaces
|
||||
{
|
||||
public interface IPackageManager
|
||||
{
|
||||
AutoCompleteResult AutoComplete(string id, int skip, int take, bool prerelease = false, string packageType = null);
|
||||
Page CatalogPage();
|
||||
CatalogIndex GenerateCatalogIndex(string commitId);
|
||||
CatalogIndex GetCatalogIndex();
|
||||
string[] GetVersions(string id, NuGetVersion parsedVersion, bool prerelease = false, string packageType = null, int skip = 0, int take = 25);
|
||||
void PublishCatalog();
|
||||
PackageIndexViewModel SearchByName(string query, int skip, int take, bool prerelease = false, string packageType = null);
|
||||
IEnumerable<Resource> GetResources(IUnleash unleashĈlient);
|
||||
}
|
||||
|
||||
}
|
@ -1,23 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using isnd.Controllers;
|
||||
using isnd.Data;
|
||||
using isnd.Data.Catalog;
|
||||
using isnd.Entities;
|
||||
using isnd.Interfaces;
|
||||
using isnd.ViewModels;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NuGet.Versioning;
|
||||
using Unleash;
|
||||
|
||||
namespace isnd.Services
|
||||
{
|
||||
public class PackageManager
|
||||
|
||||
|
||||
public class PackageManager : IPackageManager
|
||||
{
|
||||
ApplicationDbContext dbContext;
|
||||
public PackageManager(ApplicationDbContext dbContext)
|
||||
public PackageManager(ApplicationDbContext dbContext, IOptions<IsndSettings> pmConfigOptions)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
CurrentCatalogIndex = GetCatalogIndex();
|
||||
this.pmConfigOptions = pmConfigOptions.Value;
|
||||
}
|
||||
public PackageIndexViewModel SearchByName(string query,
|
||||
int skip, int take,bool prerelease = false,
|
||||
|
||||
|
||||
public PackageIndexViewModel SearchByName(string query,
|
||||
int skip, int take, bool prerelease = false,
|
||||
string packageType = null)
|
||||
{
|
||||
|
||||
@ -32,14 +43,63 @@ namespace isnd.Services
|
||||
var pkgs = scope.Skip(skip).Take(take).ToArray();
|
||||
|
||||
return new PackageIndexViewModel
|
||||
{
|
||||
query = query,
|
||||
totalHits = total,
|
||||
data = pkgs
|
||||
};
|
||||
{
|
||||
query = query,
|
||||
totalHits = total,
|
||||
data = pkgs
|
||||
};
|
||||
}
|
||||
public AutoCompleteResult AutoComplete (string id,
|
||||
int skip, int take, bool prerelease = false,
|
||||
const int maxPageLen = 512;
|
||||
|
||||
public CatalogIndex GenerateCatalogIndex(string commitId)
|
||||
{
|
||||
|
||||
var root = "/index.json";
|
||||
|
||||
|
||||
var catalog = new CatalogIndex
|
||||
{
|
||||
CommitId = commitId,
|
||||
CommitTimeStamp = DateTime.Now
|
||||
};
|
||||
var scope = dbContext.Packages.Where(p => p.Public)
|
||||
.OrderBy(p => p.CommitTimeStamp);
|
||||
catalog.Items = new List<PageRef>();
|
||||
|
||||
int pagecount = (int)(scope.Count() / maxPageLen);
|
||||
|
||||
for (int pagelen = 0, pagenum = 0; pagelen < pagecount; pagenum++)
|
||||
{
|
||||
Page p = new Page
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
dbContext.Packages.Where(p => p.Public)
|
||||
.OrderBy(p => p.CommitTimeStamp);
|
||||
return catalog;
|
||||
|
||||
}
|
||||
public Page CatalogPage()
|
||||
{
|
||||
var scope = dbContext.Packages
|
||||
.Where(P => P.Versions.Count > 0)
|
||||
.Select(
|
||||
p => new PackageRef { Id = p.Id, Version = p.Versions.Max().FullString });
|
||||
|
||||
return new Page
|
||||
{
|
||||
Items = scope.ToList()
|
||||
};
|
||||
}
|
||||
public void PublishCatalog()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public AutoCompleteResult AutoComplete(string id,
|
||||
int skip, int take, bool prerelease = false,
|
||||
string packageType = null)
|
||||
{
|
||||
var scope = dbContext.PackageVersions.Where(
|
||||
@ -49,11 +109,11 @@ namespace isnd.Services
|
||||
)
|
||||
.OrderBy(v => v.FullString);
|
||||
return new AutoCompleteResult
|
||||
{
|
||||
totalHits = scope.Count(),
|
||||
data = scope.Select(v => v.FullString)
|
||||
{
|
||||
totalHits = scope.Count(),
|
||||
data = scope.Select(v => v.FullString)
|
||||
.Skip(skip).Take(take).ToArray()
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
// TODO stocker MetaData plutôt que FullString en base,
|
||||
@ -77,11 +137,28 @@ namespace isnd.Services
|
||||
.Skip(skip).Take(take).ToArray();
|
||||
}
|
||||
|
||||
protected static bool CamelCaseMatch(string id, string q)
|
||||
public static CatalogIndex CurrentCatalogIndex { get; protected set; }
|
||||
|
||||
private IsndSettings pmConfigOptions;
|
||||
|
||||
public virtual CatalogIndex GetCatalogIndex()
|
||||
{
|
||||
if (CurrentCatalogIndex == null)
|
||||
{
|
||||
LoadCatalogFromDb();
|
||||
}
|
||||
return CurrentCatalogIndex;
|
||||
}
|
||||
void LoadCatalogFromDb()
|
||||
{
|
||||
dbContext.Commits.OrderBy(c => c.TimeStamp);
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected static bool CamelCaseMatch(string id, string query)
|
||||
{
|
||||
// Assert.False (q==null);
|
||||
string query = q;
|
||||
if (query.Length == 0) return false;
|
||||
if (string.IsNullOrEmpty(query)) return true;
|
||||
|
||||
while (id.Length > 0)
|
||||
{
|
||||
@ -89,7 +166,7 @@ namespace isnd.Services
|
||||
while (id.Length > i && char.IsLower(id[i])) i++;
|
||||
if (i == 0) break;
|
||||
id = id.Substring(i);
|
||||
if (id.StartsWith(q, System.StringComparison.OrdinalIgnoreCase)) return true;
|
||||
if (id.StartsWith(query, System.StringComparison.OrdinalIgnoreCase)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -102,7 +179,7 @@ namespace isnd.Services
|
||||
return false;
|
||||
}
|
||||
|
||||
internal List<Resource> GetResources(IUnleash unleashClient)
|
||||
public IEnumerable<Resource> GetResources(IUnleash unleashClient)
|
||||
{
|
||||
var res = new List<Resource>();
|
||||
if (unleashClient.IsEnabled("pkg-push"))
|
||||
@ -125,7 +202,7 @@ namespace isnd.Services
|
||||
res.Add(
|
||||
new Resource
|
||||
{
|
||||
id = "package/index.json",
|
||||
id = "package",
|
||||
type = "SearchAutocompleteService/3.5.0",
|
||||
comment = "Auto complete service"
|
||||
});
|
||||
@ -133,7 +210,7 @@ namespace isnd.Services
|
||||
res.Add(
|
||||
new Resource
|
||||
{
|
||||
id = "package/index.json",
|
||||
id = "package",
|
||||
type = "SearchQueryService/3.5.0",
|
||||
comment = "Search Query service"
|
||||
});
|
||||
|
@ -71,10 +71,12 @@ namespace isnd
|
||||
var config = s.GetRequiredService<IOptions<UnleashClientSettings>>();
|
||||
return s.GetRequiredService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>().CreateUnleahClient(config.Value);
|
||||
});
|
||||
services.AddSingleton<IPackageManager, PackageManager>();
|
||||
|
||||
// _unleashĈlient = env.CreateUnleahClient(unleashClientSettings.Value);
|
||||
var smtpSettingsconf = Configuration.GetSection("Smtp");
|
||||
services.Configure<SmtpSettings>(smtpSettingsconf);
|
||||
var isndSettingsconf = Configuration.GetSection("Nuget");
|
||||
var isndSettingsconf = Configuration.GetSection("Isn");
|
||||
services.Configure<IsndSettings>(isndSettingsconf);
|
||||
var adminStartupListConf = Configuration.GetSection("AdminList");
|
||||
services.Configure<AdminStartupList>(adminStartupListConf);
|
||||
@ -85,6 +87,7 @@ namespace isnd
|
||||
|
||||
|
||||
public static IUnleash UnleashĈlient { get; private set; }
|
||||
public static string ExternalAddress { get; internal set; }
|
||||
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
@ -4,7 +4,8 @@
|
||||
"paul@pschneider.fr"
|
||||
]
|
||||
},
|
||||
"Nuget": {
|
||||
"Isn": {
|
||||
"ExternalUrl": "http://localhost:5000",
|
||||
"PackagesRootDir" : "packages",
|
||||
"ProtectionTitle": "protected-data-v1",
|
||||
"MaxUserKeyCount": 5
|
||||
|
@ -4,7 +4,8 @@
|
||||
"happy-new-root"
|
||||
]
|
||||
},
|
||||
"Nuget": {
|
||||
"Isn": {
|
||||
"ExternalUrl": "<lame-server>",
|
||||
"PackagesRootDir" : "<your-Source-dir>",
|
||||
"ProtectionTitle": "protected-data-v1",
|
||||
"MaxUserKeyCount": 1
|
||||
|
Reference in New Issue
Block a user