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.
58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
using Microsoft.Extensions.Options;
|
|
using SurrealDb.Net;
|
|
using TarninaHub.Models;
|
|
using TarninaHub.Settings;
|
|
|
|
namespace TarninaHub.Services;
|
|
|
|
public sealed class StorageService : IStorageService
|
|
{
|
|
private const string ProbeTable = "storage_probe";
|
|
|
|
private readonly ISurrealDbClient _client;
|
|
private readonly SurrealDbSettings _settings;
|
|
private readonly string _dataDirectory;
|
|
|
|
public StorageService(ISurrealDbClient client, IOptions<SurrealDbSettings> settings)
|
|
{
|
|
_client = client;
|
|
_settings = settings.Value;
|
|
_dataDirectory = Path.Combine(AppContext.BaseDirectory, _settings.DataDirectory);
|
|
}
|
|
|
|
public async Task InitializeAsync(CancellationToken cancellationToken)
|
|
{
|
|
await _client.RawQuery(
|
|
$"DEFINE TABLE IF NOT EXISTS {ProbeTable} SCHEMALESS;",
|
|
cancellationToken: cancellationToken);
|
|
}
|
|
|
|
public StorageInfo GetInfo()
|
|
{
|
|
var isRemote = !string.IsNullOrWhiteSpace(_settings.RemoteUrl);
|
|
|
|
return new StorageInfo(
|
|
isRemote ? "remote" : "rocksdb",
|
|
isRemote ? _settings.RemoteUrl! : _dataDirectory,
|
|
_settings.Namespace,
|
|
_settings.Database);
|
|
}
|
|
|
|
public async Task<int> GetProbeCountAsync(CancellationToken cancellationToken)
|
|
{
|
|
var probes = await _client.Select<StorageProbe>(ProbeTable, cancellationToken);
|
|
|
|
return probes.Count();
|
|
}
|
|
|
|
public Task<StorageProbe> AddProbeAsync(CancellationToken cancellationToken)
|
|
{
|
|
var probe = new StorageProbe
|
|
{
|
|
CreatedAt = DateTimeOffset.UtcNow,
|
|
};
|
|
|
|
return _client.Create(ProbeTable, probe, cancellationToken);
|
|
}
|
|
}
|