Add env and Docker files
This commit is contained in:
parent
e96d78bd21
commit
2df719e536
7 changed files with 75 additions and 5 deletions
26
Dockerfile
Normal file
26
Dockerfile
Normal file
|
@ -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"]
|
|
@ -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
|
||||
driver: local
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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<DbContext> _logger;
|
||||
|
||||
public DbContext(IOptions<DbSettings> dbSettings)
|
||||
public DbContext(
|
||||
IOptions<DbSettings> dbSettings,
|
||||
ILogger<DbContext> 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()
|
||||
|
|
Loading…
Reference in a new issue