Server create container&element
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
14
src/Server/FileTime.Server.App/DummyAdminElevationManager.cs
Normal file
14
src/Server/FileTime.Server.App/DummyAdminElevationManager.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using FileTime.Providers.LocalAdmin;
|
||||
using FileTime.Server.Common;
|
||||
|
||||
namespace FileTime.Server.App;
|
||||
|
||||
public class DummyAdminElevationManager : IAdminElevationManager
|
||||
{
|
||||
public bool IsAdminInstanceRunning => throw new NotImplementedException();
|
||||
public Task<IRemoteConnection> CreateConnectionAsync() => throw new NotImplementedException();
|
||||
|
||||
public string ProviderName => throw new NotImplementedException();
|
||||
public Task CreateAdminInstanceIfNecessaryAsync(string? confirmationMessage = null) => throw new NotImplementedException();
|
||||
public bool IsAdminModeSupported => false;
|
||||
}
|
||||
18
src/Server/FileTime.Server.App/FileTime.Server.App.csproj
Normal file
18
src/Server/FileTime.Server.App/FileTime.Server.App.csproj
Normal file
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Providers\FileTime.Providers.LocalAdmin.Abstractions\FileTime.Providers.LocalAdmin.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\FileTime.Server.App.Abstractions\FileTime.Server.App.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
13
src/Server/FileTime.Server.App/Startup.cs
Normal file
13
src/Server/FileTime.Server.App/Startup.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using FileTime.Providers.LocalAdmin;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FileTime.Server.App;
|
||||
|
||||
public static class Startup
|
||||
{
|
||||
public static IServiceCollection AddServerServices(this IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddSingleton<IAdminElevationManager, DummyAdminElevationManager>();
|
||||
return serviceCollection;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>FileTime.Server.Common</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Core\FileTime.Core.Abstraction\FileTime.Core.Abstraction.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace FileTime.Server.Common;
|
||||
|
||||
public interface IApplicationStopper
|
||||
{
|
||||
void Stop();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using FileTime.Core.Models;
|
||||
|
||||
namespace FileTime.Server.Common;
|
||||
|
||||
public interface IRemoteConnection
|
||||
{
|
||||
Task Exit();
|
||||
Task CreateContainerAsync(string contentProviderId, FullName fullName);
|
||||
Task CreateElementAsync(string contentProviderId, FullName fullName);
|
||||
}
|
||||
15
src/Server/FileTime.Server.Common/ApplicationStopper.cs
Normal file
15
src/Server/FileTime.Server.Common/ApplicationStopper.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace FileTime.Server.Common;
|
||||
|
||||
public class ApplicationStopper : IApplicationStopper
|
||||
{
|
||||
private readonly Action _stopAction;
|
||||
|
||||
public ApplicationStopper(Action stopAction)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(stopAction);
|
||||
_stopAction = stopAction;
|
||||
}
|
||||
|
||||
|
||||
public void Stop() => _stopAction();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace FileTime.Server.Common.Connections.SignalR;
|
||||
|
||||
public interface ISignalRClient
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace FileTime.Server.Common.Connections.SignalR;
|
||||
|
||||
public interface ISignalRHub
|
||||
{
|
||||
Task Exit();
|
||||
Task CreateContainerAsync(string contentProviderId, string fullName);
|
||||
Task CreateElementAsync(string contentProviderId, string fullName);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using FileTime.Core.Models;
|
||||
using InitableService;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using TypedSignalR.Client;
|
||||
|
||||
namespace FileTime.Server.Common.Connections.SignalR;
|
||||
|
||||
public class SignalRConnection : IRemoteConnection, IAsyncInitable<string>
|
||||
{
|
||||
private string _baseUrl = null!;
|
||||
private HubConnection _connection = null!;
|
||||
|
||||
private ISignalRHub CreateClient() => _connection.CreateHubProxy<ISignalRHub>();
|
||||
|
||||
public async Task InitAsync(string baseUrl)
|
||||
{
|
||||
_baseUrl = baseUrl;
|
||||
|
||||
_connection = new HubConnectionBuilder()
|
||||
.WithUrl(_baseUrl)
|
||||
.Build();
|
||||
await _connection.StartAsync();
|
||||
}
|
||||
|
||||
public async Task Exit()
|
||||
{
|
||||
var client = CreateClient();
|
||||
await client.Exit();
|
||||
}
|
||||
|
||||
public async Task CreateContainerAsync(string contentProviderId, FullName fullName)
|
||||
{
|
||||
var client = CreateClient();
|
||||
await client.CreateContainerAsync(contentProviderId, fullName.Path);
|
||||
}
|
||||
|
||||
public async Task CreateElementAsync(string contentProviderId, FullName fullName)
|
||||
{
|
||||
var client = CreateClient();
|
||||
await client.CreateElementAsync(contentProviderId, fullName.Path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FileTime.Server.Common.Abstractions\FileTime.Server.Common.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.9" />
|
||||
<PackageReference Include="TypedSignalR.Client" Version="3.4.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
14
src/Server/FileTime.Server.Common/Startup.cs
Normal file
14
src/Server/FileTime.Server.Common/Startup.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using FileTime.Server.Common.Connections.SignalR;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FileTime.Server.Common;
|
||||
|
||||
public static class Startup
|
||||
{
|
||||
public static IServiceCollection AddRemoteServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<SignalRConnection>();
|
||||
services.AddSingleton<IApplicationStopper, ApplicationStopper>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
45
src/Server/FileTime.Server.Web/ConnectionHub.cs
Normal file
45
src/Server/FileTime.Server.Web/ConnectionHub.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using FileTime.Core.ContentAccess;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Server.Common;
|
||||
using FileTime.Server.Common.Connections.SignalR;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace FileTime.Server.Web;
|
||||
|
||||
public class ConnectionHub : Hub<ISignalRClient>, ISignalRHub
|
||||
{
|
||||
private readonly IContentProviderRegistry _contentProviderRegistry;
|
||||
private readonly IContentAccessorFactory _contentAccessorFactory;
|
||||
private readonly IApplicationStopper _applicationStopper;
|
||||
|
||||
public ConnectionHub(
|
||||
IContentProviderRegistry contentProviderRegistry,
|
||||
IContentAccessorFactory contentAccessorFactory,
|
||||
IApplicationStopper applicationStopper)
|
||||
{
|
||||
_contentProviderRegistry = contentProviderRegistry;
|
||||
_contentAccessorFactory = contentAccessorFactory;
|
||||
_applicationStopper = applicationStopper;
|
||||
}
|
||||
|
||||
public Task Exit()
|
||||
{
|
||||
_applicationStopper.Stop();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task CreateContainerAsync(string contentProviderId, string fullName)
|
||||
{
|
||||
//TODO handle no content provider with id
|
||||
var contentProvider = _contentProviderRegistry.ContentProviders.First(p => p.Name == contentProviderId);
|
||||
var itemCreator = _contentAccessorFactory.GetItemCreator(contentProvider);
|
||||
await itemCreator.CreateContainerAsync(contentProvider, new FullName(fullName));
|
||||
}
|
||||
|
||||
public async Task CreateElementAsync(string contentProviderId, string fullName)
|
||||
{
|
||||
var contentProvider = _contentProviderRegistry.ContentProviders.First(p => p.Name == contentProviderId);
|
||||
var itemCreator = _contentAccessorFactory.GetItemCreator(contentProvider);
|
||||
await itemCreator.CreateElementAsync(contentProvider, new FullName(fullName));
|
||||
}
|
||||
}
|
||||
22
src/Server/FileTime.Server.Web/FileTime.Server.Web.csproj
Normal file
22
src/Server/FileTime.Server.Web/FileTime.Server.Web.csproj
Normal file
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FileTime.Server.Common\FileTime.Server.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
53
src/Server/FileTime.Server.Web/Main.cs
Normal file
53
src/Server/FileTime.Server.Web/Main.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Net;
|
||||
using Autofac;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Hosting.Server;
|
||||
using Microsoft.AspNetCore.Hosting.Server.Features;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
|
||||
namespace FileTime.Server.Web;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static async Task Start(string[] args, IContainer rootContainer, CancellationToken applicationExit)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var configuration = builder.Configuration;
|
||||
|
||||
builder.Host.UseServiceProviderFactory(
|
||||
new AutofacChildLifetimeScopeServiceProviderFactory(rootContainer.BeginLifetimeScope("WebScope"))
|
||||
);
|
||||
|
||||
builder.Host.UseSerilog();
|
||||
builder.WebHost.ConfigureKestrel((buildContext, serverOptions) =>
|
||||
{
|
||||
var port = buildContext.Configuration.GetValue<int?>("WebPort") ?? 0;
|
||||
serverOptions.Listen(new IPEndPoint(IPAddress.Loopback, port));
|
||||
});
|
||||
|
||||
builder.Services.AddSignalR();
|
||||
builder.Services.AddHealthChecks();
|
||||
builder.Services.AddHostedService<PortWriterService>();
|
||||
|
||||
builder.Services.AddOptions<PortWriterConfiguration>()
|
||||
.Bind(configuration.GetSection(PortWriterConfiguration.SectionName));
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (!app.Environment.IsDevelopment())
|
||||
{
|
||||
}
|
||||
|
||||
app.MapHub<ConnectionHub>("/RemoteHub");
|
||||
app.UseHealthChecks("/health");
|
||||
|
||||
await app.RunAsync(applicationExit);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace FileTime.Server.Web;
|
||||
|
||||
public class PortWriterConfiguration
|
||||
{
|
||||
public const string SectionName = "PortWriter";
|
||||
public string? Filename { get; set; }
|
||||
}
|
||||
79
src/Server/FileTime.Server.Web/PortWriterService.cs
Normal file
79
src/Server/FileTime.Server.Web/PortWriterService.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using Microsoft.AspNetCore.Hosting.Server;
|
||||
using Microsoft.AspNetCore.Hosting.Server.Features;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace FileTime.Server.Web;
|
||||
|
||||
public class PortWriterService : IHostedService
|
||||
{
|
||||
private readonly IServer _server;
|
||||
private readonly IHostApplicationLifetime _hostApplicationLifetime;
|
||||
private readonly IOptions<PortWriterConfiguration> _configuration;
|
||||
private readonly ILogger<PortWriterService> _logger;
|
||||
|
||||
public PortWriterService(
|
||||
IServer server,
|
||||
IHostApplicationLifetime hostApplicationLifetime,
|
||||
IOptions<PortWriterConfiguration> configuration,
|
||||
ILogger<PortWriterService> logger)
|
||||
{
|
||||
_server = server;
|
||||
_hostApplicationLifetime = hostApplicationLifetime;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_hostApplicationLifetime.ApplicationStarted.Register(WritePort);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void WritePort()
|
||||
{
|
||||
try
|
||||
{
|
||||
var filename = _configuration.Value.Filename;
|
||||
if (filename is null)
|
||||
{
|
||||
_logger.LogWarning("Could not save port to file as there were no file name given");
|
||||
return;
|
||||
}
|
||||
|
||||
using var tempFileStream = File.CreateText(filename);
|
||||
var address = GetAddress();
|
||||
if (address is null)
|
||||
{
|
||||
_logger.LogError("Could not get address");
|
||||
return;
|
||||
}
|
||||
|
||||
var couldParsePort = int.TryParse(address.Split(':').LastOrDefault(), out var port);
|
||||
if (!couldParsePort)
|
||||
{
|
||||
_logger.LogError("Could not parse port from address {Address}", address);
|
||||
return;
|
||||
}
|
||||
|
||||
tempFileStream.Write(port.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Could not save port to file");
|
||||
}
|
||||
}
|
||||
|
||||
private string? GetAddress()
|
||||
{
|
||||
var features = _server.Features;
|
||||
var addresses = features.Get<IServerAddressesFeature>();
|
||||
var address = addresses?.Addresses.FirstOrDefault();
|
||||
return address;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
23
src/Server/FileTime.Server/FileTime.Server.csproj
Normal file
23
src/Server/FileTime.Server/FileTime.Server.csproj
Normal file
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\AppCommon\FileTime.App.DependencyInjection\FileTime.App.DependencyInjection.csproj" />
|
||||
<ProjectReference Include="..\..\Providers\FileTime.Providers.Local\FileTime.Providers.Local.csproj" />
|
||||
<ProjectReference Include="..\FileTime.Server.App\FileTime.Server.App.csproj" />
|
||||
<ProjectReference Include="..\FileTime.Server.Web\FileTime.Server.Web.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog" Version="3.0.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
64
src/Server/FileTime.Server/Program.cs
Normal file
64
src/Server/FileTime.Server/Program.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Autofac;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using FileTime.App.DependencyInjection;
|
||||
using FileTime.Providers.Local;
|
||||
using FileTime.Server.App;
|
||||
using FileTime.Server.Common;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Serilog;
|
||||
|
||||
|
||||
var applicationCancellation = new CancellationTokenSource();
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
|
||||
var bootstrapConfiguration = CreateConfiguration();
|
||||
|
||||
var rootContainer = CreateRootDiContainer(bootstrapConfiguration);
|
||||
|
||||
var webThread = CreateStartup(FileTime.Server.Web.Program.Start);
|
||||
webThread.Start();
|
||||
|
||||
Thread CreateStartup(Func<string[], IContainer, CancellationToken, Task> startup)
|
||||
{
|
||||
var thread = new Thread(() => { HandleStartup(() => startup(args, rootContainer, applicationCancellation.Token).Wait()); });
|
||||
return thread;
|
||||
}
|
||||
|
||||
void HandleStartup(Action action)
|
||||
{
|
||||
try
|
||||
{
|
||||
action();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
IConfigurationRoot CreateConfiguration()
|
||||
{
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
return configurationBuilder.Build();
|
||||
}
|
||||
|
||||
IContainer CreateRootDiContainer(IConfigurationRoot configuration)
|
||||
{
|
||||
var serviceCollection = DependencyInjection
|
||||
.RegisterDefaultServices(configuration)
|
||||
.AddLocalProviderServices()
|
||||
.AddServerServices();
|
||||
|
||||
serviceCollection.TryAddSingleton<IApplicationStopper>(
|
||||
new ApplicationStopper(() => applicationCancellation.Cancel())
|
||||
);
|
||||
|
||||
var containerBuilder = new ContainerBuilder();
|
||||
containerBuilder.Populate(serviceCollection);
|
||||
return containerBuilder.Build();
|
||||
}
|
||||
Reference in New Issue
Block a user