Skip to content

Commit

Permalink
Settings improved, Database section introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-17 committed Oct 18, 2024
1 parent ebeea85 commit 7db2efa
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
{
var inMemorySettings = new Dictionary<string, string?>
{
{ "ConnectionStrings:GibbonGitServerContext", $"Data Source={_fileName}" },
{ "AppSettings:AllowDatabaseMigration", "true" }
{ "Database:Provider", "Sqlite" },
{ "Database:AllowMigration", "true" },
{ "Database:ConnectionStrings:Default", $"Data Source={_fileName}" }
};
config.AddInMemoryCollection(inMemorySettings);
});
Expand Down
3 changes: 0 additions & 3 deletions Gibbon.Git.Server/Configuration/ApplicationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

public sealed record ApplicationSettings
{
public bool AllowDbReset { get; set; }
public bool DemoModeActive { get; set; }
public string DataPath { get; set; }
public string RepositoryPath { get; set; }
public bool AllowDatabaseMigration { get; set; }
public DatabaseProviderTypes DatabaseProvider { get; set; }
}
7 changes: 7 additions & 0 deletions Gibbon.Git.Server/Configuration/DatabaseSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Gibbon.Git.Server.Configuration;

public sealed record DatabaseSettings
{
public bool AllowReset { get; set; }
public bool AllowMigration { get; set; }
}
36 changes: 16 additions & 20 deletions Gibbon.Git.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,41 @@
.AddEnvironmentVariables()
.AddCommandLine(args);

services.Configure<ApplicationSettings>(config.GetSection("AppSettings"));
services.Configure<GitSettings>(config.GetSection("GitSettings"));
services.Configure<MailSettings>(config.GetSection("MailSettings"));
var databaseSection = config.GetSection("Database");
services.Configure<DatabaseSettings>(databaseSection);
services.Configure<ApplicationSettings>(config.GetSection("Application"));
services.Configure<GitSettings>(config.GetSection("Git"));
services.Configure<MailSettings>(config.GetSection("Mail"));

services.AddScoped(serviceProvider => serviceProvider.GetRequiredService<IServerSettingsService>().GetSettings());

var databaseProvider = databaseSection.GetValue<DatabaseProviderTypes>("Provider");
var connectionString = databaseSection.GetConnectionString($"{databaseProvider}") ?? databaseSection.GetConnectionString("Default");

var connectionString = builder.Configuration.GetConnectionString("GibbonContext");

var databaseProvider = builder.Configuration.GetSection("AppSettings").Get<ApplicationSettings>().DatabaseProvider;
DbContextOptionsBuilder ConfigureOptions(DbContextOptionsBuilder options)
{
options.ConfigureWarnings(w => w.Throw(RelationalEventId.MultipleCollectionIncludeWarning));
return options;
}

switch (databaseProvider)
{
case DatabaseProviderTypes.Memory:
services.AddDbContext<GibbonGitServerContext, SqlServerGibbonContext>(options =>
{
options.UseInMemoryDatabase(databaseName: connectionString ?? "InMemory");
options.ConfigureWarnings(w => w.Throw(RelationalEventId.MultipleCollectionIncludeWarning));
});
using (var scope = services.BuildServiceProvider().CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<GibbonGitServerContext>();
context.Database.EnsureCreated();
}
ConfigureOptions(options).UseInMemoryDatabase(connectionString ?? "Data Source=InMemory;");
});
break;
case DatabaseProviderTypes.Sqlite:
connectionString ??= builder.Configuration.GetConnectionString("SqliteGibbonContext");
services.AddDbContext<GibbonGitServerContext, SqliteGibbonContext>(options =>
{
options.UseSqlite(connectionString);
options.ConfigureWarnings(w => w.Throw(RelationalEventId.MultipleCollectionIncludeWarning));
ConfigureOptions(options).UseSqlite(connectionString);
});
break;
case DatabaseProviderTypes.SqlServer:
connectionString ??= builder.Configuration.GetConnectionString("SqlServerGibbonContext");
services.AddDbContext<GibbonGitServerContext, SqlServerGibbonContext>(options =>
{
options.UseSqlServer(connectionString);
options.ConfigureWarnings(w => w.Throw(RelationalEventId.MultipleCollectionIncludeWarning));
ConfigureOptions(options).UseSqlServer(connectionString);
});
break;
}
Expand Down
22 changes: 13 additions & 9 deletions Gibbon.Git.Server/Services/Hosted/DatabaseMigrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,36 @@

namespace Gibbon.Git.Server.Services.Hosted;

public class DatabaseMigrationService(ILogger<DatabaseMigrationService> logger, IOptions<ApplicationSettings> options, IServiceProvider serviceProvider)
public class DatabaseMigrationService(ILogger<DatabaseMigrationService> logger, IOptions<DatabaseSettings> options, IServiceProvider serviceProvider)
: IHostedService
{
private readonly ILogger<DatabaseMigrationService> _logger = logger;
private readonly ApplicationSettings _options = options.Value;
private readonly DatabaseSettings _options = options.Value;
private readonly IServiceProvider _serviceProvider = serviceProvider;

public async Task StartAsync(CancellationToken cancellationToken)
{
if (!_options.AllowDatabaseMigration)
using IServiceScope scope = _serviceProvider.CreateScope();
await using var context = scope.ServiceProvider.GetRequiredService<GibbonGitServerContext>();

await context.Database.EnsureCreatedAsync(cancellationToken);

if (!_options.AllowMigration)
{
_logger.LogInformation("Database migration is disabled.");
return;
}

_logger.LogInformation("Migrating database...");
using IServiceScope scope = _serviceProvider.CreateScope();
await using var context = scope.ServiceProvider.GetRequiredService<GibbonGitServerContext>();

var migrations = (await context.Database.GetPendingMigrationsAsync(cancellationToken)).ToList();
_logger.LogInformation("Migrating database...");
var migrations = await context.Database.GetPendingMigrationsAsync(cancellationToken);
var count = 0;
foreach (var migration in migrations)
{
count++;
_logger.LogInformation("Pending migration: {Migration}", migration);
}

if (migrations.Count == 0)
if (count == 0)
{
_logger.LogInformation("No pending migrations.");
return;
Expand Down
29 changes: 16 additions & 13 deletions Gibbon.Git.Server/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
{
"AppSettings": {
"DataPath": "~\\App_Data",
"Application": {
"DataPath": "App_Data",
"DemoModeActive": false,
"AllowDbReset": true,
"AllowDatabaseMigration": false,
"RepositoryPath": "~\\App_Data\\Repositories",
"DatabaseProvider": "Memory"
"RepositoryPath": "App_Data\\Repositories"
},
"GitSettings": {
"BinaryPath": "~\\App_Data\\Git",
"HomePath": "~\\App_Data",
"Database": {
"Provider": "Memory",
"AllowReset": true,
"AllowMigration": false,
"ConnectionStrings": {
"Default": "Data Source=Gibbon.db"
}
},
"Git": {
"BinaryPath": "App_Data\\Git",
"HomePath": "App_Data",
"Version": "2.6.1",
"Architecture": "32"
},
"MailSettings": {
"Mail": {
"Host": "",
"Port": 587,
"Name": "Your sender name",
"Username": "Your username",
"Password": "Your password"
},
"ConnectionStrings": {
"GibbonContext": "MemoryDatabase"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"DetailedErrors": true
}
}
10 changes: 5 additions & 5 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The MIT License (MIT)
MIT License

Copyright (c) 2024 Matthias Voigt
Copyright (c) 2013 Jakub Chodounsky
Expand All @@ -10,13 +10,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0 comments on commit 7db2efa

Please sign in to comment.