diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9621365 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build +WORKDIR /app + +COPY src/GitHubActionsDemo.Api/*.csproj ./GitHubActionsDemo.Api/ +COPY src/GitHubActionsDemo.Api.Sdk/*.csproj ./GitHubActionsDemo.Api.Sdk/ +COPY src/GitHubActionsDemo.Persistance/*.csproj ./GitHubActionsDemo.Persistance/ +COPY src/GitHubActionsDemo.Service/*.csproj ./GitHubActionsDemo.Service/ + +WORKDIR /app/GitHubActionsDemo.Api +RUN dotnet restore + +WORKDIR /app +COPY src/GitHubActionsDemo.Api/. ./GitHubActionsDemo.Api/ +COPY src/GitHubActionsDemo.Api.Sdk/. ./GitHubActionsDemo.Api.Sdk/ +COPY src/GitHubActionsDemo.Persistance/. ./GitHubActionsDemo.Persistance/ +COPY src/GitHubActionsDemo.Service/. ./GitHubActionsDemo.Service/ +WORKDIR /app/GitHubActionsDemo.Api +RUN dotnet publish -c Release -o out + +FROM mcr.microsoft.com/dotnet/aspnet:7.0 +WORKDIR /app +EXPOSE 5275/tcp +ENV ASPNETCORE_URLS http://*:5275 + +COPY --from=build /app/GitHubActionsDemo.Api/out ./ +ENTRYPOINT ["dotnet", "GitHubActionsDemo.Api.dll"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 53b72fc..27f1e5c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,15 +4,30 @@ services: image: mysql:8.0 cap_add: - SYS_NICE + hostname: db restart: always environment: - MYSQL_DATABASE=library - - MYSQL_ROOT_PASSWORD=libraryDbPassword + - MYSQL_RANDOM_ROOT_PASSWORD=1 + - MYSQL_USER=dbuser + - MYSQL_PASSWORD=libraryDbPassword ports: - '3306:3306' volumes: - db:/var/lib/mysql - ./db/init.sql:/docker-entrypoint-initdb.d/init.sql + + api: + build: . + restart: always + depends_on: + - db + ports: + - '5275:5275' + environment: + - ASPNETCORE_URLS=http://*:5275 + - API_DbSettings__ConnectionString=Server=db;Database=library;Uid=dbuser;Pwd=libraryDbPassword; + volumes: db: - driver: local \ No newline at end of file + driver: local diff --git a/src/GitHubActionsDemo.Api/Infrastructure/InfrastructureExtensions.cs b/src/GitHubActionsDemo.Api/Infrastructure/InfrastructureExtensions.cs new file mode 100644 index 0000000..eb19b75 --- /dev/null +++ b/src/GitHubActionsDemo.Api/Infrastructure/InfrastructureExtensions.cs @@ -0,0 +1,18 @@ +namespace GitHubActionsDemo.Api.Infrastructure; + +public static class InfrastructureExtensions +{ + public static IHostBuilder ConfigureAppSettings(this IHostBuilder host) + { + var enviroment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); + + host.ConfigureAppConfiguration((ctx, builder) => + { + builder.AddJsonFile("appsettings.json", false, true); + builder.AddJsonFile($"appsettings.{enviroment}.json", true, true); + builder.AddEnvironmentVariables("API_"); + }); + + return host; + } +} \ No newline at end of file diff --git a/src/GitHubActionsDemo.Api/Program.cs b/src/GitHubActionsDemo.Api/Program.cs index a78af57..59cc2ba 100644 --- a/src/GitHubActionsDemo.Api/Program.cs +++ b/src/GitHubActionsDemo.Api/Program.cs @@ -6,9 +6,12 @@ using GitHubActionsDemo.Api.Sdk.Authors; using GitHubActionsDemo.Api.Sdk.Books; using GitHubActionsDemo.Api.Sdk.Shared; using GitHubActionsDemo.Api.Models.Validators; +using GitHubActionsDemo.Api.Infrastructure; var builder = WebApplication.CreateBuilder(args); +builder.Host.ConfigureAppSettings(); + builder.Logging.ClearProviders(); var logger = new LoggerConfiguration() .WriteTo.Console() diff --git a/src/GitHubActionsDemo.Api/appsettings.Development.json b/src/GitHubActionsDemo.Api/appsettings.Development.json index fff4625..0cc797b 100644 --- a/src/GitHubActionsDemo.Api/appsettings.Development.json +++ b/src/GitHubActionsDemo.Api/appsettings.Development.json @@ -1,6 +1,6 @@ { "DbSettings": { - "ConnectionString": "Server=localhost; Database=library; Uid=root; Pwd=libraryDbPassword;", + "ConnectionString": "Server=localhost:3306; Database=library; Uid=dbuser;Pwd=libraryDbPassword;", "Database": "library" }, "Logging": { diff --git a/src/GitHubActionsDemo.Api/appsettings.json b/src/GitHubActionsDemo.Api/appsettings.json index 75480cb..dc1fff4 100644 --- a/src/GitHubActionsDemo.Api/appsettings.json +++ b/src/GitHubActionsDemo.Api/appsettings.json @@ -1,6 +1,6 @@ { "DbSettings": { - "ConnectionString": "Server=localhost:3306; Database=library; Uid=root; Pwd=libraryDbPassword;", + "ConnectionString": "Server=localhost:3306; Database=library; Uid=dbuser;Pwd=libraryDbPassword;", "Database": "library" }, "Logging": { diff --git a/src/GitHubActionsDemo.Persistance/DbContext.cs b/src/GitHubActionsDemo.Persistance/DbContext.cs index b70872d..1ab29b9 100644 --- a/src/GitHubActionsDemo.Persistance/DbContext.cs +++ b/src/GitHubActionsDemo.Persistance/DbContext.cs @@ -3,16 +3,22 @@ using MySql.Data.MySqlClient; using System.Data; using GitHubActionsDemo.Persistance.Infrastructure; using Microsoft.Extensions.Options; +using Microsoft.Extensions.Logging; namespace GitHubActionsDemo.Persistance; public class DbContext : IDbContext { private readonly DbSettings _dbSettings; + private readonly ILogger _logger; - public DbContext(IOptions dbSettings) + public DbContext( + IOptions dbSettings, + ILogger logger + ) { _dbSettings = dbSettings?.Value ?? throw new ArgumentNullException(nameof(dbSettings)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public IDbConnection CreateConnection() @@ -29,9 +35,11 @@ public class DbContext : IDbContext private async Task InitDatabase() { // create database if it doesn't exist + _logger.LogInformation(_dbSettings.ConnectionString); using var connection = new MySqlConnection(_dbSettings.ConnectionString); var sql = $"CREATE DATABASE IF NOT EXISTS `{_dbSettings.Database}`;"; await connection.ExecuteAsync(sql); + _logger.LogInformation("Database created"); } private async Task InitTables()