1
0

feat: Add database models and setting feature.

This commit is contained in:
Ca2didi 2025-03-21 13:37:22 +08:00
parent 337839c9d8
commit 9fcb843b9d
14 changed files with 66 additions and 40 deletions

View File

@ -4,6 +4,7 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AClaimsPrincipal_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fbf5ce3f8ae0647439d514bb1a3c7f96b13600_003F20_003Fabeaf9ae_003FClaimsPrincipal_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AClaimTypes_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fbf5ce3f8ae0647439d514bb1a3c7f96b13600_003Fbd_003F4cde67a5_003FClaimTypes_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003APath_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa6b7f037ba7b44df80b8d3aa7e58eeb2e8e938_003F28_003F6a41ec86_003FPath_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AServiceProvider_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fce37be1a06b16c6faa02038d2cc477dd3bca5b217ceeb41c5f2ad45c1bf9_003FServiceProvider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AStringValue_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fee39d1e9346e41aa9d44f0e1b1c6630f76268_003F49_003Fb92346b2_003FStringValue_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ATestMethodInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F5ef53d675c5d34a6b85963919015dc0c1b06e5ea9834aac59ae6911f4c6f38_003FTestMethodInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=b5573ef9_002Db554_002D4a56_002D82c4_002D2531c8feef65/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" IsLocked="True" Name="PathValidationTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;

View File

@ -1,6 +0,0 @@
namespace Flawless.Core.BinaryDataFormat;
public static class DataInserter
{
}

View File

@ -5,7 +5,7 @@ using Nerdbank.Streams;
namespace Flawless.Core.BinaryDataFormat;
public static class DataExtractor
public static class DataTransformer
{
public static byte GuessStandardDepotHeaderVersion(Stream depotStream)
{
@ -17,16 +17,7 @@ public static class DataExtractor
return (byte)val;
}
public static byte GuessNetworkDepotHeaderVersion(Stream depotStream)
{
var val = depotStream.ReadByte();
depotStream.Seek(-1, SeekOrigin.Current);
if (val < 0) ;
return (byte)val;
}
public static StandardDepotHeaderV1 ExtractStandardDepotHeaderV1(Stream depotStream)
{

View File

@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="Nerdbank.Streams" Version="2.11.86" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SQLite" Version="3.13.0" />
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
<PackageReference Include="System.IO.Hashing" Version="9.0.3" />
</ItemGroup>

View File

@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Mvc;
namespace Flawless.Server.Controllers;
[ApiController, Route("api/auth")]
public class AuthenticationController(FlawlessContext dbContext, ILogger<AuthenticationController> logger)
public class AuthenticationController(GlobalContext dbContext, ILogger<AuthenticationController> logger)
: ControllerBase
{

View File

@ -5,13 +5,15 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2"/>
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.0-preview.2.25163.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="8.0.0" />

View File

@ -1,10 +0,0 @@
using Microsoft.EntityFrameworkCore;
namespace Flawless.Server.Models;
public class FlawlessContext : DbContext
{
public FlawlessContext(DbContextOptions<FlawlessContext> options) : base(options)
{
}
}

View File

@ -1,5 +0,0 @@
namespace Flawless.Server.Models;
public class FlawlessStorageContext(WebApplication app)
{
}

View File

@ -0,0 +1,10 @@
using Microsoft.EntityFrameworkCore;
namespace Flawless.Server.Models;
public class GlobalContext : DbContext
{
public GlobalContext(DbContextOptions<GlobalContext> options) : base(options)
{
}
}

View File

@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;
namespace Flawless.Server.Models;
public class RepositoryContext : DbContext
{
private readonly string RepositoryPath;
public RepositoryContext(IConfiguration config, DbContextOptions<GlobalContext> options) : base(options)
{
var settings = config.GetSection("Settings").Get<Settings>();
RepositoryPath = settings?.DataStoragePath ?? "./Data";
if (!Directory.Exists(RepositoryPath))
Directory.CreateDirectory(RepositoryPath);
}
}

View File

@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
namespace Flawless.Server.Models;
public class RepositoryContextFactory : DbContextFactory<RepositoryContext>
{
public RepositoryContextFactory(IServiceProvider serviceProvider, DbContextOptions<RepositoryContext> options, IDbContextFactorySource<RepositoryContext> factorySource)
: base(serviceProvider, options, factorySource)
{
}
}

View File

@ -10,6 +10,7 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSingleton<RepositoryContext>();
builder.Services.AddSwaggerGen(opt =>
{
opt.DocInclusionPredicate((name, api) => api.HttpMethod != null); // Filter out WebSocket methods
@ -27,10 +28,10 @@ builder.Services.AddAuthentication(opt =>
});
// Data connection related.
builder.Services.AddSingleton<WebSocketTransferController>();
builder.Services.AddDbContext<FlawlessContext>(opt =>
builder.Services.AddDbContextFactory<RepositoryContext, RepositoryContextFactory>();
builder.Services.AddDbContext<GlobalContext>(opt =>
{
opt.UseInMemoryDatabase("Flawless");
opt.UseInMemoryDatabase("Main");
});
var app = builder.Build();
@ -38,12 +39,12 @@ var app = builder.Build();
app.UseRouting();
// Config WebSocket support.
app.UseWebSocketHandoffMiddleware();
app.UseWebSockets(new WebSocketOptions
{
KeepAliveInterval = TimeSpan.FromSeconds(60),
KeepAliveTimeout = TimeSpan.FromSeconds(300),
});
app.UseWebSocketHandoffMiddleware();
// Configure identity control
app.UseAuthentication();

View File

@ -0,0 +1,9 @@
namespace Flawless.Server;
public sealed class Settings
{
// Local settings
public required string DataStoragePath { get; set; } = "./Data";
}

View File

@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Settings": {
"DataStoragePath": "/Users/hcm-b0485/Desktop/test"
}
}