diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..76877ac --- /dev/null +++ b/.dockerignore @@ -0,0 +1,295 @@ + +# Created by https://www.gitignore.io/api/csharp + +### Csharp ### +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ +Properties/launchSettings.json + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/ +tools/Cake.CoreCLR +.vscode +tools +.dotnet +Dockerfile + +# .env file contains default environment variables for docker +.env +.git/ + + +node_modules +npm-debug.log +Dockerfile* +docker-compose* +.dockerignore +.gitignore +README.md +LICENSE \ No newline at end of file diff --git a/.github/workflows/post.yml b/.github/workflows/post.yml index f45fc37..88e2a1e 100644 --- a/.github/workflows/post.yml +++ b/.github/workflows/post.yml @@ -50,18 +50,47 @@ jobs: - name: Build Docker image run: | + IMAGE_VERSION="latest" if [[ "$GITHUB_REF" == refs/tags/* ]]; then - IMAGE_VERSION=${GITHUB_REF#refs/tags/} - docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/post-service:${IMAGE_VERSION} -f ./backend/src/PostService/Dockerfile ./backend - else - echo "Skipping Docker image build because this is not a tag push." + IMAGE_VERSION=${GITHUB_REF#refs/tags/} fi + docker buildx build \ + --build-arg ASPNETCORE_ENVIRONMENT=${{ secrets.ASPNETCORE_ENVIRONMENT }} \ + --build-arg DB_CONNECTION_STRING="${{ secrets.DB_CONNECTION_STRING }}" \ + -t ${{ secrets.DOCKER_HUB_USERNAME }}/post-service:${IMAGE_VERSION} \ + -f ./backend/src/PostService/Dockerfile \ + ./backend/src - name: Push Docker image to Docker Hub + if: startsWith(github.ref, 'refs/tags/') run: | - if [[ "$GITHUB_REF" == refs/tags/* ]]; then - IMAGE_VERSION=${GITHUB_REF#refs/tags/} - docker push ${{ secrets.DOCKER_HUB_USERNAME }}/post-service:${IMAGE_VERSION} - else - echo "Skipping Docker image push because this is not a tag push." - fi \ No newline at end of file + IMAGE_VERSION=${GITHUB_REF#refs/tags/} + docker push ${{ secrets.DOCKER_HUB_USERNAME }}/post-service:${IMAGE_VERSION} + + docker-compose: + needs: docker + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Build Docker Compose + run: docker compose build --no-cache + env: + ASPNETCORE_ENVIRONMENT: ${{ secrets.ASPNETCORE_ENVIRONMENT }} + DB_CONNECTION_STRING: ${{ secrets.DB_CONNECTION_STRING }} + + - name: Start Docker Compose + run: docker compose up -d + env: + ASPNETCORE_ENVIRONMENT: ${{ secrets.ASPNETCORE_ENVIRONMENT }} + DB_CONNECTION_STRING: ${{ secrets.DB_CONNECTION_STRING }} + + - name: Health Check + run: | + echo "Checking health..." + for i in {1..10}; do + curl --fail http://0.0.0.0:8080/health && break || sleep 5 + done + curl --fail http://0.0.0.0:8080/health || exit 1 \ No newline at end of file diff --git a/FitnessApp.sln b/FitnessApp.sln index 7f06252..fde5cbd 100644 --- a/FitnessApp.sln +++ b/FitnessApp.sln @@ -1,6 +1,9 @@  Microsoft Visual Studio Solution File, Format Version 12.00 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PostService", "backend\PostService", "{84AE3F00-9A59-4A8D-A737-67AB74E5D1FD}" + ProjectSection(SolutionItems) = preProject + backend\src\PostService\Dockerfile = backend\src\PostService\Dockerfile + EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Api", "backend\src\PostService\PostService.Api\PostService.Api.csproj", "{2E44C019-ED30-4671-A878-D2F7B6071024}" EndProject @@ -33,7 +36,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{D5A1 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution-items", "solution-items", "{CE086396-E7FB-46F7-87A9-4570DBF43597}" ProjectSection(SolutionItems) = preProject + .dockerignore = .dockerignore .gitignore = .gitignore + docker-compose.yml = docker-compose.yml LICENSE = LICENSE README.md = README.md EndProjectSection diff --git a/backend/src/PostService/Dockerfile b/backend/src/PostService/Dockerfile index 4703832..1f42746 100644 --- a/backend/src/PostService/Dockerfile +++ b/backend/src/PostService/Dockerfile @@ -2,26 +2,33 @@ FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base WORKDIR /app EXPOSE 8080 +ARG ASPNETCORE_ENVIRONMENT +ARG DB_CONNECTION_STRING + +ENV ASPNETCORE_ENVIRONMENT=Development +ENV ConnectionStrings__DockerConnection=${DB_CONNECTION_STRING} + FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build + WORKDIR /src -COPY ["FitnessApp.sln", "./"] -COPY ["src/PostService/PostService.Api/PostService.Api.csproj", "src/PostService/PostService.Api/"] -COPY ["src/PostService/PostService.Application/PostService.Application.csproj", "src/PostService/PostService.Application/"] -COPY ["src/PostService/PostService.Domain/PostService.Domain.csproj", "src/PostService/PostService.Domain/"] -COPY ["src/PostService/PostService.Infrastructure/PostService.Infrastructure.csproj", "src/PostService/PostService.Infrastructure/"] -COPY ["src/PostService/PostService.Persistence/PostService.Persistence.csproj", "src/PostService/PostService.Persistence/"] -COPY ["src/Shared/Shared.Application/Shared.Application.csproj", "src/Shared/Shared.Application/"] +COPY PostService/PostService.Api/PostService.Api.csproj PostService/PostService.Api/ +COPY PostService/PostService.Application/PostService.Application.csproj PostService/PostService.Application/ +COPY PostService/PostService.Domain/PostService.Domain.csproj PostService/PostService.Domain/ +COPY PostService/PostService.Infrastructure/PostService.Infrastructure.csproj PostService/PostService.Infrastructure/ +COPY PostService/PostService.Persistence/PostService.Persistence.csproj PostService/PostService.Persistence/ +COPY Shared/Shared.Application/Shared.Application.csproj Shared/Shared.Application/ -RUN dotnet restore "src/PostService/PostService.Api/PostService.Api.csproj" +RUN dotnet restore PostService/PostService.Api/PostService.Api.csproj COPY . . -WORKDIR "/src/src/PostService/PostService.Api" -RUN dotnet build -c Release -o /app/build +WORKDIR /src/PostService/PostService.Api + +RUN dotnet build PostService.Api.csproj -c Release -o /app/build FROM build AS publish -RUN dotnet publish -c Release -o /app/publish +RUN dotnet publish PostService.Api.csproj -c Release -o /app/publish FROM base AS final WORKDIR /app diff --git a/backend/src/PostService/PostService.Api/Program.cs b/backend/src/PostService/PostService.Api/Program.cs index 30ed7cc..d4c8d32 100644 --- a/backend/src/PostService/PostService.Api/Program.cs +++ b/backend/src/PostService/PostService.Api/Program.cs @@ -6,7 +6,7 @@ using PostService.Persistence; var builder = WebApplication.CreateBuilder(args); - + builder.Services .AddPersistenceServices(builder.Configuration) .AddInfrastructureServices() @@ -26,7 +26,12 @@ dbContext.Database.Migrate(); } -app.UseHttpsRedirection(); +if (!app.Environment.IsDevelopment()) +{ + app.UseHttpsRedirection(); +} + +app.MapGet("/health", () => Results.Ok("Healthy")); app.MapGraphQL(); diff --git a/backend/src/PostService/PostService.Api/appsettings.Development.json b/backend/src/PostService/PostService.Api/appsettings.Development.json index 1b138cf..21a9dde 100644 --- a/backend/src/PostService/PostService.Api/appsettings.Development.json +++ b/backend/src/PostService/PostService.Api/appsettings.Development.json @@ -6,6 +6,6 @@ } }, "ConnectionStrings": { - "DefaultConnection": "Host=localhost;Port=5432;Username=postgres;Password=mysecretpasswordfordevelopment;Database=postgres" + "DefaultConnection": "Host=postgres-posts;Port=5432;Username=postgres;Password=mysecretpasswordfordevelopment;Database=postgres" } } diff --git a/backend/src/PostService/PostService.Api/appsettings.json b/backend/src/PostService/PostService.Api/appsettings.json index 10f68b8..b93ff1f 100644 --- a/backend/src/PostService/PostService.Api/appsettings.json +++ b/backend/src/PostService/PostService.Api/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Port=5432;Username=postgres;Password=mysecretpasswordfordevelopment;Database=postgres" + } } diff --git a/backend/src/PostService/PostService.Persistence/DependencyInjection.cs b/backend/src/PostService/PostService.Persistence/DependencyInjection.cs index e420826..a9169ca 100644 --- a/backend/src/PostService/PostService.Persistence/DependencyInjection.cs +++ b/backend/src/PostService/PostService.Persistence/DependencyInjection.cs @@ -8,8 +8,15 @@ public static class DependencyInjection { public static IServiceCollection AddPersistenceServices(this IServiceCollection services, IConfiguration configuration) { + var connectionString = configuration.GetConnectionString("DockerConnection"); + + if (string.IsNullOrWhiteSpace(connectionString)) + { + connectionString = configuration.GetConnectionString("DefaultConnection"); + } + services.AddDbContext(options => - options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))); + options.UseNpgsql(connectionString)); return services; } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..caa234e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,40 @@ +networks: + microservices: + driver: bridge + +services: + post-service: + container_name: post-service + build: + context: ./backend/src + dockerfile: PostService/Dockerfile + ports: + - "8080:8080" + depends_on: + postgres-posts: + condition: service_healthy + networks: + - microservices + + postgres-posts: + container_name: postgres-posts + image: postgres:15-alpine + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: mysecretpasswordfordevelopment + POSTGRES_DB: postgres + ports: + - "5432:5432" + restart: always + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 10s + timeout: 5s + retries: 5 + volumes: + - post-service-data:/var/lib/postgresql/data + networks: + - microservices + +volumes: + post-service-data: