Started implementing fronted configuration. Upgraded mooncore. Made database calls asnyc
This commit is contained in:
57
Moonlight.ApiServer/Http/Controllers/FrontendController.cs
Normal file
57
Moonlight.ApiServer/Http/Controllers/FrontendController.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MoonCore.Exceptions;
|
||||
using Moonlight.ApiServer.Configuration;
|
||||
using Moonlight.ApiServer.Services;
|
||||
using Moonlight.Shared.Misc;
|
||||
|
||||
namespace Moonlight.ApiServer.Http.Controllers;
|
||||
|
||||
[ApiController]
|
||||
public class FrontendController : Controller
|
||||
{
|
||||
private readonly AppConfiguration Configuration;
|
||||
private readonly PluginService PluginService;
|
||||
private readonly AssetService AssetService;
|
||||
|
||||
public FrontendController(
|
||||
AppConfiguration configuration,
|
||||
PluginService pluginService,
|
||||
AssetService assetService
|
||||
)
|
||||
{
|
||||
Configuration = configuration;
|
||||
PluginService = pluginService;
|
||||
AssetService = assetService;
|
||||
}
|
||||
|
||||
[HttpGet("frontend.json")]
|
||||
public async Task<FrontendConfiguration> GetConfiguration()
|
||||
{
|
||||
var configuration = new FrontendConfiguration()
|
||||
{
|
||||
Title = "Moonlight",
|
||||
ApiUrl = Configuration.PublicUrl,
|
||||
HostEnvironment = "ApiServer"
|
||||
};
|
||||
|
||||
configuration.Plugins.Entrypoints = PluginService.HostedPluginsManifest.Entrypoints;
|
||||
configuration.Plugins.Assemblies = PluginService.HostedPluginsManifest.Assemblies;
|
||||
|
||||
configuration.Scripts = AssetService.GetJavascriptAssets();
|
||||
|
||||
return configuration;
|
||||
}
|
||||
|
||||
[HttpGet("plugins/{assemblyName}")]
|
||||
public async Task GetPluginAssembly(string assemblyName)
|
||||
{
|
||||
var assembliesMap = PluginService.ClientAssemblyMap;
|
||||
|
||||
if (assembliesMap.ContainsKey(assemblyName))
|
||||
throw new HttpApiException("The requested assembly could not be found", 404);
|
||||
|
||||
var path = assembliesMap[assemblyName];
|
||||
|
||||
await Results.File(path).ExecuteAsync(HttpContext);
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Login to your moonlight account</title>
|
||||
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<script src="https://cdn.tailwindcss.com?plugins=forms"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="flex min-h-full flex-col justify-center px-6 py-12 lg:px-8">
|
||||
<div class="sm:mx-auto sm:w-full sm:max-w-sm">
|
||||
<img class="mx-auto h-10 w-auto" src="https://gamecp.masuowo.xyz/api/core/asset/Core/svg/logo.svg" alt="Your Company">
|
||||
<h2 class="mt-10 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900">Sign in to your account</h2>
|
||||
</div>
|
||||
|
||||
<div class="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
|
||||
<form class="space-y-6" action="#" method="POST">
|
||||
<div>
|
||||
<label for="email" class="block text-sm font-medium leading-6 text-gray-900">Email address</label>
|
||||
<div class="mt-2">
|
||||
<input id="email" name="email" type="email" autocomplete="email" required class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div class="flex items-center justify-between">
|
||||
<label for="password" class="block text-sm font-medium leading-6 text-gray-900">Password</label>
|
||||
<div class="text-sm">
|
||||
<a href="#" class="font-semibold text-indigo-600 hover:text-indigo-500">Forgot password?</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
<input id="password" name="password" type="password" autocomplete="current-password" required class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<button type="submit" class="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Sign in</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<p class="mt-10 text-center text-sm text-gray-500">
|
||||
Not a member?
|
||||
|
||||
@{
|
||||
var registerUrl = $"?response_type={ResponseType}&client_id={ClientId}&redirect_uri={RedirectUri}&action=register";
|
||||
}
|
||||
|
||||
<a href="@registerUrl" class="font-semibold leading-6 text-indigo-600 hover:text-indigo-500">Register now</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@code
|
||||
{
|
||||
[Parameter]
|
||||
public string ClientId { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string RedirectUri { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string ResponseType { get; set; }
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Register your moonlight account</title>
|
||||
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<script src="https://cdn.tailwindcss.com?plugins=forms"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="flex min-h-full flex-col justify-center px-6 py-12 lg:px-8">
|
||||
<div class="sm:mx-auto sm:w-full sm:max-w-sm">
|
||||
<img class="mx-auto h-10 w-auto" src="https://gamecp.masuowo.xyz/api/core/asset/Core/svg/logo.svg" alt="Your Company">
|
||||
<h2 class="mt-10 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900">Create your account</h2>
|
||||
</div>
|
||||
|
||||
<div class="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
|
||||
<form class="space-y-6" action="#" method="POST">
|
||||
|
||||
<div>
|
||||
<label for="email" class="block text-sm font-medium leading-6 text-gray-900">Username</label>
|
||||
<div class="mt-2">
|
||||
<input id="username" name="username" type="text" autocomplete="username" required class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="email" class="block text-sm font-medium leading-6 text-gray-900">Email address</label>
|
||||
<div class="mt-2">
|
||||
<input id="email" name="email" type="email" autocomplete="email" required class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div class="flex items-center justify-between">
|
||||
<label for="password" class="block text-sm font-medium leading-6 text-gray-900">Password</label>
|
||||
<div class="text-sm">
|
||||
<a href="#" class="font-semibold text-indigo-600 hover:text-indigo-500">Forgot password?</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
<input id="password" name="password" type="password" autocomplete="current-password" required class="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<button type="submit" class="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Sign up</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<p class="mt-10 text-center text-sm text-gray-500">
|
||||
Already a member?
|
||||
|
||||
@{
|
||||
var loginUrl = $"?response_type={ResponseType}&client_id={ClientId}&redirect_uri={RedirectUri}&action=login";
|
||||
}
|
||||
|
||||
<a href="@loginUrl" class="font-semibold leading-6 text-indigo-600 hover:text-indigo-500">Login now</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@code
|
||||
{
|
||||
[Parameter]
|
||||
public string ClientId { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string RedirectUri { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public string ResponseType { get; set; }
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.Helpers;
|
||||
@@ -15,17 +16,18 @@ public class LocalOAuth2Provider : ILocalProviderImplementation<User>
|
||||
UserRepository = userRepository;
|
||||
}
|
||||
|
||||
public Task SaveChanges(User model)
|
||||
public async Task SaveChanges(User model)
|
||||
{
|
||||
UserRepository.Update(model);
|
||||
return Task.CompletedTask;
|
||||
await UserRepository.Update(model);
|
||||
}
|
||||
|
||||
public Task<User?> LoadById(int id)
|
||||
public async Task<User?> LoadById(int id)
|
||||
{
|
||||
var res = UserRepository.Get().FirstOrDefault(x => x.Id == id);
|
||||
var res = await UserRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
return Task.FromResult(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
public Task<User> Login(string email, string password)
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="MoonCore" Version="1.8.1" />
|
||||
<PackageReference Include="MoonCore.Extended" Version="1.2.5" />
|
||||
<PackageReference Include="MoonCore.Extended" Version="1.2.6" />
|
||||
<PackageReference Include="MoonCore.PluginFramework" Version="1.0.5" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
|
||||
@@ -39,6 +39,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Database\Migrations\" />
|
||||
<Folder Include="Http\Controllers\Frontend\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -47,4 +48,9 @@
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<_ContentIncludedByDefault Remove="Http\Controllers\OAuth2\Pages\Login.razor" />
|
||||
<_ContentIncludedByDefault Remove="Http\Controllers\OAuth2\Pages\Register.razor" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
41
Moonlight.Client/Implementations/RemotePluginSource.cs
Normal file
41
Moonlight.Client/Implementations/RemotePluginSource.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Runtime.Loader;
|
||||
using MoonCore.Plugins;
|
||||
using Moonlight.Shared.Misc;
|
||||
|
||||
namespace Moonlight.Client.Implementations;
|
||||
|
||||
public class RemotePluginSource : IPluginSource
|
||||
{
|
||||
private readonly FrontendConfiguration Configuration;
|
||||
private readonly ILogger<RemotePluginSource> Logger;
|
||||
private readonly HttpClient HttpClient;
|
||||
|
||||
public RemotePluginSource(
|
||||
FrontendConfiguration configuration,
|
||||
ILogger<RemotePluginSource> logger,
|
||||
HttpClient httpClient
|
||||
)
|
||||
{
|
||||
Configuration = configuration;
|
||||
Logger = logger;
|
||||
HttpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task Load(AssemblyLoadContext loadContext, List<string> entrypoints)
|
||||
{
|
||||
entrypoints.AddRange(Configuration.Plugins.Entrypoints);
|
||||
|
||||
foreach (var assembly in Configuration.Plugins.Assemblies)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileStream = await HttpClient.GetStreamAsync($"plugins/{assembly}");
|
||||
loadContext.LoadFromStream(fileStream);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogCritical("Unable to load plugin assembly '{assembly}': {e}", assembly, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@
|
||||
<PackageReference Include="MoonCore" Version="1.8.1" />
|
||||
<PackageReference Include="MoonCore.Blazor" Version="1.2.8" />
|
||||
<PackageReference Include="MoonCore.PluginFramework" Version="1.0.5"/>
|
||||
<PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.2.3" />
|
||||
<PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.2.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<!--
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using Microsoft.JSInterop;
|
||||
@@ -11,12 +12,11 @@ using MoonCore.Extensions;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.PluginFramework.Extensions;
|
||||
using MoonCore.Plugins;
|
||||
using Moonlight.Client.Implementations;
|
||||
using Moonlight.Client.Interfaces;
|
||||
using Moonlight.Client.Services;
|
||||
using Moonlight.Client.UI;
|
||||
using Moonlight.Client.UI.Forms;
|
||||
using Moonlight.Shared.Http.Responses.Assets;
|
||||
using Moonlight.Shared.Http.Responses.PluginsStream;
|
||||
using Moonlight.Shared.Misc;
|
||||
|
||||
namespace Moonlight.Client;
|
||||
|
||||
@@ -24,6 +24,9 @@ public class Startup
|
||||
{
|
||||
private string[] Args;
|
||||
|
||||
// Configuration
|
||||
private FrontendConfiguration Configuration;
|
||||
|
||||
// Logging
|
||||
private ILoggerProvider[] LoggerProviders;
|
||||
private ILoggerFactory LoggerFactory;
|
||||
@@ -54,6 +57,7 @@ public class Startup
|
||||
|
||||
await CreateWebAssemblyHostBuilder();
|
||||
|
||||
await LoadConfiguration();
|
||||
await LoadPlugins();
|
||||
await InitializePlugins();
|
||||
|
||||
@@ -93,6 +97,29 @@ public class Startup
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task LoadConfiguration()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
httpClient.BaseAddress = new Uri(WebAssemblyHostBuilder.HostEnvironment.BaseAddress);
|
||||
|
||||
var jsonText = await httpClient.GetStringAsync("frontend.json");
|
||||
|
||||
Configuration = JsonSerializer.Deserialize<FrontendConfiguration>(jsonText, new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
})!;
|
||||
|
||||
WebAssemblyHostBuilder.Services.AddSingleton(Configuration);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogCritical("Unable to load configuration. Unable to continue: {e}", e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private Task RegisterBase()
|
||||
{
|
||||
WebAssemblyHostBuilder.RootComponents.Add<App>("#app");
|
||||
@@ -101,7 +128,7 @@ public class Startup
|
||||
WebAssemblyHostBuilder.Services.AddScoped(_ =>
|
||||
new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(WebAssemblyHostBuilder.HostEnvironment.BaseAddress)
|
||||
BaseAddress = new Uri(Configuration.ApiUrl)
|
||||
}
|
||||
);
|
||||
|
||||
@@ -135,13 +162,10 @@ public class Startup
|
||||
|
||||
private async Task LoadAssets()
|
||||
{
|
||||
var apiClient = WebAssemblyHost.Services.GetRequiredService<HttpApiClient>();
|
||||
var assetManifest = await apiClient.GetJson<FrontendAssetResponse>("api/assets");
|
||||
|
||||
var jsRuntime = WebAssemblyHost.Services.GetRequiredService<IJSRuntime>();
|
||||
|
||||
foreach (var javascriptFile in assetManifest.JavascriptFiles)
|
||||
await jsRuntime.InvokeVoidAsync("moonlight.assets.loadJavascript", javascriptFile);
|
||||
foreach (var scriptName in Configuration.Scripts)
|
||||
await jsRuntime.InvokeVoidAsync("moonlight.assets.loadJavascript", scriptName);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -177,9 +201,19 @@ public class Startup
|
||||
LoggerFactory.CreateLogger<PluginLoaderService>()
|
||||
);
|
||||
|
||||
// Build source from the retrieved data
|
||||
var pluginsStreamUrl = $"{WebAssemblyHostBuilder.HostEnvironment.BaseAddress}api/assets/plugins";
|
||||
PluginLoaderService.AddHttpHostedSource(pluginsStreamUrl);
|
||||
// Create everything required to stream plugins
|
||||
using var clientForStreaming = new HttpClient();
|
||||
|
||||
clientForStreaming.BaseAddress = new Uri(Configuration.HostEnvironment == "ApiServer"
|
||||
? Configuration.ApiUrl
|
||||
: WebAssemblyHostBuilder.HostEnvironment.BaseAddress
|
||||
);
|
||||
|
||||
PluginLoaderService.AddSource(new RemotePluginSource(
|
||||
Configuration,
|
||||
LoggerFactory.CreateLogger<RemotePluginSource>(),
|
||||
clientForStreaming
|
||||
));
|
||||
|
||||
// Perform assembly loading
|
||||
await PluginLoaderService.Load();
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
@using Moonlight.Client.Interfaces
|
||||
@using Moonlight.Client.Services
|
||||
@using Moonlight.Client.UI.Partials
|
||||
@using MoonCore.Blazor.Components
|
||||
@using Moonlight.Client.UI.Components
|
||||
@using Moonlight.Shared.Misc
|
||||
|
||||
@inherits LayoutComponentBase
|
||||
|
||||
@@ -12,6 +11,9 @@
|
||||
@inject ILogger<MainLayout> Logger
|
||||
@inject IAppLoader[] AppLoaders
|
||||
@inject IAppScreen[] AppScreens
|
||||
@inject FrontendConfiguration Configuration
|
||||
|
||||
<PageTitle>@Configuration.Title</PageTitle>
|
||||
|
||||
@if (IsLoading)
|
||||
{
|
||||
|
||||
@@ -10,3 +10,5 @@
|
||||
</div>
|
||||
<div class="text-gray-200 text-2xl">What do you want to do today?</div>
|
||||
</div>
|
||||
|
||||
<div class="text-primary-500/10"></div>
|
||||
19
Moonlight.Client/wwwroot/frontend.example.json
Normal file
19
Moonlight.Client/wwwroot/frontend.example.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"apiUrl": "http://localhost:5165",
|
||||
"hostEnvironment": "Static",
|
||||
"theme": {
|
||||
"variables": {
|
||||
"primary": {
|
||||
"500": "100 100 100"
|
||||
}
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
},
|
||||
"plugins": {
|
||||
"assemblies": [
|
||||
],
|
||||
"entrypoints": [
|
||||
]
|
||||
}
|
||||
}
|
||||
22
Moonlight.Shared/Misc/FrontendConfiguration.cs
Normal file
22
Moonlight.Shared/Misc/FrontendConfiguration.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
namespace Moonlight.Shared.Misc;
|
||||
|
||||
public class FrontendConfiguration
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string ApiUrl { get; set; }
|
||||
public string HostEnvironment { get; set; }
|
||||
public ThemeData Theme { get; set; } = new();
|
||||
public string[] Scripts { get; set; }
|
||||
public PluginData Plugins { get; set; } = new();
|
||||
|
||||
public class ThemeData
|
||||
{
|
||||
public Dictionary<string, Dictionary<int, string>> Variables { get; set; } = new();
|
||||
}
|
||||
|
||||
public class PluginData
|
||||
{
|
||||
public string[] Assemblies { get; set; }
|
||||
public string[] Entrypoints { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user