Host: Moving the default host to a subproject, Kyoo is now a library without host

This commit is contained in:
Zoe Roux 2021-08-30 13:43:54 +02:00 committed by Diabltica
parent acf87b2619
commit e338fc6b37
14 changed files with 94 additions and 34 deletions

View File

@ -44,7 +44,7 @@ jobs:
uses: ilammy/msvc-dev-cmd@v1
- name: Select the project to build
shell: bash
run: echo "PROJECT=$([ "${{runner.os}}" == "Windows" ] && echo Kyoo.WindowsHost || echo Kyoo)" >> $GITHUB_ENV
run: echo "PROJECT=$([ "${{runner.os}}" == "Windows" ] && echo Kyoo.Host.WindowsTrait || echo Kyoo.Host.Console)" >> $GITHUB_ENV
- name: Build the app
env:
INCLUDE: ${{env.INCLUDE}};C:\Program Files\FFmpeg\include

View File

@ -20,5 +20,12 @@
/// </summary>
/// <returns>Retrieve the data directory where runtime data should be stored</returns>
string GetDataDirectory();
/// <summary>
/// Retrieve the path of the json configuration file
/// (relative to the data directory, see <see cref="GetDataDirectory"/>).
/// </summary>
/// <returns>The configuration file name.</returns>
string GetConfigFile();
}
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<StartupObject>Kyoo.Host.Console.Program</StartupObject>
<Company>SDG</Company>
<Authors>Zoe Roux</Authors>
<RepositoryUrl>https://github.com/AnonymusRaccoon/Kyoo</RepositoryUrl>
<LangVersion>default</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../Kyoo/Kyoo.csproj" />
</ItemGroup>
</Project>

View File

