From 06089f23cf70f0d062ec191e874c9eb1b1bb1d01 Mon Sep 17 00:00:00 2001 From: nikita <368050@edu.itmo.ru> Date: Sun, 31 Mar 2024 15:38:44 +0300 Subject: [PATCH] add dockerfile & docker-compose --- .dockerignore | 25 ++++ .../Extensions/ServiceCollectionExtensions.cs | 1 + .../20240329121538_Initial.Designer.cs | 139 ------------------ .../Migrations/20240329121538_Initial.cs | 112 -------------- .../DatabaseContextModelSnapshot.cs | 136 ----------------- Presentation/Dockerfile | 28 ++++ Presentation/Presentation.csproj | 6 + Presentation/Program.cs | 17 ++- Presentation/appsettings.json | 2 +- Presentation/docker-compose.yml | 12 -- docker-compose.yml | 27 ++++ 11 files changed, 98 insertions(+), 407 deletions(-) create mode 100644 .dockerignore delete mode 100644 Infrastructure.DataAccess/Migrations/20240329121538_Initial.Designer.cs delete mode 100644 Infrastructure.DataAccess/Migrations/20240329121538_Initial.cs delete mode 100644 Infrastructure.DataAccess/Migrations/DatabaseContextModelSnapshot.cs create mode 100644 Presentation/Dockerfile delete mode 100644 Presentation/docker-compose.yml create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..38bece4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/Infrastructure.DataAccess/Extensions/ServiceCollectionExtensions.cs b/Infrastructure.DataAccess/Extensions/ServiceCollectionExtensions.cs index cffac88..4540ea2 100644 --- a/Infrastructure.DataAccess/Extensions/ServiceCollectionExtensions.cs +++ b/Infrastructure.DataAccess/Extensions/ServiceCollectionExtensions.cs @@ -12,6 +12,7 @@ public static IServiceCollection AddDataAccess( { collection.AddDbContext(configuration); collection.AddScoped(x => x.GetRequiredService()); + return collection; } diff --git a/Infrastructure.DataAccess/Migrations/20240329121538_Initial.Designer.cs b/Infrastructure.DataAccess/Migrations/20240329121538_Initial.Designer.cs deleted file mode 100644 index 38daf40..0000000 --- a/Infrastructure.DataAccess/Migrations/20240329121538_Initial.Designer.cs +++ /dev/null @@ -1,139 +0,0 @@ -// -using System; -using Infrastructure.DataAccess; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace Infrastructure.DataAccess.Migrations -{ - [DbContext(typeof(DatabaseContext))] - [Migration("20240329121538_Initial")] - partial class Initial - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.17") - .HasAnnotation("Relational:MaxIdentifierLength", 63); - - NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - - modelBuilder.Entity("Domain.Applications.ApplicationActivity", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("Description") - .IsRequired() - .HasColumnType("text"); - - b.Property("Name") - .IsRequired() - .HasColumnType("text"); - - b.HasKey("Id"); - - b.HasIndex("Name") - .IsUnique(); - - b.ToTable("ApplicationActivities"); - }); - - modelBuilder.Entity("Domain.Applications.SubmittedApplication", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("ActivityId") - .HasColumnType("uuid"); - - b.Property("CreatedDateTime") - .HasColumnType("timestamp with time zone"); - - b.Property("Description") - .HasColumnType("text"); - - b.Property("Name") - .IsRequired() - .HasColumnType("text"); - - b.Property("Plan") - .IsRequired() - .HasColumnType("text"); - - b.Property("UserId") - .HasColumnType("uuid"); - - b.HasKey("Id"); - - b.HasIndex("ActivityId"); - - b.HasIndex("UserId"); - - b.ToTable("SubmittedApplications"); - }); - - modelBuilder.Entity("Domain.Applications.UnsubmittedApplication", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("ActivityId") - .HasColumnType("uuid"); - - b.Property("CreatedDateTime") - .HasColumnType("timestamp with time zone"); - - b.Property("Description") - .HasColumnType("text"); - - b.Property("Name") - .HasColumnType("text"); - - b.Property("Plan") - .HasColumnType("text"); - - b.Property("UserId") - .HasColumnType("uuid"); - - b.HasKey("Id"); - - b.HasIndex("ActivityId"); - - b.HasIndex("UserId") - .IsUnique(); - - b.ToTable("UnsubmittedApplications"); - }); - - modelBuilder.Entity("Domain.Applications.SubmittedApplication", b => - { - b.HasOne("Domain.Applications.ApplicationActivity", "Activity") - .WithMany() - .HasForeignKey("ActivityId"); - - b.Navigation("Activity"); - }); - - modelBuilder.Entity("Domain.Applications.UnsubmittedApplication", b => - { - b.HasOne("Domain.Applications.ApplicationActivity", "Activity") - .WithMany() - .HasForeignKey("ActivityId"); - - b.Navigation("Activity"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Infrastructure.DataAccess/Migrations/20240329121538_Initial.cs b/Infrastructure.DataAccess/Migrations/20240329121538_Initial.cs deleted file mode 100644 index fd8fe81..0000000 --- a/Infrastructure.DataAccess/Migrations/20240329121538_Initial.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace Infrastructure.DataAccess.Migrations -{ - /// - public partial class Initial : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "ApplicationActivities", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: false), - Description = table.Column(type: "text", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_ApplicationActivities", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "SubmittedApplications", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - UserId = table.Column(type: "uuid", nullable: false), - ActivityId = table.Column(type: "uuid", nullable: true), - Name = table.Column(type: "text", nullable: false), - Description = table.Column(type: "text", nullable: true), - Plan = table.Column(type: "text", nullable: false), - CreatedDateTime = table.Column(type: "timestamp with time zone", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_SubmittedApplications", x => x.Id); - table.ForeignKey( - name: "FK_SubmittedApplications_ApplicationActivities_ActivityId", - column: x => x.ActivityId, - principalTable: "ApplicationActivities", - principalColumn: "Id"); - }); - - migrationBuilder.CreateTable( - name: "UnsubmittedApplications", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - UserId = table.Column(type: "uuid", nullable: false), - ActivityId = table.Column(type: "uuid", nullable: true), - Name = table.Column(type: "text", nullable: true), - Description = table.Column(type: "text", nullable: true), - Plan = table.Column(type: "text", nullable: true), - CreatedDateTime = table.Column(type: "timestamp with time zone", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_UnsubmittedApplications", x => x.Id); - table.ForeignKey( - name: "FK_UnsubmittedApplications_ApplicationActivities_ActivityId", - column: x => x.ActivityId, - principalTable: "ApplicationActivities", - principalColumn: "Id"); - }); - - migrationBuilder.CreateIndex( - name: "IX_ApplicationActivities_Name", - table: "ApplicationActivities", - column: "Name", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_SubmittedApplications_ActivityId", - table: "SubmittedApplications", - column: "ActivityId"); - - migrationBuilder.CreateIndex( - name: "IX_SubmittedApplications_UserId", - table: "SubmittedApplications", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_UnsubmittedApplications_ActivityId", - table: "UnsubmittedApplications", - column: "ActivityId"); - - migrationBuilder.CreateIndex( - name: "IX_UnsubmittedApplications_UserId", - table: "UnsubmittedApplications", - column: "UserId", - unique: true); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "SubmittedApplications"); - - migrationBuilder.DropTable( - name: "UnsubmittedApplications"); - - migrationBuilder.DropTable( - name: "ApplicationActivities"); - } - } -} diff --git a/Infrastructure.DataAccess/Migrations/DatabaseContextModelSnapshot.cs b/Infrastructure.DataAccess/Migrations/DatabaseContextModelSnapshot.cs deleted file mode 100644 index 4075533..0000000 --- a/Infrastructure.DataAccess/Migrations/DatabaseContextModelSnapshot.cs +++ /dev/null @@ -1,136 +0,0 @@ -// -using System; -using Infrastructure.DataAccess; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace Infrastructure.DataAccess.Migrations -{ - [DbContext(typeof(DatabaseContext))] - partial class DatabaseContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.17") - .HasAnnotation("Relational:MaxIdentifierLength", 63); - - NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - - modelBuilder.Entity("Domain.Applications.ApplicationActivity", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("Description") - .IsRequired() - .HasColumnType("text"); - - b.Property("Name") - .IsRequired() - .HasColumnType("text"); - - b.HasKey("Id"); - - b.HasIndex("Name") - .IsUnique(); - - b.ToTable("ApplicationActivities"); - }); - - modelBuilder.Entity("Domain.Applications.SubmittedApplication", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("ActivityId") - .HasColumnType("uuid"); - - b.Property("CreatedDateTime") - .HasColumnType("timestamp with time zone"); - - b.Property("Description") - .HasColumnType("text"); - - b.Property("Name") - .IsRequired() - .HasColumnType("text"); - - b.Property("Plan") - .IsRequired() - .HasColumnType("text"); - - b.Property("UserId") - .HasColumnType("uuid"); - - b.HasKey("Id"); - - b.HasIndex("ActivityId"); - - b.HasIndex("UserId"); - - b.ToTable("SubmittedApplications"); - }); - - modelBuilder.Entity("Domain.Applications.UnsubmittedApplication", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property("ActivityId") - .HasColumnType("uuid"); - - b.Property("CreatedDateTime") - .HasColumnType("timestamp with time zone"); - - b.Property("Description") - .HasColumnType("text"); - - b.Property("Name") - .HasColumnType("text"); - - b.Property("Plan") - .HasColumnType("text"); - - b.Property("UserId") - .HasColumnType("uuid"); - - b.HasKey("Id"); - - b.HasIndex("ActivityId"); - - b.HasIndex("UserId") - .IsUnique(); - - b.ToTable("UnsubmittedApplications"); - }); - - modelBuilder.Entity("Domain.Applications.SubmittedApplication", b => - { - b.HasOne("Domain.Applications.ApplicationActivity", "Activity") - .WithMany() - .HasForeignKey("ActivityId"); - - b.Navigation("Activity"); - }); - - modelBuilder.Entity("Domain.Applications.UnsubmittedApplication", b => - { - b.HasOne("Domain.Applications.ApplicationActivity", "Activity") - .WithMany() - .HasForeignKey("ActivityId"); - - b.Navigation("Activity"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Presentation/Dockerfile b/Presentation/Dockerfile new file mode 100644 index 0000000..f33307e --- /dev/null +++ b/Presentation/Dockerfile @@ -0,0 +1,28 @@ +FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base +WORKDIR /app +EXPOSE 80 + +FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["Presentation/Presentation.csproj", "Presentation/"] +COPY ["Infrastructure.DataAccess/Infrastructure.DataAccess.csproj", "Infrastructure.DataAccess/"] +COPY ["Application.Abstractions/Application.Abstractions.csproj", "Application.Abstractions/"] +COPY ["Domain/Domain.csproj", "Domain/"] +COPY ["Application.Dto/Application.Dto.csproj", "Application.Dto/"] +COPY ["Application/Application/Application.csproj", "Application/Application/"] +COPY ["Application/Application.Contracts/Application.Contracts.csproj", "Application/Application.Contracts/"] +RUN dotnet restore "Presentation/Presentation.csproj" +RUN dotnet dev-certs https +COPY . . +WORKDIR "/src/Presentation" +RUN dotnet build "Presentation.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "Presentation.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "Presentation.dll"] diff --git a/Presentation/Presentation.csproj b/Presentation/Presentation.csproj index 4f78804..b80d1d2 100644 --- a/Presentation/Presentation.csproj +++ b/Presentation/Presentation.csproj @@ -30,4 +30,10 @@ + + + .dockerignore + + + diff --git a/Presentation/Program.cs b/Presentation/Program.cs index 9a6473f..f02bd67 100644 --- a/Presentation/Program.cs +++ b/Presentation/Program.cs @@ -1,6 +1,8 @@ +using Application.Abstractions.DataAccess; using Application.Extensions; using Infrastructure.DataAccess.Extensions; using Microsoft.EntityFrameworkCore; +using Microsoft.OpenApi.Models; using Presentation.Extensions; var builder = WebApplication.CreateBuilder(args); @@ -11,19 +13,20 @@ builder.Services.AddJsonSettings(); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddSwaggerGen(c => + c.SwaggerDoc("v1", new OpenApiInfo { Title = "Applications API", Version = "v1" }) +); var app = builder.Build(); -if (app.Environment.IsDevelopment()) +using (var scope = app.Services.CreateScope()) { - app.UseSwagger(); - app.UseSwaggerUI(); + var context = scope.ServiceProvider.GetRequiredService(); + context.Database.Migrate(); } -app.UseHttpsRedirection(); - -app.UseAuthorization(); +app.UseSwagger(); +app.UseSwaggerUI(); app.MapControllers(); diff --git a/Presentation/appsettings.json b/Presentation/appsettings.json index 7478e0f..a126693 100644 --- a/Presentation/appsettings.json +++ b/Presentation/appsettings.json @@ -7,6 +7,6 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "DefaultConnection": "Host=localhost;Port=5434;Username=postgres;Password=postgres;Database=postgres;SslMode=disable;" + "DefaultConnection": "Host=postgres;Port=5432;Username=postgres;Password=postgres;Database=postgres;SslMode=disable;" } } diff --git a/Presentation/docker-compose.yml b/Presentation/docker-compose.yml deleted file mode 100644 index 236c350..0000000 --- a/Presentation/docker-compose.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: '3.9' - -services: - postgres: - image: postgres:latest - environment: - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=postgres - - POSTGRES_DB=postgres - ports: - - "5434:5432" - restart: unless-stopped \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b4f6cdb --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,27 @@ +version: '3.9' + +services: + applications-api: + build: + context: . + dockerfile: Presentation/Dockerfile + environment: + - ASPNETCORE_ENVIRONMENT=Development + - ASPNETCORE_URLS=http://+:80 + ports: + - "8080:80" + depends_on: + - postgres + restart: unless-stopped + + postgres: + image: postgres:latest + volumes: + - ./.database/postgres/data:/var/lib/postgresql/data:z + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=postgres + ports: + - "5432:5432" + restart: unless-stopped \ No newline at end of file