using TarninaHub.Services; using TarninaHub.Settings; var builder = WebApplication.CreateBuilder(args); var surrealSettings = builder.Configuration.GetSection("SurrealDb").Get() ?? new SurrealDbSettings(); builder.Services.Configure(builder.Configuration.GetSection("SurrealDb")); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); builder.Services.AddControllers(); builder.Services.AddSingleton(); 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(); await storageService.InitializeAsync(CancellationToken.None); } // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.MapOpenApi(); } else { app.UseHttpsRedirection(); } app.UseDefaultFiles(); app.UseStaticFiles(); 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('\\', '/')}"; }