add chat base

This commit is contained in:
sam 2024-05-14 22:24:53 +02:00
parent a7b25c42f1
commit 996e59f49a
9 changed files with 167 additions and 3 deletions

View file

@ -0,0 +1,52 @@
using Foxchat.Core;
using Foxchat.Core.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace Foxchat.Chat.Database;
public class ChatContext : IDatabaseContext
{
private readonly string _connString;
public override DbSet<Instance> Instance { get; set; }
public ChatContext(InstanceConfig config)
{
_connString = new Npgsql.NpgsqlConnectionStringBuilder(config.Database.Url)
{
Timeout = config.Database.Timeout ?? 5,
MaxPoolSize = config.Database.MaxPoolSize ?? 50,
}.ConnectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql(_connString)
.UseSnakeCaseNamingConvention();
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
// ULIDs are stored as UUIDs in the database
configurationBuilder.Properties<Ulid>().HaveConversion<UlidConverter>();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
public class DesignTimeIdentityContextFactory : IDesignTimeDbContextFactory<ChatContext>
{
public ChatContext CreateDbContext(string[] args)
{
// Read the configuration file
var config = new ConfigurationBuilder()
.AddConfiguration("identity.ini")
.Build()
// Get the configuration as our config class
.Get<InstanceConfig>() ?? new();
return new ChatContext(config);
}
}

View file

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.5" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Foxchat.Core\Foxchat.Core.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,3 @@
global using ILogger = Serilog.ILogger;
global using Log = Serilog.Log;
global using NUlid;

View file

@ -0,0 +1,5 @@
using Foxchat.Core;
namespace Foxchat.Chat;
public class InstanceConfig : CoreConfig;

53
Foxchat.Chat/Program.cs Normal file
View file

@ -0,0 +1,53 @@
using Newtonsoft.Json.Serialization;
using Serilog;
using Foxchat.Core;
using Foxchat.Chat;
using Foxchat.Chat.Database;
var builder = WebApplication.CreateBuilder(args);
var config = builder.AddConfiguration<InstanceConfig>("chat.ini");
builder.Services.AddSerilog(config.LogEventLevel);
await BuildInfo.ReadBuildInfo();
Log.Information("Starting Foxchat.Chat {Version} ({Hash})", BuildInfo.Version, BuildInfo.Hash);
builder.Services
.AddControllers()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
});
builder.Services
.AddCoreServices<ChatContext>()
.AddEndpointsApiExplorer()
.AddSwaggerGen();
var app = builder.Build();
app.UseSerilogRequestLogging();
app.UseRouting();
app.UseSwagger();
app.UseSwaggerUI();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
using (var scope = app.Services.CreateScope())
using (var context = scope.ServiceProvider.GetRequiredService<ChatContext>())
{
Log.Information("Initializing instance keypair...");
if (await context.InitializeInstanceAsync())
{
Log.Information("Initialized instance keypair");
}
}
app.Urls.Clear();
app.Urls.Add(config.Address);
app.Run();

15
Foxchat.Chat/chat.ini Normal file
View file

@ -0,0 +1,15 @@
Host = localhost
Port = 7610
Domain = chat.fox.localhost
; The level to log things at. Valid settings: Verbose, Debug, Information, Warning, Error, Fatal
LogEventLevel = Debug
[Database]
; The database URL in ADO.NET format.
Url = "Host=localhost;Database=foxchat_cs_chat;Username=foxchat;Password=password"
; The timeout for opening new connections. Defaults to 5.
Timeout = 5
; The maximum number of open connections. Defaults to 50.
MaxPoolSize = 500

View file

@ -0,0 +1,3 @@
global using ILogger = Serilog.ILogger;
global using Log = Serilog.Log;
global using NUlid;

View file

@ -2,6 +2,4 @@ using Foxchat.Core;
namespace Foxchat.Identity;
public class InstanceConfig : CoreConfig
{
}
public class InstanceConfig : CoreConfig;

View file

@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Foxchat.Identity", "Foxchat
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Foxchat.Core", "Foxchat.Core\Foxchat.Core.csproj", "{06352B8B-628C-4476-9F78-83F326B05B16}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Foxchat.Chat", "Foxchat.Chat\Foxchat.Chat.csproj", "{9DC99C95-55DD-4091-9F2B-92B6BCF9D980}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -24,5 +26,9 @@ Global
{06352B8B-628C-4476-9F78-83F326B05B16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06352B8B-628C-4476-9F78-83F326B05B16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06352B8B-628C-4476-9F78-83F326B05B16}.Release|Any CPU.Build.0 = Release|Any CPU
{9DC99C95-55DD-4091-9F2B-92B6BCF9D980}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9DC99C95-55DD-4091-9F2B-92B6BCF9D980}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9DC99C95-55DD-4091-9F2B-92B6BCF9D980}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9DC99C95-55DD-4091-9F2B-92B6BCF9D980}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal