From b75a52c223402b5961a08f14ba86541961d6d19c Mon Sep 17 00:00:00 2001 From: Felipe Augusto Date: Thu, 14 Dec 2023 22:39:37 -0300 Subject: [PATCH] docs: organizando o readme do projeto --- README.md | 211 +++++++++++++++++- docker/docker-compose.yml | 10 + .../DEPLOY.Cachorro.Api.csproj | 2 +- .../Telemetria/AppInsightsExtensions.cs | 8 +- src/DEPLOY.Cachorro.Api/Program.cs | 1 - 5 files changed, 219 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 0d171a0..af9e84f 100644 --- a/README.md +++ b/README.md @@ -22,14 +22,48 @@ Projeto educacional, criado e mantido através do canal DEPLOY no YouTube. > Para criar a imagem, a partir do diretório root da aplicação (pasta que contem o arquivo sln) +# pre-requisitos para executar o projeto + +1. Docker +2. Um conta no Azure para criar recursos + ``` -docker build -f ./DEPLOY.Cachorro.Api/Dockerfile -t crcanaldeploydev.azurecr.io/cachorro.api:0.4 . +mnt ❯ c ❯ proj ❯ DEPLOY ❯ DEPLOY.Cachorro $ docker build -f ./DEPLOY.Cachorro.Api/Dockerfile -t crcanaldeploydev.azurecr.io/cachorro.api:latest . ``` +dotnet run --project ./src/DEPLOY.Cachorro.Api/DEPLOY.Cachorro.Api.csproj ASPNETCORE_ENVIRONMENT=aaa ConnectionStrings:ApplicationInsights="bbb" ConnectionStrings:DefaultConnection="ccc" ConnectionStrings:AppConfiguration="ddd" +ApplicationInsights:ApiKey="eee" KeyVault:VaultUri="fff" + > Para executar o projeto local, utilizando docker ``` -docker container run --rm -p 8088:80 -e ConnectionsString__ApplicationInsights="xxxx" -e ApplicationInsights__ApiKey="yyy" felipementel/cachorro-api:0.3 +docker container run --rm -p 8088:80 \ +-e ASPNETCORE_ENVIRONMENT=aaa \ +-e ConnectionStrings__ApplicationInsights="bbb" \ +-e ConnectionStrings__DefaultConnection="ccc" \ +-e ConnectionStrings__AppConfiguration="ddd" \ +-e ApplicationInsights__ApiKey="eee" \ +-e KeyVault__VaultUri="fff" \ +felipementel/cachorro.api:latest +``` + +aaa = Development|Production +bbb = ConnectionString do Application Insights +ccc = Connection String (Azure SQL Database ou https://hub.docker.com/_/microsoft-azure-sql-edge) +ddd = Connection string do App Configuration +eee = ApiKey gerada dentro do Application Insights +fff = URI do Azure KeyVault + +Comando para subir o banco de dados local: + +``` +docker run --cap-add SYS_PTRACE -e 'ACCEPT_EULA=1' -e 'MSSQL_SA_PASSWORD=Abcd1234%' -e 'MSSQL_PID=Developer' -p 1433:1433 --name azuresqledge -d mcr.microsoft.com/azure-sql-edge:2.0.0 +``` + +Caso queira testar com outra tag, utilize os comandos abaixo para obter a lista de tags + +``` +https://mcr.microsoft.com/v2/azure-sql-edge/tags/list ``` # Testes de unidade @@ -55,44 +89,59 @@ dotnet test ``` 2. Geração de relatório de testes + 1.1 A partir da pasta src execute o comando: ``` -dotnet test --collect:"XPlat Code Coverage" --logger "console;verbosity=detailed" +dotnet test --collect:"XPlat Code Coverage" --logger "console;verbosity=detailed" --results-directory .\DEPLOY.Cachorro.Api.Tests\TestResults\XPlatCodeCoverage\ ``` -ou +e depois execute: ``` -dotnet-coverage collect "dotnet test" -f xml -o "coverage.xml" +reportgenerator -reports:.\DEPLOY.Cachorro.Api.Tests\TestResults\XPlatCodeCoverage\**\coverage.cobertura.xml -targetdir:./DEPLOY.Cachorro.Api.Tests/TestResults/XPlatCodeCoverage/CoverageReport -reporttypes:"Html;SonarQube;JsonSummary;Badges" -verbosity:Verbose -title:Cachorro.API -tag:canal-deploy ``` -1.2 A partir da pasta src execute o comando: +ou + +``` +$var = (Get-Date).ToString("yyyyMMdd-HHmmss"); dotnet-coverage collect "dotnet test" -f xml -o ".\DEPLOY.Cachorro.Api.Tests\TestResults\DotnetCoverageCollect\$var\coverage.cobertura.xml" +``` ``` -reportgenerator -reports:C:/Proj/DEPLOY/DEPLOY.Cachorro/src/DEPLOY.Cachorro.Api.Tests/TestResults/**/*cobertura.xml -targetdir:C:/Proj/DEPLOY/DEPLOY.Cachorro/src/DEPLOY.Cachorro.Api.Tests/coveragereport -reporttypes:"Html;SonarQube;JsonSummary;Badges" -verbosity:Verbose -title:Cachorro.API -tag:canal-deploy +reportgenerator -reports:.\DEPLOY.Cachorro.Api.Tests\TestResults\DotnetCoverageCollect\**\coverage.cobertura.xml -targetdir:./DEPLOY.Cachorro.Api.Tests/TestResults/DotnetCoverageCollect/CoverageReport -reporttypes:"Html;SonarQube;JsonSummary;Badges" -verbosity:Verbose -title:Cachorro.API -tag:canal-deploy ``` +1.2 A partir da pasta src execute o comando: + --- # EntityFramework Commands ``` + dotnet tool install --global dotnet-ef + ``` ``` + dotnet ef migrations add InitDatabaseAPI -s DEPLOY.Cachorro.Api -p DEPLOY.Cachorro.Repository -c DEPLOY.Cachorro.Repository.CachorroDbContext --output-dir Migrations/API -v + ``` ``` + dotnet ef database update InitDatabaseAPI --startup-project DEPLOY.Cachorro.Api --project DEPLOY.Cachorro.Repository --context DEPLOY.Cachorro.Repository.CachorroDbContext --verbose + ``` Connection String ``` + Data Source=127.0.0.1,1433;Initial Catalog=Cachorro;User Id=sa;Password=Abcd1234%;Integrated Security=False;MultipleActiveResultSets=True;TrustServerCertificate=true; + ```
@@ -104,37 +153,185 @@ Data Source=127.0.0.1,1433;Initial Catalog=Cachorro;User Id=sa;Password=Abcd1234 # Link de documentações citadas durante a criaçao do projeto ``` +https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet +``` + +``` + https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-7.0 + ``` ``` + https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-7.0&tabs=windows + ``` ILogger ``` + https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-7.0 + +``` + +``` + +https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-test + ``` ``` + https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger?tabs=dotnet6 + ``` Application Insights ``` + https://learn.microsoft.com/pt-br/azure/azure-monitor/app/asp-net-core?tabs=netcorenew%2Cnetcore6 + ``` Live Stream / Live Metrics ``` + https://learn.microsoft.com/en-us/azure/azure-monitor/app/live-stream?tabs=dotnet6 + ``` Configurations ``` + https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-7.0 + ``` + +Docker Commands + +``` + +docker container rm -f $(docker ps -a -q) + +``` + +``` + +docker volume rm $(docker volume ls -q)\ + +``` + +``` + +docker image rm -f $(docker image ls -a -q) + +``` + +``` + +docker system prune + +``` + +``` + +docker image ls + +``` + +``` + +docker tag 430f5d9f4229 crcanaldeploydev.azurecr.io/cachorro.api:latest + +``` + +``` + +docker push crcanaldeploydev.azurecr.io/cachorro.api:latest + +``` + +``` + +docker push ghcr.io/felipementel/cachorro.api:latest + +``` + +``` + +docker container run -p 8088:80 crcanaldeploydev.azurecr.io/cachorro.api:latest + +``` + +``` + +docker container run --rm --name sonar-canal-deploy -p 9044:9000 sonarqube:10.3.0-community + +``` + +Comandos Azure Key Vault + +``` + +az keyvault secret show --name CachorroConnectionString --vault-name kv-canaldeploy-dev + +``` + +``` + +@Microsoft.KeyVault(SecretUri=https://kv-canaldeploy-dev.vault.azure.net/secrets/ConnectionStrings--ApplicationInsights/) + +``` + +``` + +@Microsoft.KeyVault(VaultName=kv-canaldeploy-dev;SecretName=ConnectionStrings--ApplicationInsights) + +``` + +Comandos Azure Container Instance + +``` + +az container logs --resource-group rg-canaldeploy-dev --name api-cachorro + +``` + +``` + +az container exec -g g-canaldeploy-dev --name cachorro.api --exec-command "/bin/bash" + +``` + +/d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml + +``` + +``` + +#git +#github +#github-actions +#sonar-qube +#wsl-2 +#docker +#docker-compose +#docker-hub +#donet +#ef-core +#entity-framework-core +#x-unit +#postman +#azure +#azure-application-ingishts +#azure-sql-database +#azure-app-configuration +#azure-container-registry +#azure-container-instance +#azure-container-apps +#azure-key-vault diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 1d5fcaf..179b64f 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -43,6 +43,16 @@ services: ports: - 1433:1433 + sonarqube: + image: sonarqube:10.3.0-community + hostname: sonar-canal-deploy + container_name: sonar-canal-deploy + restart: always + networks: + - cachorro-net + ports: + - 9044:9000 + networks: cachorro-net: driver: bridge diff --git a/src/DEPLOY.Cachorro.Api/DEPLOY.Cachorro.Api.csproj b/src/DEPLOY.Cachorro.Api/DEPLOY.Cachorro.Api.csproj index 894d306..a2d8d7b 100644 --- a/src/DEPLOY.Cachorro.Api/DEPLOY.Cachorro.Api.csproj +++ b/src/DEPLOY.Cachorro.Api/DEPLOY.Cachorro.Api.csproj @@ -9,7 +9,7 @@ true Linux /subscriptions/f4574f09-7fab-45bd-8eb5-508b8c6aa04a/resourceGroups/rg-canaldeploy-dev/providers/microsoft.insights/components/appi-cachorroapi-dev - 1.1.0 + 1.1.1 diff --git a/src/DEPLOY.Cachorro.Api/Extensions/Telemetria/AppInsightsExtensions.cs b/src/DEPLOY.Cachorro.Api/Extensions/Telemetria/AppInsightsExtensions.cs index 20800cd..25d7cc0 100644 --- a/src/DEPLOY.Cachorro.Api/Extensions/Telemetria/AppInsightsExtensions.cs +++ b/src/DEPLOY.Cachorro.Api/Extensions/Telemetria/AppInsightsExtensions.cs @@ -14,17 +14,17 @@ public static void AddTelemetriaExtension( services .AddApplicationInsightsTelemetry(options => { - options.ConnectionString = configuration.GetSection("ConnectionsString:ApplicationInsights").Value; + options.ConnectionString = configuration.GetSection("ConnectionStrings:ApplicationInsights").Value; }) .ConfigureTelemetryModule((module, o) => { module.AuthenticationApiKey = configuration.GetSection("ApplicationInsights:ApiKey").Value; - }); - + }); + services .ConfigureTelemetryModule((module, o) => { - module.EnableSqlCommandTextInstrumentation = true; + module.EnableSqlCommandTextInstrumentation = true; }); } } diff --git a/src/DEPLOY.Cachorro.Api/Program.cs b/src/DEPLOY.Cachorro.Api/Program.cs index 709953a..e22fdd3 100644 --- a/src/DEPLOY.Cachorro.Api/Program.cs +++ b/src/DEPLOY.Cachorro.Api/Program.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.Azure; using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; -using Azure.Identity; namespace DEPLOY.Cachorro.Api {