More database context stuff

This commit is contained in:
Cyberboss
2018-04-07 14:42:32 -04:00
parent 8c470440ec
commit c63549fa93
12 changed files with 235 additions and 7 deletions
+6
View File
@@ -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"]
@@ -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/"
}
}
}
@@ -0,0 +1,8 @@
namespace Tgstation.Server.Host.Configuration
{
sealed class DatabaseConfiguration
{
public DatabaseType DatabaseType { get; set; }
public string ConnectionString { get; set; }
}
}
@@ -0,0 +1,9 @@
namespace Tgstation.Server.Host.Configuration
{
enum DatabaseType
{
SqlServer,
MySql,
Sqlite
}
}
+11 -2
View File
@@ -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<DatabaseContext>(configuration.GetSection("Database"));
services.AddMvc();
services.AddOptions();
services.AddLocalization();
}
services.AddDbContext<DatabaseContext>();
services.AddScoped<IDatabaseContext>(x => x.GetRequiredService<DatabaseContext>());
}
/// <summary>
/// Configure the <see cref="Application"/>
@@ -70,6 +77,8 @@ namespace Tgstation.Server.Host.Core
SupportedUICultures = supportedCultures,
});
applicationBuilder.UseAsyncInitialization<IDatabaseContext>((databaseContext, cancellationToken) => databaseContext.Initialize(cancellationToken));
applicationBuilder.UseSystemAuthentication();
applicationBuilder.UseMvc();
@@ -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> ServerSettings { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Instance> Instances { get; set; }
/// <summary>
/// The <see cref="DbSet{TEntity}"/> for <see cref="Log"/>s
/// </summary>
public DbSet<Log> Logs { get; set; }
/// <summary>
/// The <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext"/>
/// </summary>
readonly DatabaseConfiguration databaseConfiguration;
/// <summary>
/// The <see cref="ILoggerFactory"/> for the <see cref="DatabaseContext"/>
/// </summary>
readonly ILoggerFactory loggerFactory;
/// <summary>
/// The <see cref="IHostingEnvironment"/> for the <see cref="DatabaseContext"/>
/// </summary>
readonly IHostingEnvironment hostingEnvironment;
/// <summary>
/// Construct a <see cref="DatabaseContext"/>
/// </summary>
/// <param name="options">The <see cref="DbContextOptions{TContext}"/> for the <see cref="DatabaseContext"/></param>
/// <param name="databaseConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="databaseConfiguration"/></param>
/// <param name="loggerFactory">The value of <see cref="loggerFactory"/></param>
/// <param name="hostingEnvironment">The value of <see cref="hostingEnvironment"/></param>
public DatabaseContext(DbContextOptions<DatabaseContext> options, IOptions<DatabaseConfiguration> 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));
}
/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// build default model.
LogModelBuilderHelper.Build(modelBuilder.Entity<Log>());
// real relation database can map table:
modelBuilder.Entity<Log>().ToTable(nameof(Log));
}
/// <inheritdoc />
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();
}
/// <inheritdoc />
public Task<ServerSettings> GetServerSettings(CancellationToken cancellationToken) => ServerSettings.FirstOrDefaultAsync(cancellationToken);
/// <inheritdoc />
public async Task Initialize(CancellationToken cancellationToken)
{
await Database.EnsureCreatedAsync(cancellationToken).ConfigureAwait(false);
await Database.MigrateAsync(cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc />
public Task Save(CancellationToken cancellationToken) => SaveChangesAsync(cancellationToken);
}
}
@@ -9,11 +9,6 @@ namespace Tgstation.Server.Host.Models
/// </summary>
interface IDatabaseContext
{
/// <summary>
/// The <see cref="ServerSettings"/> in the <see cref="IDatabaseContext"/>
/// </summary>
ServerSettings ServerSettings { get; }
/// <summary>
/// The <see cref="User"/>s in the <see cref="IDatabaseContext"/>
/// </summary>
@@ -24,6 +19,13 @@ namespace Tgstation.Server.Host.Models
/// </summary>
DbSet<Instance> Instances { get; }
/// <summary>
/// Get the <see cref="ServerSettings"/> in the <see cref="IDatabaseContext"/>
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="ServerSettings"/> in the <see cref="IDatabaseContext"/></returns>
Task<ServerSettings> GetServerSettings(CancellationToken cancellationToken);
/// <summary>
/// Saves changes made to the <see cref="IDatabaseContext"/>
/// </summary>
@@ -38,5 +38,10 @@ namespace Tgstation.Server.Host.Models
/// The <see cref="CompileJob"/>s in the <see cref="Instance"/>
/// </summary>
public List<CompileJob> CompileJobs { get; set; }
/// <summary>
/// The <see cref="Jobs"/> in the <see cref="Instance"/>
/// </summary>
public List<Job> Jobs { get; set; }
}
}
@@ -17,12 +17,30 @@
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.Development.json" />
<None Remove="appsettings.Docker.json" />
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.Development.json" />
<Content Include="appsettings.Docker.json" />
<Content Include="appsettings.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Byond.TopicSender" Version="1.1.0.1" />
<PackageReference Include="Cyberboss.AspNetCore.AsyncInitializer" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.2" />
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="6.10.6" />
<PackageReference Include="Octokit" Version="0.29.0" />
<PackageReference Include="System.Security.Principal.Windows" Version="4.4.1" />
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="1.7.16" />
<PackageReference Include="ZNetCS.AspNetCore.Logging.EntityFrameworkCore" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
@@ -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;"
}
}
@@ -0,0 +1,6 @@
{
"Database": {
"DatabaseType": "SqlServer",
"ConnectionString": "<Your connection string>"
}
}
@@ -0,0 +1,24 @@
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Debug"
}
},
"Console": {
"LogLevel": {
"Default": "Trace"
}
},
"EntityFramework": {
"LogLevel": {
"Default": "Warning"
}
}
},
"Database": {
"DatabaseType": "Sqlite",
"ConnectionString": "Fake"
}
}