gonna do some fixes for routing and other stuff

This commit is contained in:
2022-01-08 00:24:04 +01:00
parent 3435cfac1d
commit 8e9b0872d6
18 changed files with 93 additions and 131 deletions
+2 -23
View File
@@ -1,30 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
namespace zero.Api;
namespace zero.Api;
public static class ZeroBuilderExtensions
{
public static ZeroBuilder AddApi<T>(this ZeroBuilder builder) where T : ZeroApiPlugin, IZeroPlugin, new()
{
return builder.AddApi<T>(_ => { });
}
public static ZeroBuilder AddApi(this ZeroBuilder builder)
{
return builder.AddApi<ZeroApiPlugin>();
}
public static ZeroBuilder AddApi<T>(this ZeroBuilder builder, Action<ApiOptions> options) where T : ZeroApiPlugin, IZeroPlugin, new()
{
return builder.AddPlugin(services =>
{
T plugin = new();
plugin.PostConfigureServices = (services, configuration) =>
{
services.Configure<ApiOptions>(opts => options(opts));
};
return plugin;
});
return builder.AddPlugin<ZeroApiPlugin>();
}
}
+1 -20
View File
@@ -4,27 +4,8 @@ namespace zero.Backoffice;
public static class ServiceCollectionExtensions
{
public static ZeroBuilder AddBackoffice<T>(this ZeroBuilder builder) where T : ZeroBackofficePlugin, IZeroPlugin, new()
{
return builder.AddBackoffice<T>(_ => { });
}
public static ZeroBuilder AddBackoffice(this ZeroBuilder builder)
{
return builder.AddBackoffice<ZeroBackofficePlugin>();
}
public static ZeroBuilder AddBackoffice<T>(this ZeroBuilder builder, Action<BackofficeOptions> options) where T : ZeroBackofficePlugin, IZeroPlugin, new()
{
return builder.AddPlugin(services =>
{
T plugin = new();
plugin.PostConfigureServices = (services, configuration) =>
{
services.Configure<BackofficeOptions>(opts => options(opts));
};
return plugin;
});
return builder.AddPlugin<ZeroBackofficePlugin>();
}
}
@@ -1,48 +0,0 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace zero.Architecture;
internal class ZeroPluginInitializer
{
/// <summary>
/// Adds a zero plugin
/// </summary>
public static void AddPlugin<T>(IServiceCollection services, IConfiguration configuration) where T : class, IZeroPlugin, new()
{
AssemblyDiscovery.Current.AddAssembly(typeof(T).Assembly);
services.AddSingleton<IZeroPlugin, T>();
AddPluginServices<T>(services, configuration);
}
/// <summary>
/// Adds a zero plugin
/// </summary>
public static void AddPlugin<T>(IServiceCollection services, IConfiguration configuration, Func<IServiceProvider, T> implementationFactory) where T : class, IZeroPlugin, new()
{
AssemblyDiscovery.Current.AddAssembly(typeof(T).Assembly);
services.AddSingleton<IZeroPlugin, T>(implementationFactory);
AddPluginServices<T>(services, configuration);
}
/// <summary>
/// Creates a temporary instance of the plugin to add additional services
/// </summary>
/// <typeparam name="T"></typeparam>
static void AddPluginServices<T>(IServiceCollection services, IConfiguration configuration) where T : class, IZeroPlugin, new()
{
try
{
T plugin = new();
plugin.ConfigureServices(services, configuration);
//services.Configure<ZeroOptions>(opts => plugin.Configure(opts));
}
catch
{
throw new Exception($"Plugin \"{nameof(T)}\" needs an additional parameterless constructor as ConfigureServices() is called before the DI container is built");
}
}
}
+5 -6
View File
@@ -1,6 +1,6 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System.IO;
@@ -63,7 +63,7 @@ public class MediaCreator : IMediaCreator
if (isImage)
{
using Image<Rgba32> image = await Image.LoadAsync<Rgba32>(fileInfo.AbsolutePath, cancellationToken);
using Image<Rgba32> image = await Image.LoadAsync<Rgba32>(fileInfo.AbsolutePath);
model.ImageMeta = GetImageMetadata(image);
string extension = Path.GetExtension(model.Path);
@@ -74,15 +74,14 @@ public class MediaCreator : IMediaCreator
imageFrame.Mutate(x => x.Resize(opts));
using MemoryStream stream = new();
await imageFrame.SaveAsync(stream, new WebpEncoder()
await imageFrame.SaveAsync(stream, new JpegEncoder()
{
FilterStrength = 30,
Quality = 50
Quality = 80
}, cancellationToken);
stream.Position = 0;
string thumbFilename = normalizedFilename.TrimEnd(extension) + "." + Safenames.File(key) + ".webp";
string thumbFilename = normalizedFilename.TrimEnd(extension) + "." + Safenames.File(key) + ".jpg";
string path = directory + '/' + thumbFilename;
await FileSystem.CreateFile(path, stream, cancellationToken: cancellationToken);
@@ -8,4 +8,4 @@ public interface IRouteModel
public class RouteModel : IRouteModel
{
public Route Route { get; set; }
}
}
+8 -16
View File
@@ -14,15 +14,19 @@ public class ZeroApplicationBuilder : IZeroApplicationBuilder
App = app;
App.UseStaticFiles();
//App.UseExceptionHandler("/zero/api/error");
// TODO
// UseRouting() + UseMiddleware() works for Backoffice (Frontend can't resolve route as context not available yet)
// UseMiddleware() + UseRouting() works for Frontend (Backoffice can't resolve context and falls back to the shared context...)
App.UseMiddleware<ZeroContextMiddleware>();
App.UseRouting();
App.UseAuthentication();
App.UseAuthorization();
ZeroBuilder.Modules.Configure(app, null, app.ApplicationServices);
foreach (IZeroPlugin plugin in ZeroBuilder.Plugins)
{
plugin.Configure(app, null, app.ApplicationServices);
}
//ZeroModuleInitializer.ConfigureAll(app.ApplicationServices.GetRequiredService<IZeroOptions>());
}
@@ -50,16 +54,4 @@ public interface IZeroApplicationBuilder
void WithEndpoints(Action<IZeroEndpointRouteBuilder> endpoints);
IZeroApplicationBuilder WithMiddleware(Action<IApplicationBuilder> configure);
}
public class ZeroApplicationBuilderContext
{
}
public class ZeroEndpointBuilderContext
{
}
+8 -12
View File
@@ -13,11 +13,13 @@ public class ZeroBuilder
public virtual IMvcBuilder Mvc { get; }
protected ZeroModuleCollection Modules { get; } = new();
internal static ZeroModuleCollection Modules { get; private set; } = new();
readonly IConfiguration Configuration;
readonly IZeroStartupOptions StartupOptions;
internal static HashSet<IZeroPlugin> Plugins { get; private set; } = new();
public ZeroBuilder(IServiceCollection services, IConfiguration configuration, Action<IZeroStartupOptions> setupAction)
{
@@ -76,17 +78,11 @@ public class ZeroBuilder
/// </summary>
public ZeroBuilder AddPlugin<T>() where T : class, IZeroPlugin, new()
{
ZeroPluginInitializer.AddPlugin<T>(Services, Configuration);
return this;
}
/// <summary>
/// Adds a zero plugin
/// </summary>
public ZeroBuilder AddPlugin<T>(Func<IServiceProvider, T> implementationFactory) where T : class, IZeroPlugin, new()
{
ZeroPluginInitializer.AddPlugin<T>(Services, Configuration, implementationFactory);
T plugin = new T();
plugin.ConfigureServices(Services, Configuration);
AssemblyDiscovery.Current.AddAssembly(typeof(T).Assembly);
Services.AddSingleton<IZeroPlugin, T>();
Plugins.Add(plugin);
return this;
}
}
+1 -1
View File
@@ -25,7 +25,7 @@
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="RavenDB.Client" Version="5.3.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.0.0-alpha.0.143" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.4" />
<PackageReference Include="System.Linq.Async" Version="5.1.0" />
</ItemGroup>
+37 -4
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using zero.Extensions;
using zero.Media;
using zero.Persistence;
namespace zero.Web.ViewHelpers;
@@ -13,6 +14,8 @@ public class ZeroMediaHelper : IZeroMediaHelper
public IZeroMediaHelper Core { get; private set; }
protected IMediaManagement MediaManagement { get; private set; }
protected bool Global { get; set; }
/// <summary>
@@ -21,13 +24,14 @@ public class ZeroMediaHelper : IZeroMediaHelper
ConcurrentDictionary<string, Media.Media> Cache { get; set; } = new();
public ZeroMediaHelper(IZeroStore store, bool global = false)
public ZeroMediaHelper(IZeroStore store, IMediaManagement mediaManagement, bool global = false)
{
Store = store;
MediaManagement = mediaManagement;
if (!global)
{
Core = new ZeroMediaHelper(store, true);
Core = new ZeroMediaHelper(store, MediaManagement, true);
}
}
@@ -87,15 +91,44 @@ public class ZeroMediaHelper : IZeroMediaHelper
public async Task<string> GetUrl(string id, bool isAbsolute = false)
{
Media.Media media = await GetById(id);
return media?.Path.TrimStart("url://");
if (media == null)
{
return null;
}
if (media.Path.StartsWith("url://"))
{
return media.Path.TrimStart("url://");
}
return MediaManagement.GetPublicFilePath(media);
}
/// <inheritdoc />
public async Task<Dictionary<string, string>> GetUrls(string[] ids, bool isAbsolute = false)
{
Dictionary<string, string> result = new();
Dictionary<string, Media.Media> medias = await GetByIds(ids);
return medias.ToDictionary(x => x.Key, x => x.Value?.Path.TrimStart("url://"));
foreach ((string id, Media.Media media) in medias)
{
if (media == null)
{
result.Add(id, null);
}
else if (media.Path.StartsWith("url://"))
{
result.Add(id, media.Path.TrimStart("url://"));
}
else
{
result.Add(id, MediaManagement.GetPublicFilePath(media));
}
}
return result;
}
}
+30
View File
@@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.DependencyInjection;
using zero.Applications;
using zero.Context;
using zero.Routing;
namespace zero.Web;
public abstract class ZeroPageModel<T> : ZeroPageModel where T : class, IRouteModel
{
/// <summary>
/// Resolved route
/// </summary>
public virtual T Route => _route ?? (_route = HttpContext.Features.Get<IRouteModel>() as T);
T _route;
}
public abstract class ZeroPageModel : PageModel
{
/// <summary>
/// Get acces to the zero context for this request
/// </summary>
public IZeroContext ZeroContext => _zeroContext ?? (_zeroContext = HttpContext?.RequestServices?.GetService<IZeroContext>());
IZeroContext _zeroContext;
/// <summary>
/// Resolved application
/// </summary>
public virtual Application Application => ZeroContext?.Application;
}