72 lines
1.7 KiB
C#
72 lines
1.7 KiB
C#
using Connected.Interop;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Connected.Instance
|
|
{
|
|
public static class Instance
|
|
{
|
|
internal static WebApplication Host { get; private set; }
|
|
|
|
internal static async Task StartAsync(IConfiguration config)
|
|
{
|
|
var options = new WebApplicationOptions();
|
|
config.Bind(options);
|
|
|
|
var builder = WebApplication
|
|
.CreateBuilder(options);
|
|
|
|
builder.Configuration.AddConfiguration(config);
|
|
|
|
var startups = Assemblies.QueryImplementations<IStartup>();
|
|
|
|
foreach (var assembly in Assemblies.All)
|
|
builder.Services.AddMicroService(assembly);
|
|
|
|
foreach (var startup in startups)
|
|
{
|
|
if (startup.CreateInstance<IStartup>() is IStartup start)
|
|
start.ConfigureServices(builder.Services);
|
|
}
|
|
|
|
Host = builder.Build();
|
|
|
|
foreach (var startup in startups)
|
|
{
|
|
if (startup.CreateInstance<IStartup>() is IStartup start)
|
|
start.Configure(Host);
|
|
}
|
|
|
|
foreach (var startup in startups)
|
|
{
|
|
if (startup.CreateInstance<IStartup>() is IStartup start)
|
|
await start.Initialize(config);
|
|
}
|
|
|
|
foreach (var startup in startups)
|
|
{
|
|
if (startup.CreateInstance<IStartup>() is IStartup start)
|
|
await start.Start(config);
|
|
}
|
|
|
|
if (config.GetValue<string?>("entitySynchronization") is string entities)
|
|
await EntitySynchronizer.Synchronize(Host.Services, entities);
|
|
|
|
await Host.RunAsync();
|
|
}
|
|
|
|
private static string[] UnpackArguments(Dictionary<string, string> args)
|
|
{
|
|
var result = new string[args.Count];
|
|
|
|
for (var i = 0; i < args.Count; i++)
|
|
{
|
|
var arg = args.ElementAt(i);
|
|
|
|
result[i] = string.IsNullOrWhiteSpace(arg.Value) ? arg.Key : $"{arg.Key}={arg.Value}";
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |