feat: efcore & postgres setup
This commit is contained in:
parent
c282d42c61
commit
bb8e972a75
5 changed files with 56 additions and 23 deletions
|
@ -1,9 +1,11 @@
|
|||
namespace Server.DB.Documents;
|
||||
|
||||
using CouchDB.Driver.Types;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
public class Account : CouchDocument
|
||||
public class Account
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public Account(string username, byte[] password, string email, byte permissionLevel, byte[] salt)
|
||||
{
|
||||
this.Username = username;
|
||||
|
|
|
@ -1,14 +1,37 @@
|
|||
namespace Server.DB;
|
||||
|
||||
using CouchDB.Driver;
|
||||
using CouchDB.Driver.Options;
|
||||
using System.Data.Common;
|
||||
using Documents;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
|
||||
public class WonderkingContext : CouchContext
|
||||
public class WonderkingContext : DbContext
|
||||
{
|
||||
public WonderkingContext(CouchOptions<WonderkingContext> options) : base(options)
|
||||
private readonly ILoggerFactory loggerFactory;
|
||||
private readonly IConfiguration configuration;
|
||||
|
||||
public WonderkingContext(ILoggerFactory loggerFactory, IConfiguration configuration)
|
||||
{
|
||||
this.loggerFactory = loggerFactory;
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
public CouchDatabase<Account> Accounts { get; set; }
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
|
||||
optionsBuilder
|
||||
.UseNpgsql(
|
||||
$"Host={this.configuration["DB:Host"]};Username={this.configuration["DB:Username"]};Password={this.configuration["DB:Password"]};Database={this.configuration["DB:Database"]};Port={this.configuration["DB:Port"]}")
|
||||
.EnableSensitiveDataLogging().UseLoggerFactory(this.loggerFactory);
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder) =>
|
||||
modelBuilder.Entity<Account>(builder =>
|
||||
{
|
||||
builder.Property(b => b.Username).HasColumnType("varchar(20)");
|
||||
builder.Property(b => b.Password).HasColumnType("bytea");
|
||||
builder.Property(b => b.Salt).HasColumnType("bytea");
|
||||
builder.HasKey(b => b.Id);
|
||||
});
|
||||
|
||||
public DbSet<Account> Accounts { get; set; }
|
||||
}
|
||||
|
|
|
@ -2,10 +2,8 @@
|
|||
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using CouchDB.Driver.Query.Extensions;
|
||||
using DB;
|
||||
using DB.Documents;
|
||||
using DotNext;
|
||||
using Konscious.Security.Cryptography;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
@ -47,10 +45,12 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
|
|||
var finalAccount =
|
||||
await this.wonderkingContext.Accounts.AddAsync(new Account(packet.Username, Array.Empty<byte>(), "",
|
||||
0, argon2Id.Salt));
|
||||
argon2Id.AssociatedData = Guid.Parse(finalAccount.Id).ToByteArray();
|
||||
finalAccount.Password = await argon2Id.GetBytesAsync(128);
|
||||
await this.wonderkingContext.Accounts.AddOrUpdateAsync(finalAccount);
|
||||
await this.wonderkingContext.SaveChangesAsync();
|
||||
argon2Id.AssociatedData = finalAccount.Entity.Id.ToByteArray();
|
||||
finalAccount.Entity.Password = await argon2Id.GetBytesAsync(128);
|
||||
this.wonderkingContext.Accounts.Update(finalAccount.Entity);
|
||||
loginResponseReason = LoginResponseReason.Ok;
|
||||
await this.wonderkingContext.SaveChangesAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -62,7 +62,7 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
|
|||
else
|
||||
{
|
||||
argon2Id.Salt = account.Salt;
|
||||
argon2Id.AssociatedData = Guid.Parse(account.Id).ToByteArray();
|
||||
argon2Id.AssociatedData = account.Id.ToByteArray();
|
||||
var tempPasswordBytes = await argon2Id.GetBytesAsync(128);
|
||||
loginResponseReason = tempPasswordBytes.SequenceEqual(account.Password)
|
||||
? LoginResponseReason.Ok
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
#pragma warning disable AV1500
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using CouchDB.Driver.DependencyInjection;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
@ -16,13 +16,8 @@ var configurationRoot = builder.Configuration.AddJsonFile("settings.json", false
|
|||
builder.Services.AddLogging();
|
||||
builder.Logging.AddFile("Logs/Server-{Date}.log", LogLevel.Debug);
|
||||
builder.Logging.AddFile("Logs/Server-{Date}.json.log", LogLevel.Debug, isJson: true);
|
||||
builder.Services.AddCouchContext<WonderkingContext>(cfg =>
|
||||
{
|
||||
cfg.UseEndpoint(configurationRoot["DB:Endpoint"] ?? throw new InvalidOperationException())
|
||||
.UseBasicAuthentication(configurationRoot["DB:User"] ?? throw new InvalidOperationException(),
|
||||
configurationRoot["DB:Password"] ?? throw new InvalidOperationException())
|
||||
.EnsureDatabaseExists();
|
||||
});
|
||||
builder.Services.AddEntityFrameworkNpgsql();
|
||||
builder.Services.AddDbContext<WonderkingContext>();
|
||||
builder.Services.AddSingleton<PacketDistributorService>();
|
||||
builder.Services.AddHostedService<PacketDistributorService>(provider =>
|
||||
provider.GetService<PacketDistributorService>() ?? throw new InvalidOperationException());
|
||||
|
@ -36,5 +31,11 @@ builder.Services.AddHostedService<WonderkingAuthServer>(provider => new Wonderki
|
|||
provider.GetService<IServiceProvider>() ?? throw new InvalidOperationException()));
|
||||
|
||||
using var host = builder.Build();
|
||||
await using (var scope = host.Services.CreateAsyncScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<WonderkingContext>();
|
||||
await db.Database.MigrateAsync();
|
||||
}
|
||||
|
||||
await host.RunAsync();
|
||||
#pragma warning restore AV1500
|
||||
|
|
|
@ -17,8 +17,6 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CouchDB.NET" Version="3.4.0"/>
|
||||
<PackageReference Include="CouchDB.NET.DependencyInjection" Version="3.4.0"/>
|
||||
<PackageReference Include="DotNext" Version="4.13.1" />
|
||||
<PackageReference Include="DotNext.IO" Version="4.13.1" />
|
||||
<PackageReference Include="DotNext.Metaprogramming" Version="4.13.1" />
|
||||
|
@ -36,6 +34,14 @@
|
|||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="7.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.10">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0"/>
|
||||
|
@ -49,6 +55,7 @@
|
|||
</PackageReference>
|
||||
<PackageReference Include="NetCoreServer" Version="7.0.0"/>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
|
||||
<PackageReference Include="Nullable.Extended.Analyzer" Version="1.10.4539">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
|
Loading…
Reference in a new issue