-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using DayFlags.Core.Models; | ||
|
||
namespace DayFlags.Core.EntryTypes; | ||
|
||
/// <summary> | ||
/// Provides <see cref="EntryType"/>s | ||
/// </summary> | ||
public abstract class AEntryTypeProvider | ||
{ | ||
/// <summary> | ||
/// Provides all EntryTypes that this Provider knows | ||
/// </summary> | ||
/// <returns>List of EntryTypes</returns> | ||
public abstract Task<List<EntryType>> GetEntryTypesAsync(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using DayFlags.Core.EntryTypes; | ||
using DayFlags.Core.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace DayFlags.Server.EntryTypes; | ||
|
||
/// <summary> | ||
/// A <see cref="AEntryTypeProvider"/> that fetches entries from the database | ||
/// </summary> | ||
public class DbEntryTypesProvider : AEntryTypeProvider | ||
{ | ||
private readonly DayFlagsDb _db; | ||
|
||
public DbEntryTypesProvider(DayFlagsDb db) | ||
{ | ||
_db = db; | ||
} | ||
|
||
public override Task<List<EntryType>> GetEntryTypesAsync() | ||
{ | ||
return _db.EntryTypes.ToListAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using DayFlags.Core.Plugins; | ||
using McMaster.NETCore.Plugins; | ||
using Microsoft.Extensions.Logging.Console; | ||
|
||
namespace DayFlags.Server.Plugins; | ||
|
||
/// <summary> | ||
/// Responsible to load Plugins | ||
/// </summary> | ||
public class PluginsLoader | ||
{ | ||
|
||
private readonly static Type[] SharedTypes = new Type[]{ | ||
typeof(IServiceCollection), typeof(IServiceProvider), typeof(ILogger), | ||
typeof(IDayFlagPlugin) | ||
}; | ||
|
||
private readonly IConfiguration _config; | ||
private List<IDayFlagPlugin> _plugins = new(); | ||
private readonly LoggerFactory _loggerFactory; | ||
private readonly ILogger _logger; | ||
|
||
public PluginsLoader(IConfiguration config) | ||
{ | ||
_config = config; | ||
_loggerFactory = new LoggerFactory(); | ||
_loggerFactory.AddProvider(new ConsoleLoggerProvider(null)); | ||
_logger = _loggerFactory.CreateLogger("PluginLoader"); | ||
} | ||
|
||
private void LoadPlugin(string assemblyPath) | ||
{ | ||
_logger.LogInformation($"Loading Plugin {assemblyPath}"); | ||
var plugin = PluginLoader.CreateFromAssemblyFile( | ||
assemblyPath, true, SharedTypes | ||
); | ||
|
||
if (plugin != null) | ||
{ | ||
var pluginEntries = plugin.LoadDefaultAssembly() | ||
.GetTypes() | ||
.Where(e => e.IsAssignableFrom(typeof(IDayFlagPlugin)) | ||
&& !e.IsAbstract); | ||
|
||
foreach (var pluginEntry in pluginEntries) | ||
{ | ||
var instance = (IDayFlagPlugin?)Activator | ||
.CreateInstance(pluginEntry); | ||
|
||
if (instance != null) | ||
{ | ||
_plugins.Add(instance); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void ConfigureServices(IServiceCollection serviceDescriptors) | ||
{ | ||
foreach (var plugin in _plugins) | ||
{ | ||
serviceDescriptors.AddSingleton(typeof(IDayFlagPlugin), plugin); | ||
plugin.ConfigureServices(serviceDescriptors); | ||
} | ||
} | ||
|
||
public void ConfigureApp(WebApplicationBuilder webApplicationBuilder) | ||
{ | ||
foreach (var plugin in _plugins) | ||
{ | ||
plugin.ConfigureApp(webApplicationBuilder); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters