Skip to content

Commit

Permalink
[F] Docker config (#225)
Browse files Browse the repository at this point in the history
* Create docker config

* add Dockerfile
* enable ef migrations without connectionString

* Add flag to apply migrations.

* include Dockerfile in solution

* bump version
  • Loading branch information
SSchulze1989 authored Jan 31, 2025
1 parent a427dde commit 9c9443f
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 29 deletions.
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0 as build
WORKDIR /source
COPY . .
RUN dotnet restore src/iRLeagueApiCore.Server -r linux-x64
RUN dotnet publish src/iRLeagueApiCore.Server -r linux-x64 -o /app --no-restore

FROM mcr.microsoft.com/dotnet/aspnet:8.0-noble-chiseled-extra
WORKDIR /app
COPY --link --from=build /app .
USER $APP_UID
ENTRYPOINT ["./iRLeagueApiCore.Server"]
7 changes: 1 addition & 6 deletions iRLeagueApiCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7F90E099-56DE-4FEF-8CA2-4F4610CDEF19}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Dockerfile = Dockerfile
.github\workflows\dotnet_develop.yml = .github\workflows\dotnet_develop.yml
.github\workflows\dotnet_main_pr.yml = .github\workflows\dotnet_main_pr.yml
.github\workflows\dotnet_main_push.yml = .github\workflows\dotnet_main_push.yml
Expand All @@ -30,8 +31,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "iRLeagueApiCore.Common", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "iRLeagueDatabaseCore", "src\iRLeagueDatabaseCore\iRLeagueDatabaseCore.csproj", "{374D9358-C654-4882-AE9F-89DDB135A69D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "iRLeagueDatabaseCore.Migrations", "src\iRLeagueDatabaseCore.Migrations\iRLeagueDatabaseCore.Migrations.csproj", "{C335B0B3-B0AE-45EF-B620-87E9EBE257D3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbIntegrationTests", "test\DbIntegrationTests\DbIntegrationTests.csproj", "{CBE19DF1-F112-4BCB-9DC4-BE33D971CCCC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "test\UnitTests\UnitTests.csproj", "{FB4E0FC3-97A9-4456-93DB-F8335175FA47}"
Expand Down Expand Up @@ -78,10 +77,6 @@ Global
{374D9358-C654-4882-AE9F-89DDB135A69D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{374D9358-C654-4882-AE9F-89DDB135A69D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{374D9358-C654-4882-AE9F-89DDB135A69D}.Release|Any CPU.Build.0 = Release|Any CPU
{C335B0B3-B0AE-45EF-B620-87E9EBE257D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C335B0B3-B0AE-45EF-B620-87E9EBE257D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C335B0B3-B0AE-45EF-B620-87E9EBE257D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C335B0B3-B0AE-45EF-B620-87E9EBE257D3}.Release|Any CPU.Build.0 = Release|Any CPU
{CBE19DF1-F112-4BCB-9DC4-BE33D971CCCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBE19DF1-F112-4BCB-9DC4-BE33D971CCCC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBE19DF1-F112-4BCB-9DC4-BE33D971CCCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
2 changes: 1 addition & 1 deletion src/iRLeagueApiCore.Client/iRLeagueApiCore.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<OutputType>Library</OutputType>
<TargetFrameworks>net8.0</TargetFrameworks>
<PackageId>iRLeagueApiCore.Client</PackageId>
<Version>0.14.7</Version>
<Version>0.15.0</Version>
<Authors>Simon Schulze</Authors>
<Company>Simon Schulze</Company>
<PackageDescription>This package contains shared objects for all members of the iRLeagueDatabase-iRLeagueApi stack</PackageDescription>
Expand Down
2 changes: 1 addition & 1 deletion src/iRLeagueApiCore.Common/iRLeagueApiCore.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<PackageId>iRLeagueApiCore.Common</PackageId>
<Version>0.14.7</Version>
<Version>0.15.0</Version>
<Authors>Simon Schulze</Authors>
<Company>Simon Schulze</Company>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
1 change: 1 addition & 0 deletions src/iRLeagueApiCore.Server/Authentication/UserDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public UserDbContext(DbContextOptions<UserDbContext> options) : base(options)
{

}

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ namespace iRLeagueApiCore.Server.Authentication;

public sealed class UserDbContextFactory : IDbContextFactory<UserDbContext>
{
private readonly IConfiguration _configuration;
private readonly DbContextOptions<UserDbContext> contextOptions;

public UserDbContextFactory(IConfiguration configuration)
{
_configuration = configuration;
}

public UserDbContext CreateDbContext()
{
var dbConnectionString = _configuration.GetConnectionString("UserDb") ??
// load the database configurations once at start - reloading is not enabled
var dbConnectionString = configuration.GetConnectionString("UserDb") ??
throw new InvalidConfigurationException("No connection string for 'UserDB' found in configuration");
var optionsBuilder = new DbContextOptionsBuilder<UserDbContext>();
optionsBuilder.UseMySQL(dbConnectionString);
contextOptions = optionsBuilder.Options;
}

var dbContext = new UserDbContext(optionsBuilder.Options);
public UserDbContext CreateDbContext()
{
var dbContext = new UserDbContext(contextOptions);
dbContext.Database.EnsureCreated();
return dbContext;
}
Expand Down
22 changes: 15 additions & 7 deletions src/iRLeagueApiCore.Server/Models/LeagueDbContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@ namespace iRLeagueApiCore.Server.Models;

public sealed class LeagueDbContextFactory
{
private readonly IConfiguration _configuration;
private readonly bool applyMigrations = false;
private readonly DbContextOptions<LeagueDbContext> contextOptions;


public LeagueDbContextFactory(IConfiguration configuration)
{
_configuration = configuration;
}
applyMigrations = configuration.GetValue<bool>("ApplyDatabaseMigrations") == true;

public LeagueDbContext CreateDbContext(ILeagueProvider leagueProvider)
{
var dbConnectionString = _configuration.GetConnectionString("ModelDb") ??
// load the database configurations once at start - reloading is not enabled
var dbConnectionString = configuration.GetConnectionString("ModelDb") ??
throw new InvalidConfigurationException("No connection string for 'ModelDb' found in configuration");
var optionsBuilder = new DbContextOptionsBuilder<LeagueDbContext>();
optionsBuilder.UseMySQL(dbConnectionString);
contextOptions = optionsBuilder.Options;
}

var dbContext = new LeagueDbContext(optionsBuilder.Options, leagueProvider);
public LeagueDbContext CreateDbContext(ILeagueProvider leagueProvider)
{
var dbContext = new LeagueDbContext(contextOptions, leagueProvider);
if (applyMigrations)
{
dbContext.Database.Migrate();
}
return dbContext;
}
}
1 change: 0 additions & 1 deletion src/iRLeagueApiCore.Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ public void ConfigureServices(IServiceCollection services)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
Expand Down
2 changes: 1 addition & 1 deletion src/iRLeagueApiCore.Server/iRLeagueApiCore.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>0.14.7</Version>
<Version>0.15.0</Version>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
12 changes: 9 additions & 3 deletions src/iRLeagueDatabaseCore/MigrationLeagueDbContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ internal class MigrationLeagueDbContextFactory : IDesignTimeDbContextFactory<Lea
{
public LeagueDbContext CreateDbContext(string[] args)
{
var connectionString = Environment.GetEnvironmentVariable("EFCORETOOLSDB")
?? throw new InvalidOperationException("No connection string for migration provided. Please set $env:EFCORETOOLSDB");
var connectionString = Environment.GetEnvironmentVariable("EFCORETOOLSDB");
var optionsBuilder = new DbContextOptionsBuilder<LeagueDbContext>();
optionsBuilder.UseMySQL(connectionString);
if (string.IsNullOrEmpty(connectionString))
{
optionsBuilder.UseMySQL();
}
else
{
optionsBuilder.UseMySQL(connectionString);
}
var leagueProvider = Mock.Of<ILeagueProvider>();

var dbContext = new LeagueDbContext(optionsBuilder.Options, leagueProvider);
Expand Down
2 changes: 1 addition & 1 deletion src/iRLeagueDatabaseCore/iRLeagueDatabaseCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<PackageId>iRLeagueDatabaseCore</PackageId>
<Version>0.14.7</Version>
<Version>0.15.0</Version>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Expand Down

0 comments on commit 9c9443f

Please sign in to comment.