Split the backend into API host, Domain, Application and Infrastructure projects. Add Swagger/OpenAPI per API version, Asp.Versioning (query/header), ProblemDetails with a global exception handler and SurrealQL migrations applied at startup (namespace/database ensured). Infrastructure owns the SurrealDB client, settings and DI.
53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using SurrealDb.Net;
|
|
using TarninaHub.Application.Abstractions;
|
|
using TarninaHub.Infrastructure.Persistence;
|
|
using TarninaHub.Infrastructure.Settings;
|
|
|
|
namespace TarninaHub.Infrastructure;
|
|
|
|
public static class DependencyInjection
|
|
{
|
|
public static IServiceCollection AddInfrastructure(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
services.Configure<SurrealDbSettings>(configuration.GetSection("SurrealDb"));
|
|
|
|
var settings = configuration.GetSection("SurrealDb").Get<SurrealDbSettings>()
|
|
?? new SurrealDbSettings();
|
|
|
|
var options = new SurrealDbOptions
|
|
{
|
|
Endpoint = SurrealDbEndpoint.Build(settings),
|
|
Namespace = settings.Namespace,
|
|
Database = settings.Database,
|
|
};
|
|
|
|
if (string.IsNullOrWhiteSpace(settings.RemoteUrl))
|
|
{
|
|
// Embedded RocksDB, persisted next to the running app (./data), never in memory.
|
|
var dataDirectory = Path.Combine(AppContext.BaseDirectory, settings.DataDirectory);
|
|
Directory.CreateDirectory(dataDirectory);
|
|
|
|
services
|
|
.AddSurreal(options, ServiceLifetime.Singleton)
|
|
.AddRocksDbProvider();
|
|
}
|
|
else
|
|
{
|
|
// Remote SurrealDB server on its own dedicated port (used for sync with the frontend).
|
|
options.Username = settings.Username;
|
|
options.Password = settings.Password;
|
|
|
|
services.AddSurreal(options, ServiceLifetime.Singleton);
|
|
}
|
|
|
|
services.AddSingleton<IStorageService, StorageService>();
|
|
services.AddSingleton<IMigrationRunner, MigrationRunner>();
|
|
|
|
return services;
|
|
}
|
|
}
|