diff --git a/.gitignore b/.gitignore
index 8d2808a3..3290a6b1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,7 +11,6 @@ Gridly-Client/publish/
Gridly-Client/dist
Gridly-Client/package-lock.json
.vs/*
-Gridly.csproj.lscache
# Node
Gridly-Client/node_modules
@@ -26,6 +25,8 @@ yarn-error.log
*.launch
.settings/
*.sublime-workspace
+Gridly.csproj.lscache
+Gridly.csproj.user
# Visual Studio Code
!.vscode/settings.json
diff --git a/Data/DbInitializer.cs b/Data/DbInitializer.cs
index 231f0de4..666ccb85 100644
--- a/Data/DbInitializer.cs
+++ b/Data/DbInitializer.cs
@@ -1,15 +1,15 @@
using Dapper;
-using Microsoft.Data.Sqlite;
+using System.Data;
namespace Gridly.Data;
public class DbInitializer
{
- private SqliteConnection connection;
+ private readonly IDbConnection connection;
- public DbInitializer(IConfiguration configuration)
+ public DbInitializer(IDbConnection connection)
{
- connection = new SqliteConnection(configuration.GetConnectionString("GridlyDb"));
+ this.connection = connection;
}
public async Task EnsureTablesCreatedAsync()
@@ -47,4 +47,4 @@ CREATE TABLE IF NOT EXISTS Settings(
FOREIGN KEY(CardId) REFERENCES Card(Id));",
commandTimeout:150);
}
-}
\ No newline at end of file
+}
diff --git a/Gridly.csproj b/Gridly.csproj
index 54a0d5a7..c9694107 100644
--- a/Gridly.csproj
+++ b/Gridly.csproj
@@ -41,6 +41,17 @@
+
+
+ PreserveNewest
+ PreserveNewest
+
+
+ PreserveNewest
+ PreserveNewest
+
+
+
diff --git a/Program.cs b/Program.cs
index 922b271c..f3c33fb1 100644
--- a/Program.cs
+++ b/Program.cs
@@ -3,24 +3,31 @@
using Gridly.Repositories;
using Gridly.Services;
using Gridly.Data;
-using Microsoft.Data.Sqlite;
-using Microsoft.Extensions.FileProviders;
+using System.Data;
-var builder = WebApplication.CreateBuilder(args);
+var appDirectory = Path.GetDirectoryName(Environment.ProcessPath) ?? AppContext.BaseDirectory;
+Directory.SetCurrentDirectory(appDirectory);
+
+var builder = WebApplication.CreateBuilder(new WebApplicationOptions
+{
+ Args = args,
+ ContentRootPath = appDirectory
+});
builder.Services.AddControllersWithViews();
builder.Services.AddControllers();
await builder.Services.AddTokenBucketRateLimiter();
builder.Services.AddScoped();
-builder.Services.AddScoped(sp =>
- new SqliteConnection(builder.Configuration.GetConnectionString("GridlyDb")));
+builder.Services.AddScoped(sp =>
+ sp.GetRequiredService().CreateConnection());
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
+builder.Services.AddSingleton();
builder.Services.AddSingleton();
builder.Services.AddSingleton();
builder.Services.AddSingleton();
@@ -32,12 +39,6 @@
app.UseStaticFiles();
-app.UseStaticFiles(new StaticFileOptions
-{
- FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Assets/Icons")),
- RequestPath = "/Assets/Icons"
-});
-
app.MapDefaultControllerRoute().RequireRateLimiting("fixed");
app.MapControllerRoute(
@@ -59,4 +60,4 @@
endpoints.MapFallbackToFile("index.html");
});
-app.Run();
\ No newline at end of file
+app.Run();
diff --git a/Services/DbConnectionServices.cs b/Services/DbConnectionServices.cs
new file mode 100644
index 00000000..5555d882
--- /dev/null
+++ b/Services/DbConnectionServices.cs
@@ -0,0 +1,37 @@
+using System.Data;
+using Microsoft.Data.Sqlite;
+
+namespace Gridly.Data;
+
+public class DbConnectionServices : IDbConnectionServices
+{
+ private readonly string connectionString;
+
+ public DbConnectionServices(IConfiguration configuration, IHostEnvironment environment)
+ {
+ var configuredConnectionString = configuration.GetConnectionString("GridlyDb");
+ if (string.IsNullOrWhiteSpace(configuredConnectionString))
+ {
+ throw new InvalidOperationException("Connection string 'GridlyDb' is missing.");
+ }
+
+ var sqliteBuilder = new SqliteConnectionStringBuilder(configuredConnectionString);
+ if (!Path.IsPathRooted(sqliteBuilder.DataSource))
+ {
+ sqliteBuilder.DataSource = Path.Combine(environment.ContentRootPath, sqliteBuilder.DataSource);
+ }
+
+ var dbDirectory = Path.GetDirectoryName(sqliteBuilder.DataSource);
+ if (!string.IsNullOrWhiteSpace(dbDirectory))
+ {
+ Directory.CreateDirectory(dbDirectory);
+ }
+
+ connectionString = sqliteBuilder.ConnectionString;
+ }
+
+ public IDbConnection CreateConnection()
+ {
+ return new SqliteConnection(connectionString);
+ }
+}
diff --git a/Services/IDbConnectionServices.cs b/Services/IDbConnectionServices.cs
new file mode 100644
index 00000000..f0737ddf
--- /dev/null
+++ b/Services/IDbConnectionServices.cs
@@ -0,0 +1,7 @@
+using System.Data;
+namespace Gridly.Data;
+
+public interface IDbConnectionServices
+{
+ public IDbConnection CreateConnection();
+}