Skip to content

Commit

Permalink
Realised Api Gateway, configured docker compose
Browse files Browse the repository at this point in the history
  • Loading branch information
Belgrak committed Dec 20, 2024
1 parent e68b75d commit 58d0735
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 15 deletions.
30 changes: 30 additions & 0 deletions Gateway/Gateway.Api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# См. статью по ссылке https://aka.ms/customizecontainer, чтобы узнать как настроить контейнер отладки и как Visual Studio использует этот Dockerfile для создания образов для ускорения отладки.

# Этот этап используется при запуске из VS в быстром режиме (по умолчанию для конфигурации отладки)
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081


# Этот этап используется для сборки проекта службы
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Gateway/Gateway.Api/Gateway.Api.csproj", "Gateway/Gateway.Api/"]
RUN dotnet restore "./Gateway/Gateway.Api/Gateway.Api.csproj"
COPY . .
WORKDIR "/src/Gateway/Gateway.Api"
RUN dotnet build "./Gateway.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build

# Этот этап используется для публикации проекта службы, который будет скопирован на последний этап
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Gateway.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# Этот этап используется в рабочей среде или при запуске из VS в обычном режиме (по умолчанию, когда конфигурация отладки не используется)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Gateway.Api.dll"]
19 changes: 19 additions & 0 deletions Gateway/Gateway.Api/Gateway.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>bc1b7e89-5cda-4112-8b7c-ff0162e59ce3</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.1.0" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.2.0" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions Gateway/Gateway.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenApi();
builder.Services.AddSwaggerGen();

builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.MapReverseProxy();

app.Run();
31 changes: 31 additions & 0 deletions Gateway/Gateway.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"profiles": {
"http": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5064"
},
"https": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7265;http://localhost:5064"
},
"Container (Dockerfile)": {
"commandName": "Docker",
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": {
"ASPNETCORE_HTTPS_PORTS": "8081",
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true,
"useSSL": true
}
},
"$schema": "https://json.schemastore.org/launchsettings.json"
}
8 changes: 8 additions & 0 deletions Gateway/Gateway.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
29 changes: 29 additions & 0 deletions Gateway/Gateway.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"CoreService": {
"ClusterId": "coreServiceCluster",
"Match": { "Path": "/core-api/{**catch-all}" },
"Transforms": [
{
"PathPattern": "/api/{**catch-all}"
}
]
}
},
"Clusters": {
"coreServiceCluster": {
"Destinations": {
"CoreService": { "Address": "http://core.api:8080/" }
}
}
}
}
}
9 changes: 9 additions & 0 deletions PracticesService.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreService.Api", "Services
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Services", "Services", "{89CC78B4-9A7B-46F4-B786-7FB0D49911B2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Gateway", "Gateway", "{222D815F-4394-417D-AF31-DA86AE6E21C1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gateway.Api", "Gateway\Gateway.Api\Gateway.Api.csproj", "{C5116E8B-8D09-406D-80CD-0039B4AC5B68}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -23,12 +27,17 @@ Global
{94A409B2-9D11-41F4-9BB8-6B89AC26764E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94A409B2-9D11-41F4-9BB8-6B89AC26764E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94A409B2-9D11-41F4-9BB8-6B89AC26764E}.Release|Any CPU.Build.0 = Release|Any CPU
{C5116E8B-8D09-406D-80CD-0039B4AC5B68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C5116E8B-8D09-406D-80CD-0039B4AC5B68}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5116E8B-8D09-406D-80CD-0039B4AC5B68}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5116E8B-8D09-406D-80CD-0039B4AC5B68}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{94A409B2-9D11-41F4-9BB8-6B89AC26764E} = {89CC78B4-9A7B-46F4-B786-7FB0D49911B2}
{C5116E8B-8D09-406D-80CD-0039B4AC5B68} = {222D815F-4394-417D-AF31-DA86AE6E21C1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DC6C6DEA-3EB6-439A-AE03-43698361909A}
Expand Down
9 changes: 0 additions & 9 deletions Services/CoreService.Api/Core/CoreContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,6 @@ public CoreContext(DbContextOptions<CoreContext> options)
/// </summary>
public virtual DbSet<Theme> Themes { get; set; }

/// <summary>
/// On configuring method.
/// </summary>
/// <param name="optionsBuilder">Db context options builder.</param>
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("Host=localhost:5435;Database=postgres;Username=postgres;Password=postgres");
}

/// <summary>
/// On model creating method.
/// </summary>
Expand Down
24 changes: 18 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
version: "3.9"
name: practices-service
name: "practices-service"

services:
gateway.api:
image: ${DOCKER_REGISTRY-}gatewayapi
environment:
- ASPNETCORE_ENVIRONMENT=Development
build:
context: .
dockerfile: Gateway/Gateway.Api/Dockerfile
ports:
- "5000:8080"
networks:
- proxybackend
core.db:
container_name: core_db
container_name: core.db
image: postgres:14.5-alpine
volumes:
- ./Services/CoreService.Api/init-db/:/docker-entrypoint-initdb.d
Expand All @@ -19,9 +30,10 @@ services:
interval: 5s
timeout: 5s
retries: 5
networks:
- proxybackend

core:
container_name: core
core.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ENVIRONMENT=Docker
Expand All @@ -32,7 +44,7 @@ services:
volumes:
- './:/core'
ports:
- "5000:8080"
- "5001:8080"
depends_on:
core.db:
condition: service_healthy
Expand All @@ -42,4 +54,4 @@ services:
networks:
proxybackend:
name: proxybackend
driver: bridge
driver: bridge

0 comments on commit 58d0735

Please sign in to comment.