@ -1,25 +1,20 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
namespace Kyoo
namespace Kyoo.Host.Console
{
/// <summary>
/// Program entrypoint.
/// </summary>
public static class Program
{
/// <summary>
/// The path of the json configuration of the application.
/// </summary>
public const string JsonConfigPath = "./settings.json";
/// <summary>
/// The string representation of the environment used in <see cref="IWebHostEnvironment"/>.
/// </summary>
#if DEBUG
public const string Environment = "Development";
private const string Environment = "Development";
#else
public const string Environment = "Production";
private const string Environment = "Production";
#endif
/// <summary>
@ -28,7 +23,7 @@ namespace Kyoo
/// <param name="args">Command line arguments</param>
public static Task Main(string[] args)
{
Application application = new();
Application application = new(Environment);
return application.Start(args);
}
}

View File

@ -3,8 +3,8 @@
<IsWindows Condition="$([MSBuild]::IsOSPlatform('Windows'))">true</IsWindows>
</PropertyGroup>
<Import Project="Kyoo.WindowsHost.target" Condition="$(IsWindows) == true" />
<Import Project="Kyoo.WindowsHost.linux.target" Condition="$(IsWindows) != true" />
<Import Project="Kyoo.Host.WindowsTrait.target" Condition="$(IsWindows) == true" />
<Import Project="Kyoo.Host.WindowsTrait.linux.target" Condition="$(IsWindows) != true" />
<ItemGroup>
<None Remove="*.target" />

View File

@ -1,18 +1,27 @@
using System.Threading.Tasks;
using Autofac;
namespace Kyoo.WindowsHost
namespace Kyoo.Host.WindowsTrait
{
public static class Program
{
/// <summary>
/// The main entry point for the application that overrides the default host (<see cref="Kyoo.Program"/>).
/// The string representation of the environment used in IWebHostEnvironment.
/// </summary>
#if DEBUG
private const string Environment = "Development";
#else
private const string Environment = "Production";
#endif
/// <summary>
/// The main entry point for the application that overrides the default host.
/// It adds a system trait for windows and since the host is build as a windows executable instead of a console
/// app, the console is not showed.
/// </summary>
public static Task Main(string[] args)
{
Application application = new();
Application application = new(Environment);
return application.Start(args, builder =>
{
builder.RegisterType<SystemTrait>().As<IStartable>().SingleInstance();

View File

@ -9,7 +9,7 @@ using Kyoo.Abstractions.Controllers;
using Kyoo.Models.Options;
using Microsoft.Extensions.Options;
namespace Kyoo.WindowsHost
namespace Kyoo.Host.WindowsTrait
{
/// <summary>
/// A singleton that add an notification icon on the window's toolbar.

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -19,7 +19,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.Tests", "tests\Kyoo.Te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.WebApp", "Kyoo.WebApp\Kyoo.WebApp.csproj", "{2374D500-1ADB-4752-85DB-8BB0DDF5A8E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.WindowsHost", "Kyoo.WindowsHost\Kyoo.WindowsHost.csproj", "{98851001-40DD-46A6-94B3-2F8D90722076}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.Host.WindowsTrait", "Kyoo.Host.WindowsTrait\Kyoo.Host.WindowsTrait.csproj", "{98851001-40DD-46A6-94B3-2F8D90722076}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kyoo.Host.Console", "Kyoo.Host.Console\Kyoo.Host.Console.csproj", "{D8658BEA-8949-45AC-BEBB-A4FFC4F800F5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -75,5 +77,9 @@ Global
{98851001-40DD-46A6-94B3-2F8D90722076}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98851001-40DD-46A6-94B3-2F8D90722076}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98851001-40DD-46A6-94B3-2F8D90722076}.Release|Any CPU.Build.0 = Release|Any CPU
{D8658BEA-8949-45AC-BEBB-A4FFC4F800F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8658BEA-8949-45AC-BEBB-A4FFC4F800F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8658BEA-8949-45AC-BEBB-A4FFC4F800F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8658BEA-8949-45AC-BEBB-A4FFC4F800F5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -37,6 +37,22 @@ namespace Kyoo
/// </summary>
private CancellationTokenSource _tokenSource;
/// <summary>
/// The environment in witch Kyoo will run (ether "Production" or "Development").
/// </summary>
private readonly string _environment;
/// <summary>
/// Create a new <see cref="Application"/> that will use the specified environment.
/// </summary>
/// <param name="environment">The environment to run in.</param>
public Application(string environment)
{
_environment = environment;
}
/// <summary>
/// Start the application with the given console args.
/// This is generally called from the Main entrypoint of Kyoo.
@ -100,7 +116,14 @@ namespace Kyoo
{
return _dataDir;
}
/// <inheritdoc />
public string GetConfigFile()
{
return "./settings.json";
}
/// <summary>
/// Parse the data directory from environment variables and command line arguments, create it if necessary.
/// Set the current directory to said data folder and place a default configuration file if it does not already
@ -108,7 +131,7 @@ namespace Kyoo
/// </summary>
/// <param name="args">The command line arguments</param>
/// <returns>The current data directory.</returns>
private static string _SetupDataDir(string[] args)
private string _SetupDataDir(string[] args)
{
Dictionary<string, string> registry = new();
@ -134,9 +157,9 @@ namespace Kyoo
Directory.CreateDirectory(path);
Environment.CurrentDirectory = path;
if (!File.Exists(Program.JsonConfigPath))
File.Copy(Path.Join(AppDomain.CurrentDomain.BaseDirectory, Program.JsonConfigPath),
Program.JsonConfigPath);
if (!File.Exists(GetConfigFile()))
File.Copy(Path.Join(AppDomain.CurrentDomain.BaseDirectory, GetConfigFile()),
GetConfigFile());
return path;
}
@ -159,7 +182,7 @@ namespace Kyoo
Log.Fatal(ex, "Unhandled exception");
}
}
/// <summary>
/// Create a a web host
/// </summary>
@ -172,7 +195,7 @@ namespace Kyoo
return new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
.UseEnvironment(Program.Environment)
.UseEnvironment(_environment)
.ConfigureAppConfiguration(x => _SetupConfig(x, args))
.UseSerilog((host, builder) => _ConfigureLogging(builder, host.Configuration))
.ConfigureServices(x => x.AddRouting())
@ -198,8 +221,8 @@ namespace Kyoo
private IConfigurationBuilder _SetupConfig(IConfigurationBuilder builder, string[] args)
{
return builder.SetBasePath(GetDataDirectory())
.AddJsonFile(Path.Join(AppDomain.CurrentDomain.BaseDirectory, Program.JsonConfigPath), false, true)
.AddJsonFile(Program.JsonConfigPath, false, true)
.AddJsonFile(Path.Join(AppDomain.CurrentDomain.BaseDirectory, GetConfigFile()), false, true)
.AddJsonFile(GetConfigFile(), false, true)
.AddEnvironmentVariables()
.AddEnvironmentVariables("KYOO_")
.AddCommandLine(args);

View File

@ -21,6 +21,11 @@ namespace Kyoo.Controllers
/// </summary>
private readonly IConfiguration _configuration;
/// <summary>
/// The application running Kyoo, it is used to retrieve the configuration file.
/// </summary>
private readonly IApplication _application;
/// <summary>
/// The strongly typed list of options
/// </summary>
@ -31,9 +36,11 @@ namespace Kyoo.Controllers
/// </summary>
/// <param name="configuration">The configuration to use.</param>
/// <param name="references">The strongly typed option list.</param>
public ConfigurationManager(IConfiguration configuration, IEnumerable<ConfigurationReference> references)
/// <param name="application">The application running Kyoo, it is used to retrieve the configuration file.</param>
public ConfigurationManager(IConfiguration configuration, IEnumerable<ConfigurationReference> references, IApplication application)
{
_configuration = configuration;
_application = application;
_references = references.ToDictionary(x => x.Path, x => x.Type, StringComparer.OrdinalIgnoreCase);
}
@ -131,7 +138,7 @@ namespace Kyoo.Controllers
IDictionary<string, object> configDic = config;
configDic[path] = value;
JObject obj = JObject.FromObject(config);
await using StreamWriter writer = new(Program.JsonConfigPath);
await using StreamWriter writer = new(_application.GetConfigFile());
await writer.WriteAsync(obj.ToString());
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
@ -7,7 +7,6 @@
<Company>SDG</Company>
<Authors>Zoe Roux</Authors>
<RepositoryUrl>https://github.com/AnonymusRaccoon/Kyoo</RepositoryUrl>
<StartupObject>Kyoo.Program</StartupObject>
<LangVersion>default</LangVersion>
</PropertyGroup>
@ -55,10 +54,7 @@
<Exec WorkingDirectory="$(TranscoderRoot)" Condition="'$(IsWindows)' == 'true'" Command="(if not exist build mkdir build) %26%26 cd build %26%26 cmake .. -G &quot;NMake Makefiles&quot; %26%26 nmake" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
<Error Condition="'$(ErrorCode)' != '0'" Text="An environement capable of building the transcoder was not found. Appropriate tools are not installed, not available in the $PATH or not correctly configured. To fix this you can ether:
- Fix your tools
- Skip the transcoder via the '-p:SkipTranscoder=true'
- Download an already built transcoder and put it in ./Kyoo.Transcoder/build" />
<Error Condition="'$(ErrorCode)' != '0'" Text="An environement capable of building the transcoder was not found. Appropriate tools are not installed, not available in the $PATH or not correctly configured. To fix this you can ether:&#xA; - Fix your tools&#xA; - Skip the transcoder via the '-p:SkipTranscoder=true'&#xA; - Download an already built transcoder and put it in ./Kyoo.Transcoder/build" />
</Target>
<ItemGroup Condition="'$(SkipTranscoder)' != 'true'">
@ -70,5 +66,6 @@
<ItemGroup>
<Content Include="../LICENSE" CopyToOutputDirectory="Always" Visible="false" />
<Content Include="settings.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>