Skip to content

Commit e09b594

Browse files
authored
ApiGateway: .NET 8 (#626)
* ApiGateway: .NET 8 * wip * wip * wip * wip * wip * wip
1 parent d95cfff commit e09b594

File tree

11 files changed

+159
-153
lines changed

11 files changed

+159
-153
lines changed

HwProj.APIGateway/HwProj.APIGateway.API/ApplicationProfile.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using AutoMapper;
2-
using HwProj.Models.AuthService.DTO;
32
using HwProj.Models.AuthService.ViewModels;
4-
using HwProj.Models.CoursesService;
53
using HwProj.Models.CoursesService.DTO;
6-
using HwProj.Models.CoursesService.ViewModels;
74

85
namespace HwProj.APIGateway.API
96
{

HwProj.APIGateway/HwProj.APIGateway.API/Controllers/SolutionsController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Net;
55
using System.Threading.Tasks;
66
using HwProj.APIGateway.API.ExceptionFilters;
7-
using HwProj.APIGateway.API.Extensions;
87
using HwProj.APIGateway.API.Models.Solutions;
98
using HwProj.AuthService.Client;
109
using HwProj.CoursesService.Client;

HwProj.APIGateway/HwProj.APIGateway.API/Controllers/TasksController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using HwProj.Models.CoursesService.ViewModels;
55
using HwProj.Models.Result;
66
using HwProj.Models.Roles;
7-
using HwProj.Utils.Authorization;
87
using Microsoft.AspNetCore.Authorization;
98
using Microsoft.AspNetCore.Mvc;
109

HwProj.APIGateway/HwProj.APIGateway.API/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ COPY ["HwProj.SolutionsService/HwProj.SolutionsService.Client/", "HwProj.Solutio
1616
COPY ["HwProj.Common/HwProj.Exceptions/", "HwProj.Common/HwProj.Exceptions/"]
1717
COPY ["HwProj.ContentService/HwProj.ContentService.Client/", "HwProj.ContentService/HwProj.ContentService.Client/"]
1818
COPY ["HwProj.StudentInfo/IStudentsInfo/", "HwProj.StudentInfo/IStudentsInfo/"]
19-
COPY ["HwProj.StudentInfo/StudentsInfo.Tests/", "HwProj.StudentInfo/StudentsInfo.Tests/"]
2019
COPY ["HwProj.StudentInfo/StudentsInfo/", "HwProj.StudentInfo/StudentsInfo/"]
2120

2221
WORKDIR "/src/HwProj.APIGateway/HwProj.APIGateway.API"

HwProj.APIGateway/HwProj.APIGateway.API/Extensions/LinqExtensions.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<DockerComposeProjectPath>..\..\docker-compose.dcproj</DockerComposeProjectPath>
66
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
77
<DockerfileContext>..\..</DockerfileContext>
8-
<LangVersion>$(CSharpLanguageVersion)</LangVersion>
98
<Nullable>$(NullableReferenceTypes)</Nullable>
109
</PropertyGroup>
1110

12-
<ItemGroup>
13-
<PackageReference Include="Microsoft.AspNetCore.All" />
14-
<PackageReference Include="Microsoft.AspNetCore.App" />
15-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.3" />
16-
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.13" />
17-
</ItemGroup>
18-
1911
<ItemGroup>
2012
<ProjectReference Include="..\..\HwProj.AuthService\HwProj.AuthService.Client\HwProj.AuthService.Client.csproj" />
21-
<ProjectReference Include="..\..\HwProj.Common\HwProj.Utils\HwProj.Utils.csproj" />
2213
<ProjectReference Include="..\..\HwProj.ContentService\HwProj.ContentService.Client\HwProj.ContentService.Client.csproj" />
2314
<ProjectReference Include="..\..\HwProj.CoursesService\HwProj.CoursesService.Client\HwProj.CoursesService.Client.csproj" />
2415
<ProjectReference Include="..\..\HwProj.NotificationsService\HwProj.NotificationsService.Client\HwProj.NotificationsService.Client.csproj" />
2516
<ProjectReference Include="..\..\HwProj.SolutionsService\HwProj.SolutionsService.Client\HwProj.SolutionsService.Client.csproj" />
2617
<ProjectReference Include="..\..\HwProj.StudentInfo\IStudentsInfo\IStudentsInfo.csproj" />
27-
<ProjectReference Include="..\..\HwProj.StudentInfo\StudentsInfo.Tests\StudentsInfo.Tests.csproj" />
2818
<ProjectReference Include="..\..\HwProj.StudentInfo\StudentsInfo\StudentsInfo.csproj" />
2919
</ItemGroup>
20+
21+
<ItemGroup>
22+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.20" />
23+
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" />
24+
<PackageReference Include="AutoMapper" Version="15.0.1" />
25+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.14.0" />
26+
</ItemGroup>
3027
</Project>

HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
using System.Text;
1+
using System.Collections.Generic;
2+
using System.Text;
3+
using System.Text.Json.Serialization;
24
using HwProj.AuthService.Client;
35
using HwProj.ContentService.Client;
46
using HwProj.CoursesService.Client;
57
using HwProj.NotificationsService.Client;
68
using HwProj.SolutionsService.Client;
7-
using HwProj.Utils.Configuration;
89
using HwProj.APIGateway.API.Filters;
910
using Microsoft.AspNetCore.Builder;
10-
using Microsoft.AspNetCore.Hosting;
1111
using Microsoft.AspNetCore.Http.Features;
1212
using Microsoft.Extensions.Configuration;
1313
using Microsoft.Extensions.DependencyInjection;
14-
using Microsoft.IdentityModel.Tokens;
1514
using IStudentsInfo;
1615
using Microsoft.AspNetCore.Authentication.JwtBearer;
16+
using Microsoft.Extensions.Hosting;
17+
using Microsoft.IdentityModel.Tokens;
18+
using Microsoft.OpenApi.Models;
1719
using StudentsInfo;
1820

1921
namespace HwProj.APIGateway.API
@@ -29,8 +31,17 @@ public Startup(IConfiguration configuration)
2931

3032
public void ConfigureServices(IServiceCollection services)
3133
{
34+
services
35+
.AddCors()
36+
.AddControllers()
37+
.AddJsonOptions(options =>
38+
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
39+
40+
services.AddHttpContextAccessor();
41+
services.AddAutoMapper(x => x.AddProfile<ApplicationProfile>());
3242
services.Configure<FormOptions>(options => { options.MultipartBodyLengthLimit = 200 * 1024 * 1024; });
33-
services.ConfigureHwProjServices("API Gateway");
43+
ConfigureHwProjServiceSwaggerGen(services);
44+
3445
services.AddSingleton<IStudentsInformationProvider>(provider =>
3546
new StudentsInformationProvider(Configuration["StudentsInfo:Login"],
3647
Configuration["StudentsInfo:Password"],
@@ -71,9 +82,66 @@ public void ConfigureServices(IServiceCollection services)
7182
services.AddScoped<CourseMentorOnlyAttribute>();
7283
}
7384

74-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
85+
public void Configure(IApplicationBuilder app, IHostEnvironment env)
86+
{
87+
if (env.IsDevelopment())
88+
{
89+
app.UseDeveloperExceptionPage()
90+
.UseSwagger()
91+
.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API Gateway"); });
92+
}
93+
else
94+
{
95+
app.UseHsts();
96+
}
97+
98+
app.UseRouting();
99+
app.UseAuthentication();
100+
app.UseAuthorization();
101+
app.UseCors(x => x
102+
.AllowAnyMethod()
103+
.AllowAnyHeader()
104+
.SetIsOriginAllowed(_ => true)
105+
.AllowCredentials());
106+
107+
app.UseEndpoints(x => x.MapControllers());
108+
}
109+
110+
private static void ConfigureHwProjServiceSwaggerGen(IServiceCollection services)
75111
{
76-
app.ConfigureHwProj(env, "API Gateway");
112+
services.AddSwaggerGen(c =>
113+
{
114+
c.SwaggerDoc("v1", new OpenApiInfo { Title = "API Gateway", Version = "v1" });
115+
c.CustomOperationIds(apiDesc =>
116+
{
117+
var controllerName = apiDesc.ActionDescriptor.RouteValues["controller"];
118+
var actionName = apiDesc.ActionDescriptor.RouteValues["action"];
119+
return $"{controllerName}{actionName}";
120+
});
121+
c.AddSecurityDefinition("Bearer",
122+
new OpenApiSecurityScheme
123+
{
124+
In = ParameterLocation.Header,
125+
Description = "Please enter into field the word 'Bearer' following by space and JWT",
126+
Name = "Authorization",
127+
Type = SecuritySchemeType.ApiKey
128+
});
129+
c.AddSecurityRequirement(
130+
new OpenApiSecurityRequirement
131+
{
132+
{
133+
new OpenApiSecurityScheme
134+
{
135+
Reference = new OpenApiReference
136+
{
137+
Id = "Bearer",
138+
Type = ReferenceType.SecurityScheme
139+
}
140+
},
141+
new List<string>()
142+
}
143+
});
144+
});
77145
}
78146
}
79147
}

HwProj.AuthService/HwProj.AuthService.API/Startup.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Builder;
1+
using System.Text.Json.Serialization;
2+
using Microsoft.AspNetCore.Builder;
23
using Microsoft.Extensions.Configuration;
34
using Microsoft.Extensions.DependencyInjection;
45
using HwProj.AuthService.API.Models;
@@ -25,7 +26,13 @@ public Startup(IConfiguration configuration)
2526

2627
public void ConfigureServices(IServiceCollection services)
2728
{
28-
services.ConfigureHwProjServices("AuthService API");
29+
services
30+
.AddCors()
31+
.AddControllers()
32+
.AddJsonOptions(options =>
33+
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
34+
35+
services.AddAutoMapper(x => x.AddProfile<ApplicationProfile>());
2936
services.AddHttpClient();
3037

3138
var connectionString = ConnectionString.GetConnectionString(Configuration);
@@ -57,7 +64,20 @@ public void ConfigureServices(IServiceCollection services)
5764

5865
public void Configure(IApplicationBuilder app, IHostEnvironment env, IdentityContext context)
5966
{
60-
app.ConfigureHwProj(env, "AuthService API", context);
67+
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
68+
else app.UseHsts();
69+
70+
app.UseRouting();
71+
app.UseAuthentication();
72+
app.UseCors(x => x
73+
.AllowAnyMethod()
74+
.AllowAnyHeader()
75+
.SetIsOriginAllowed(_ => true)
76+
.AllowCredentials());
77+
78+
app.UseEndpoints(x => x.MapControllers());
79+
80+
app.UseDatabase(env, context);
6181

6282
using var scope = app.ApplicationServices.CreateScope();
6383
var userManager = scope.ServiceProvider.GetService<UserManager<User>>();

HwProj.Common/HwProj.Common.Net8/HwProj.Common.Net8.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
<ItemGroup>
99
<PackageReference Include="AutoMapper" Version="15.0.1" />
10+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
1011
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.9" />
11-
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" />
12+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.9" />
1213
</ItemGroup>
1314
</Project>
Lines changed: 16 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,38 @@
1-
using System.Reflection;
2-
using System.Text.Json.Serialization;
3-
using Microsoft.AspNetCore.Builder;
1+
using Microsoft.AspNetCore.Builder;
42
using Microsoft.EntityFrameworkCore;
53
using Microsoft.Extensions.DependencyInjection;
64
using Microsoft.Extensions.Hosting;
75
using Microsoft.Extensions.Logging;
8-
using Microsoft.OpenApi.Models;
96

107
namespace HwProj.Common.Net8;
118

129
public static class StartupExtensions
1310
{
14-
public static IServiceCollection ConfigureHwProjServices(this IServiceCollection services, string serviceName)
15-
{
16-
services.AddAutoMapper(x => x.AddMaps(Assembly.GetCallingAssembly()))
17-
.AddCors()
18-
.AddControllers()
19-
.AddJsonOptions(options =>
20-
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
21-
22-
services.ConfigureHwProjServiceSwaggerGen(serviceName);
23-
services.AddHttpContextAccessor();
24-
25-
return services;
26-
}
27-
28-
private static void ConfigureHwProjServiceSwaggerGen(this IServiceCollection services, string serviceName)
29-
{
30-
services.AddSwaggerGen(c =>
31-
{
32-
c.SwaggerDoc("v1", new OpenApiInfo { Title = serviceName, Version = "v1" });
33-
c.CustomOperationIds(apiDesc =>
34-
{
35-
var controllerName = apiDesc.ActionDescriptor.RouteValues["controller"];
36-
var actionName = apiDesc.ActionDescriptor.RouteValues["action"];
37-
return $"{controllerName}{actionName}";
38-
});
39-
});
40-
}
41-
42-
public static IApplicationBuilder ConfigureHwProj(this IApplicationBuilder app, IHostEnvironment env,
43-
string serviceName, DbContext? context = null)
11+
public static IApplicationBuilder UseDatabase(this IApplicationBuilder app, IHostEnvironment env, DbContext? context = null)
4412
{
13+
if (context == null) return app;
4514
if (env.IsDevelopment())
4615
{
47-
app.UseDeveloperExceptionPage()
48-
.UseSwagger()
49-
.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", serviceName); });
50-
}
51-
else
52-
{
53-
app.UseHsts();
16+
context.Database.EnsureCreated();
17+
return app;
5418
}
5519

56-
app.UseRouting();
57-
app.UseAuthentication();
58-
app.UseCors(x => x
59-
.AllowAnyMethod()
60-
.AllowAnyHeader()
61-
.SetIsOriginAllowed(_ => true)
62-
.AllowCredentials());
20+
var logger = app.ApplicationServices
21+
.GetService<ILoggerFactory>()!
22+
.CreateLogger(typeof(StartupExtensions));
6323

64-
app.UseEndpoints(x => x.MapControllers());
24+
var tries = 0;
25+
const int maxTries = 100;
6526

66-
if (context != null)
27+
while (!context.Database.CanConnect() && ++tries <= maxTries)
6728
{
68-
if (env.IsDevelopment())
69-
{
70-
context.Database.EnsureCreated();
71-
return app;
72-
}
73-
74-
var logger = app.ApplicationServices
75-
.GetService<ILoggerFactory>()!
76-
.CreateLogger(typeof(StartupExtensions));
77-
78-
var tries = 0;
79-
const int maxTries = 100;
80-
81-
while (!context.Database.CanConnect() && ++tries <= maxTries)
82-
{
83-
logger.LogWarning($"Can't connect to database. Try {tries}.");
84-
Thread.Sleep(5000);
85-
}
86-
87-
if (tries > maxTries) throw new Exception("Can't connect to database");
88-
context.Database.Migrate();
29+
logger.LogWarning($"Can't connect to database. Try {tries}.");
30+
Thread.Sleep(5000);
8931
}
9032

33+
if (tries > maxTries) throw new Exception("Can't connect to database");
34+
context.Database.Migrate();
35+
9136
return app;
9237
}
9338
}

0 commit comments

Comments
 (0)