Removed old architecture. Added new base project structure
This commit is contained in:
17
Moonlight.ApiServer/Configuration/AppConfiguration.cs
Normal file
17
Moonlight.ApiServer/Configuration/AppConfiguration.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Moonlight.ApiServer.Configuration;
|
||||
|
||||
public class AppConfiguration
|
||||
{
|
||||
public DatabaseConfig Database { get; set; } = new();
|
||||
|
||||
public class DatabaseConfig
|
||||
{
|
||||
public string Host { get; set; } = "your-database-host.name";
|
||||
public int Port { get; set; } = 3306;
|
||||
|
||||
public string Username { get; set; } = "db_user";
|
||||
public string Password { get; set; } = "db_password";
|
||||
|
||||
public string Database { get; set; } = "db_name";
|
||||
}
|
||||
}
|
||||
8
Moonlight.ApiServer/Database/CoreDataContext.cs
Normal file
8
Moonlight.ApiServer/Database/CoreDataContext.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Moonlight.ApiServer.Helpers;
|
||||
|
||||
namespace Moonlight.ApiServer.Database;
|
||||
|
||||
public class CoreDataContext : DatabaseContext
|
||||
{
|
||||
public override string Prefix { get; } = "Core";
|
||||
}
|
||||
11
Moonlight.ApiServer/Helpers/ApplicationStateHelper.cs
Normal file
11
Moonlight.ApiServer/Helpers/ApplicationStateHelper.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using MoonCore.Services;
|
||||
using Moonlight.ApiServer.Configuration;
|
||||
|
||||
namespace Moonlight.ApiServer.Helpers;
|
||||
|
||||
public class ApplicationStateHelper
|
||||
{
|
||||
public static ConfigService<AppConfiguration>? Configuration { get; private set; }
|
||||
|
||||
public static void SetConfiguration(ConfigService<AppConfiguration>? configuration) => Configuration = configuration;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Moonlight.ApiServer.Helpers.Authentication;
|
||||
|
||||
public class SyncedClaimsPrinciple : ClaimsPrincipal
|
||||
{
|
||||
|
||||
}
|
||||
59
Moonlight.ApiServer/Helpers/DatabaseContext.cs
Normal file
59
Moonlight.ApiServer/Helpers/DatabaseContext.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.Services;
|
||||
using Moonlight.ApiServer.Configuration;
|
||||
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
|
||||
|
||||
namespace Moonlight.ApiServer.Helpers;
|
||||
|
||||
public abstract class DatabaseContext : DbContext
|
||||
{
|
||||
private ConfigService<AppConfiguration>? ConfigService;
|
||||
public abstract string Prefix { get; }
|
||||
|
||||
public DatabaseContext()
|
||||
{
|
||||
ConfigService = ApplicationStateHelper.Configuration;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured)
|
||||
return;
|
||||
|
||||
// If no config service has been configured, we are probably
|
||||
// in a EF Core migration, so we need to construct the config manually
|
||||
if (ConfigService == null)
|
||||
{
|
||||
ConfigService = new ConfigService<AppConfiguration>(
|
||||
PathBuilder.File("storage", "config.json")
|
||||
);
|
||||
}
|
||||
|
||||
var config = ConfigService.Get().Database;
|
||||
|
||||
var connectionString = $"host={config.Host};" +
|
||||
$"port={config.Port};" +
|
||||
$"database={config.Database};" +
|
||||
$"uid={config.Username};" +
|
||||
$"pwd={config.Password}";
|
||||
|
||||
optionsBuilder.UseMySql(
|
||||
connectionString,
|
||||
ServerVersion.AutoDetect(connectionString),
|
||||
builder =>
|
||||
{
|
||||
builder.EnableRetryOnFailure(5);
|
||||
builder.SchemaBehavior(MySqlSchemaBehavior.Translate, (name, objectName) => $"{name}_{objectName}");
|
||||
builder.MigrationsHistoryTable($"{Prefix}_MigrationHistory");
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Model.SetDefaultSchema(Prefix);
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Moonlight.ApiServer.Http.Middleware;
|
||||
|
||||
public class AuthenticationMiddleware
|
||||
{
|
||||
private readonly RequestDelegate Next;
|
||||
|
||||
public AuthenticationMiddleware(RequestDelegate next)
|
||||
{
|
||||
Next = next;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
36
Moonlight.ApiServer/Moonlight.ApiServer.csproj
Normal file
36
Moonlight.ApiServer/Moonlight.ApiServer.csproj
Normal file
@@ -0,0 +1,36 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.7"/>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="MoonCore" Version="1.5.7" />
|
||||
<PackageReference Include="MoonCore.Extended" Version="1.0.2" />
|
||||
<PackageReference Include="MoonCore.PluginFramework" Version="1.0.0" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Moonlight.Client\Moonlight.Client.csproj"/>
|
||||
<ProjectReference Include="..\Moonlight.Shared\Moonlight.Shared.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Database\Entities\" />
|
||||
<Folder Include="Database\Migrations\" />
|
||||
<Folder Include="Http\Controllers\" />
|
||||
<Folder Include="Models\" />
|
||||
<Folder Include="Services\" />
|
||||
<Folder Include="storage\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
110
Moonlight.ApiServer/Program.cs
Normal file
110
Moonlight.ApiServer/Program.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using MoonCore.Extended.Helpers;
|
||||
using MoonCore.Extensions;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.Services;
|
||||
using Moonlight.ApiServer.Configuration;
|
||||
using Moonlight.ApiServer.Database;
|
||||
using Moonlight.ApiServer.Helpers;
|
||||
|
||||
// Prepare file system
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage"));
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage", "plugins"));
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage", "clientPlugins"));
|
||||
Directory.CreateDirectory(PathBuilder.Dir("storage", "logs"));
|
||||
|
||||
// Configuration
|
||||
var configService = new ConfigService<AppConfiguration>(
|
||||
PathBuilder.File("storage", "config.json")
|
||||
);
|
||||
|
||||
ApplicationStateHelper.SetConfiguration(configService);
|
||||
|
||||
// Build pre run logger
|
||||
var providers = LoggerBuildHelper.BuildFromConfiguration(configuration =>
|
||||
{
|
||||
configuration.Console.Enable = true;
|
||||
configuration.Console.EnableAnsiMode = true;
|
||||
|
||||
configuration.FileLogging.Enable = true;
|
||||
configuration.FileLogging.Path = PathBuilder.File("storage", "logs", "moonlight.log");
|
||||
configuration.FileLogging.EnableLogRotation = true;
|
||||
configuration.FileLogging.RotateLogNameTemplate = PathBuilder.File("storage", "logs", "moonlight.log.{0}");
|
||||
});
|
||||
|
||||
using var loggerFactory = new LoggerFactory(providers);
|
||||
var logger = loggerFactory.CreateLogger("Startup");
|
||||
|
||||
// Fancy start console output... yes very fancy :>
|
||||
var rainbow = new Crayon.Rainbow(0.5);
|
||||
foreach (var c in "Moonlight")
|
||||
{
|
||||
Console.Write(
|
||||
rainbow
|
||||
.Next()
|
||||
.Bold()
|
||||
.Text(c.ToString())
|
||||
);
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Configure application logging
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddProviders(providers);
|
||||
|
||||
// Logging levels
|
||||
var logConfigPath = PathBuilder.File("storage", "logConfig.json");
|
||||
|
||||
// Ensure logging config, add a default one is missing
|
||||
if (!File.Exists(logConfigPath))
|
||||
{
|
||||
await File.WriteAllTextAsync(logConfigPath,
|
||||
"{\"LogLevel\":{\"Default\":\"Information\",\"Microsoft.AspNetCore\":\"Warning\"}}");
|
||||
}
|
||||
|
||||
builder.Logging.AddConfiguration(await File.ReadAllTextAsync(logConfigPath));
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddSingleton(configService);
|
||||
|
||||
// Database
|
||||
var databaseHelper = new DatabaseHelper(
|
||||
loggerFactory.CreateLogger<DatabaseHelper>()
|
||||
);
|
||||
|
||||
builder.Services.AddSingleton(databaseHelper);
|
||||
|
||||
builder.Services.AddDbContext<CoreDataContext>();
|
||||
databaseHelper.AddDbContext<CoreDataContext>();
|
||||
|
||||
databaseHelper.GenerateMappings();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
await databaseHelper.EnsureMigrated(scope.ServiceProvider);
|
||||
}
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
app.UseWebAssemblyDebugging();
|
||||
}
|
||||
|
||||
app.UseBlazorFrameworkFiles();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
app.MapControllers();
|
||||
|
||||
app.MapFallbackToFile("index.html");
|
||||
|
||||
app.Run();
|
||||
14
Moonlight.ApiServer/Properties/launchSettings.json
Normal file
14
Moonlight.ApiServer/Properties/launchSettings.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
|
||||
"applicationUrl": "http://localhost:5165",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user