From c63549fa93213815b2444febe421dca1d04afd52 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Sat, 7 Apr 2018 14:42:32 -0400 Subject: [PATCH] More database context stuff --- build/Dockerfile | 6 ++ .../Properties/launchSettings.json | 20 ++++ .../Configuration/DatabaseConfiguration.cs | 8 ++ .../Configuration/DatabaseType.cs | 9 ++ src/Tgstation.Server.Host/Core/Application.cs | 13 ++- .../Models/DatabaseContext.cs | 97 +++++++++++++++++++ .../Models/IDatabaseContext.cs | 12 ++- src/Tgstation.Server.Host/Models/Instance.cs | 5 + .../Tgstation.Server.Host.csproj | 18 ++++ .../appsettings.Development.json | 24 +++++ .../appsettings.Docker.json | 6 ++ src/Tgstation.Server.Host/appsettings.json | 24 +++++ 12 files changed, 235 insertions(+), 7 deletions(-) create mode 100644 src/Tgstation.Server.Host.Console/Properties/launchSettings.json create mode 100644 src/Tgstation.Server.Host/Configuration/DatabaseConfiguration.cs create mode 100644 src/Tgstation.Server.Host/Configuration/DatabaseType.cs create mode 100644 src/Tgstation.Server.Host/Models/DatabaseContext.cs create mode 100644 src/Tgstation.Server.Host/appsettings.Development.json create mode 100644 src/Tgstation.Server.Host/appsettings.Docker.json create mode 100644 src/Tgstation.Server.Host/appsettings.json diff --git a/build/Dockerfile b/build/Dockerfile index 5332fa7e91..b0f90f3cad 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -38,6 +38,12 @@ RUN dotnet publish -c Release -o /app FROM microsoft/dotnet:2.1-runtime EXPOSE 80 + WORKDIR /app + COPY --from=publish /app . + +RUN mkdir /config_data && mv appsettings.Docker.json /config_data/appsettings.Production.json +VOLUME ["/config_data"] + ENTRYPOINT ["dotnet", "Tgstation.Server.Host.Console.dll"] diff --git a/src/Tgstation.Server.Host.Console/Properties/launchSettings.json b/src/Tgstation.Server.Host.Console/Properties/launchSettings.json new file mode 100644 index 0000000000..df4509a62a --- /dev/null +++ b/src/Tgstation.Server.Host.Console/Properties/launchSettings.json @@ -0,0 +1,20 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:51882/", + "sslPort": 0 + } + }, + "profiles": { + "Tgstation.Server.Host.Console": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:51885/" + } + } +} \ No newline at end of file diff --git a/src/Tgstation.Server.Host/Configuration/DatabaseConfiguration.cs b/src/Tgstation.Server.Host/Configuration/DatabaseConfiguration.cs new file mode 100644 index 0000000000..b356408c50 --- /dev/null +++ b/src/Tgstation.Server.Host/Configuration/DatabaseConfiguration.cs @@ -0,0 +1,8 @@ +namespace Tgstation.Server.Host.Configuration +{ + sealed class DatabaseConfiguration + { + public DatabaseType DatabaseType { get; set; } + public string ConnectionString { get; set; } + } +} diff --git a/src/Tgstation.Server.Host/Configuration/DatabaseType.cs b/src/Tgstation.Server.Host/Configuration/DatabaseType.cs new file mode 100644 index 0000000000..919c82ed47 --- /dev/null +++ b/src/Tgstation.Server.Host/Configuration/DatabaseType.cs @@ -0,0 +1,9 @@ +namespace Tgstation.Server.Host.Configuration +{ + enum DatabaseType + { + SqlServer, + MySql, + Sqlite + } +} diff --git a/src/Tgstation.Server.Host/Core/Application.cs b/src/Tgstation.Server.Host/Core/Application.cs index 1c4cf52f9c..e568a1526f 100644 --- a/src/Tgstation.Server.Host/Core/Application.cs +++ b/src/Tgstation.Server.Host/Core/Application.cs @@ -1,10 +1,12 @@ -using Microsoft.AspNetCore.Builder; +using Cyberboss.AspNetCore.AsyncInitializer; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Globalization; +using Tgstation.Server.Host.Models; namespace Tgstation.Server.Host.Core { @@ -35,10 +37,15 @@ namespace Tgstation.Server.Host.Core if (services == null) throw new ArgumentNullException(nameof(services)); + services.Configure(configuration.GetSection("Database")); + services.AddMvc(); services.AddOptions(); services.AddLocalization(); - } + + services.AddDbContext(); + services.AddScoped(x => x.GetRequiredService()); + } /// /// Configure the @@ -70,6 +77,8 @@ namespace Tgstation.Server.Host.Core SupportedUICultures = supportedCultures, }); + applicationBuilder.UseAsyncInitialization((databaseContext, cancellationToken) => databaseContext.Initialize(cancellationToken)); + applicationBuilder.UseSystemAuthentication(); applicationBuilder.UseMvc(); diff --git a/src/Tgstation.Server.Host/Models/DatabaseContext.cs b/src/Tgstation.Server.Host/Models/DatabaseContext.cs new file mode 100644 index 0000000000..6eaabd13b4 --- /dev/null +++ b/src/Tgstation.Server.Host/Models/DatabaseContext.cs @@ -0,0 +1,97 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System; +using System.Threading; +using System.Threading.Tasks; +using Tgstation.Server.Host.Configuration; +using ZNetCS.AspNetCore.Logging.EntityFrameworkCore; + +namespace Tgstation.Server.Host.Models +{ + sealed class DatabaseContext : DbContext, IDatabaseContext + { + public DbSet ServerSettings { get; set; } + + public DbSet Users { get; set; } + + public DbSet Instances { get; set; } + + /// + /// The for s + /// + public DbSet Logs { get; set; } + + /// + /// The for the + /// + readonly DatabaseConfiguration databaseConfiguration; + /// + /// The for the + /// + readonly ILoggerFactory loggerFactory; + /// + /// The for the + /// + readonly IHostingEnvironment hostingEnvironment; + + /// + /// Construct a + /// + /// The for the + /// The containing the value of + /// The value of + /// The value of + public DatabaseContext(DbContextOptions options, IOptions databaseConfigurationOptions, ILoggerFactory loggerFactory, IHostingEnvironment hostingEnvironment) : base(options) + { + databaseConfiguration = databaseConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(databaseConfigurationOptions)); + this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); + this.hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment)); + } + + /// + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + // build default model. + LogModelBuilderHelper.Build(modelBuilder.Entity()); + // real relation database can map table: + modelBuilder.Entity().ToTable(nameof(Log)); + } + + /// + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + switch (databaseConfiguration.DatabaseType) + { + case DatabaseType.MySql: + optionsBuilder.UseMySQL(databaseConfiguration.ConnectionString); + break; + case DatabaseType.Sqlite: + optionsBuilder.UseSqlite(databaseConfiguration.ConnectionString); + break; + case DatabaseType.SqlServer: + optionsBuilder.UseSqlServer(databaseConfiguration.ConnectionString); + break; + } + optionsBuilder.UseLoggerFactory(loggerFactory); + if (hostingEnvironment.IsDevelopment()) + optionsBuilder.EnableSensitiveDataLogging(); + } + + /// + public Task GetServerSettings(CancellationToken cancellationToken) => ServerSettings.FirstOrDefaultAsync(cancellationToken); + + /// + public async Task Initialize(CancellationToken cancellationToken) + { + await Database.EnsureCreatedAsync(cancellationToken).ConfigureAwait(false); + await Database.MigrateAsync(cancellationToken).ConfigureAwait(false); + } + + /// + public Task Save(CancellationToken cancellationToken) => SaveChangesAsync(cancellationToken); + } +} diff --git a/src/Tgstation.Server.Host/Models/IDatabaseContext.cs b/src/Tgstation.Server.Host/Models/IDatabaseContext.cs index 9cbf54d537..9330d3d175 100644 --- a/src/Tgstation.Server.Host/Models/IDatabaseContext.cs +++ b/src/Tgstation.Server.Host/Models/IDatabaseContext.cs @@ -9,11 +9,6 @@ namespace Tgstation.Server.Host.Models /// interface IDatabaseContext { - /// - /// The in the - /// - ServerSettings ServerSettings { get; } - /// /// The s in the /// @@ -24,6 +19,13 @@ namespace Tgstation.Server.Host.Models /// DbSet Instances { get; } + /// + /// Get the in the + /// + /// The for the operation + /// A resulting in the in the + Task GetServerSettings(CancellationToken cancellationToken); + /// /// Saves changes made to the /// diff --git a/src/Tgstation.Server.Host/Models/Instance.cs b/src/Tgstation.Server.Host/Models/Instance.cs index 859ceac3c2..6ebb7f8507 100644 --- a/src/Tgstation.Server.Host/Models/Instance.cs +++ b/src/Tgstation.Server.Host/Models/Instance.cs @@ -38,5 +38,10 @@ namespace Tgstation.Server.Host.Models /// The s in the /// public List CompileJobs { get; set; } + + /// + /// The in the + /// + public List Jobs { get; set; } } } diff --git a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj index 40ab8fa5f7..bc0d5b6b3f 100644 --- a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj +++ b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj @@ -17,12 +17,30 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/Tgstation.Server.Host/appsettings.Development.json b/src/Tgstation.Server.Host/appsettings.Development.json new file mode 100644 index 0000000000..d3796e9ae8 --- /dev/null +++ b/src/Tgstation.Server.Host/appsettings.Development.json @@ -0,0 +1,24 @@ +{ + "Logging": { + "IncludeScopes": false, + "Debug": { + "LogLevel": { + "Default": "Debug" + } + }, + "Console": { + "LogLevel": { + "Default": "Trace" + } + }, + "EntityFramework": { + "LogLevel": { + "Default": "Warning" + } + } + }, + "Database": { + "DatabaseType": "Sqlite", + "ConnectionString": "Data Source=TestDB.sqlite3;Version=3;" + } +} diff --git a/src/Tgstation.Server.Host/appsettings.Docker.json b/src/Tgstation.Server.Host/appsettings.Docker.json new file mode 100644 index 0000000000..906e461ee4 --- /dev/null +++ b/src/Tgstation.Server.Host/appsettings.Docker.json @@ -0,0 +1,6 @@ +{ + "Database": { + "DatabaseType": "SqlServer", + "ConnectionString": "" + } +} diff --git a/src/Tgstation.Server.Host/appsettings.json b/src/Tgstation.Server.Host/appsettings.json new file mode 100644 index 0000000000..bfcbeffa32 --- /dev/null +++ b/src/Tgstation.Server.Host/appsettings.json @@ -0,0 +1,24 @@ +{ + "Logging": { + "IncludeScopes": false, + "Debug": { + "LogLevel": { + "Default": "Debug" + } + }, + "Console": { + "LogLevel": { + "Default": "Trace" + } + }, + "EntityFramework": { + "LogLevel": { + "Default": "Warning" + } + } + }, + "Database": { + "DatabaseType": "Sqlite", + "ConnectionString": "Fake" + } +}