feat(backend): integrate SurrealDB storage with embedded RocksDB
Add StorageService backed by SurrealDB using the embedded RocksDB provider that persists to ./data (never in memory). Support an optional remote server via SurrealDb:RemoteUrl for frontend sync. Expose readiness and storage probe endpoints under /api/system and add a helper script to start a local SurrealDB server. Set the backend version to 0.0.1.
This commit is contained in:
@@ -1,12 +1,55 @@
|
||||
using TarninaHub.Services;
|
||||
using TarninaHub.Settings;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var surrealSettings = builder.Configuration.GetSection("SurrealDb").Get<SurrealDbSettings>()
|
||||
?? new SurrealDbSettings();
|
||||
|
||||
builder.Services.Configure<SurrealDbSettings>(builder.Configuration.GetSection("SurrealDb"));
|
||||
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.AddSingleton<IStorageService, StorageService>();
|
||||
|
||||
var surrealOptions = new SurrealDbOptions
|
||||
{
|
||||
Endpoint = BuildEndpoint(surrealSettings),
|
||||
Namespace = surrealSettings.Namespace,
|
||||
Database = surrealSettings.Database,
|
||||
};
|
||||
|
||||
if (string.IsNullOrWhiteSpace(surrealSettings.RemoteUrl))
|
||||
{
|
||||
// Embedded RocksDB, persisted next to the running app (./data), never in memory.
|
||||
var dataDirectory = Path.Combine(AppContext.BaseDirectory, surrealSettings.DataDirectory);
|
||||
Directory.CreateDirectory(dataDirectory);
|
||||
|
||||
builder.Services
|
||||
.AddSurreal(surrealOptions, ServiceLifetime.Singleton)
|
||||
.AddRocksDbProvider();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Remote SurrealDB server on its own dedicated port (used for sync with the frontend).
|
||||
surrealOptions.Username = surrealSettings.Username;
|
||||
surrealOptions.Password = surrealSettings.Password;
|
||||
|
||||
builder.Services.AddSurreal(surrealOptions, ServiceLifetime.Singleton);
|
||||
}
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var storageService = scope.ServiceProvider.GetRequiredService<IStorageService>();
|
||||
|
||||
await storageService.InitializeAsync(CancellationToken.None);
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
@@ -25,3 +68,15 @@ app.MapControllers();
|
||||
app.MapFallbackToFile("index.html");
|
||||
|
||||
app.Run();
|
||||
|
||||
static string BuildEndpoint(SurrealDbSettings settings)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(settings.RemoteUrl))
|
||||
{
|
||||
return settings.RemoteUrl;
|
||||
}
|
||||
|
||||
var dataDirectory = Path.Combine(AppContext.BaseDirectory, settings.DataDirectory);
|
||||
|
||||
return $"rocksdb://{dataDirectory.Replace('\\', '/')}";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user