Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions HwProj.APIGateway/HwProj.APIGateway.API/ApplicationProfile.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using AutoMapper;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.AuthService.ViewModels;
using HwProj.Models.CoursesService;
using HwProj.Models.CoursesService.DTO;
using HwProj.Models.CoursesService.ViewModels;

namespace HwProj.APIGateway.API
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Net;
using System.Threading.Tasks;
using HwProj.APIGateway.API.ExceptionFilters;
using HwProj.APIGateway.API.Extensions;
using HwProj.APIGateway.API.Models.Solutions;
using HwProj.AuthService.Client;
using HwProj.CoursesService.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using HwProj.Models.CoursesService.ViewModels;
using HwProj.Models.Result;
using HwProj.Models.Roles;
using HwProj.Utils.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down
1 change: 0 additions & 1 deletion HwProj.APIGateway/HwProj.APIGateway.API/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ COPY ["HwProj.SolutionsService/HwProj.SolutionsService.Client/", "HwProj.Solutio
COPY ["HwProj.Common/HwProj.Exceptions/", "HwProj.Common/HwProj.Exceptions/"]
COPY ["HwProj.ContentService/HwProj.ContentService.Client/", "HwProj.ContentService/HwProj.ContentService.Client/"]
COPY ["HwProj.StudentInfo/IStudentsInfo/", "HwProj.StudentInfo/IStudentsInfo/"]
COPY ["HwProj.StudentInfo/StudentsInfo.Tests/", "HwProj.StudentInfo/StudentsInfo.Tests/"]
COPY ["HwProj.StudentInfo/StudentsInfo/", "HwProj.StudentInfo/StudentsInfo/"]

WORKDIR "/src/HwProj.APIGateway/HwProj.APIGateway.API"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<DockerComposeProjectPath>..\..\docker-compose.dcproj</DockerComposeProjectPath>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
<LangVersion>$(CSharpLanguageVersion)</LangVersion>
<Nullable>$(NullableReferenceTypes)</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.3" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.13" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\HwProj.AuthService\HwProj.AuthService.Client\HwProj.AuthService.Client.csproj" />
<ProjectReference Include="..\..\HwProj.Common\HwProj.Utils\HwProj.Utils.csproj" />
<ProjectReference Include="..\..\HwProj.ContentService\HwProj.ContentService.Client\HwProj.ContentService.Client.csproj" />
<ProjectReference Include="..\..\HwProj.CoursesService\HwProj.CoursesService.Client\HwProj.CoursesService.Client.csproj" />
<ProjectReference Include="..\..\HwProj.NotificationsService\HwProj.NotificationsService.Client\HwProj.NotificationsService.Client.csproj" />
<ProjectReference Include="..\..\HwProj.SolutionsService\HwProj.SolutionsService.Client\HwProj.SolutionsService.Client.csproj" />
<ProjectReference Include="..\..\HwProj.StudentInfo\IStudentsInfo\IStudentsInfo.csproj" />
<ProjectReference Include="..\..\HwProj.StudentInfo\StudentsInfo.Tests\StudentsInfo.Tests.csproj" />
<ProjectReference Include="..\..\HwProj.StudentInfo\StudentsInfo\StudentsInfo.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.20" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" />
<PackageReference Include="AutoMapper" Version="15.0.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.14.0" />
</ItemGroup>
</Project>
82 changes: 75 additions & 7 deletions HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using System.Text;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
using HwProj.AuthService.Client;
using HwProj.ContentService.Client;
using HwProj.CoursesService.Client;
using HwProj.NotificationsService.Client;
using HwProj.SolutionsService.Client;
using HwProj.Utils.Configuration;
using HwProj.APIGateway.API.Filters;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using IStudentsInfo;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using StudentsInfo;

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

public void ConfigureServices(IServiceCollection services)
{
services
.AddCors()
.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);

services.AddHttpContextAccessor();
services.AddAutoMapper(x => x.AddProfile<ApplicationProfile>());
services.Configure<FormOptions>(options => { options.MultipartBodyLengthLimit = 200 * 1024 * 1024; });
services.ConfigureHwProjServices("API Gateway");
ConfigureHwProjServiceSwaggerGen(services);

services.AddSingleton<IStudentsInformationProvider>(provider =>
new StudentsInformationProvider(Configuration["StudentsInfo:Login"],
Configuration["StudentsInfo:Password"],
Expand Down Expand Up @@ -71,9 +82,66 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<CourseMentorOnlyAttribute>();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage()
.UseSwagger()
.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API Gateway"); });
}
else
{
app.UseHsts();
}

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(_ => true)
.AllowCredentials());

app.UseEndpoints(x => x.MapControllers());
}

private static void ConfigureHwProjServiceSwaggerGen(IServiceCollection services)
{
app.ConfigureHwProj(env, "API Gateway");
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "API Gateway", Version = "v1" });
c.CustomOperationIds(apiDesc =>
{
var controllerName = apiDesc.ActionDescriptor.RouteValues["controller"];
var actionName = apiDesc.ActionDescriptor.RouteValues["action"];
return $"{controllerName}{actionName}";
});
c.AddSecurityDefinition("Bearer",
new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please enter into field the word 'Bearer' following by space and JWT",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(
new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Id = "Bearer",
Type = ReferenceType.SecurityScheme
}
},
new List<string>()
}
});
});
}
}
}
26 changes: 23 additions & 3 deletions HwProj.AuthService/HwProj.AuthService.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using HwProj.AuthService.API.Models;
Expand All @@ -25,7 +26,13 @@ public Startup(IConfiguration configuration)

public void ConfigureServices(IServiceCollection services)
{
services.ConfigureHwProjServices("AuthService API");
services
.AddCors()
.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);

services.AddAutoMapper(x => x.AddProfile<ApplicationProfile>());
services.AddHttpClient();

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

public void Configure(IApplicationBuilder app, IHostEnvironment env, IdentityContext context)
{
app.ConfigureHwProj(env, "AuthService API", context);
if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
else app.UseHsts();

app.UseRouting();
app.UseAuthentication();
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(_ => true)
.AllowCredentials());

app.UseEndpoints(x => x.MapControllers());

app.UseDatabase(env, context);

using var scope = app.ApplicationServices.CreateScope();
var userManager = scope.ServiceProvider.GetService<UserManager<User>>();
Expand Down
3 changes: 2 additions & 1 deletion HwProj.Common/HwProj.Common.Net8/HwProj.Common.Net8.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

<ItemGroup>
<PackageReference Include="AutoMapper" Version="15.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.9" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.4" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.9" />
</ItemGroup>
</Project>
87 changes: 16 additions & 71 deletions HwProj.Common/HwProj.Common.Net8/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,93 +1,38 @@
using System.Reflection;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;

namespace HwProj.Common.Net8;

public static class StartupExtensions
{
public static IServiceCollection ConfigureHwProjServices(this IServiceCollection services, string serviceName)
{
services.AddAutoMapper(x => x.AddMaps(Assembly.GetCallingAssembly()))
.AddCors()
.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);

services.ConfigureHwProjServiceSwaggerGen(serviceName);
services.AddHttpContextAccessor();

return services;
}

private static void ConfigureHwProjServiceSwaggerGen(this IServiceCollection services, string serviceName)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = serviceName, Version = "v1" });
c.CustomOperationIds(apiDesc =>
{
var controllerName = apiDesc.ActionDescriptor.RouteValues["controller"];
var actionName = apiDesc.ActionDescriptor.RouteValues["action"];
return $"{controllerName}{actionName}";
});
});
}

public static IApplicationBuilder ConfigureHwProj(this IApplicationBuilder app, IHostEnvironment env,
string serviceName, DbContext? context = null)
public static IApplicationBuilder UseDatabase(this IApplicationBuilder app, IHostEnvironment env, DbContext? context = null)
{
if (context == null) return app;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage()
.UseSwagger()
.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", serviceName); });
}
else
{
app.UseHsts();
context.Database.EnsureCreated();
return app;
}

app.UseRouting();
app.UseAuthentication();
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(_ => true)
.AllowCredentials());
var logger = app.ApplicationServices
.GetService<ILoggerFactory>()!
.CreateLogger(typeof(StartupExtensions));

app.UseEndpoints(x => x.MapControllers());
var tries = 0;
const int maxTries = 100;

if (context != null)
while (!context.Database.CanConnect() && ++tries <= maxTries)
{
if (env.IsDevelopment())
{
context.Database.EnsureCreated();
return app;
}

var logger = app.ApplicationServices
.GetService<ILoggerFactory>()!
.CreateLogger(typeof(StartupExtensions));

var tries = 0;
const int maxTries = 100;

while (!context.Database.CanConnect() && ++tries <= maxTries)
{
logger.LogWarning($"Can't connect to database. Try {tries}.");
Thread.Sleep(5000);
}

if (tries > maxTries) throw new Exception("Can't connect to database");
context.Database.Migrate();
logger.LogWarning($"Can't connect to database. Try {tries}.");
Thread.Sleep(5000);
}

if (tries > maxTries) throw new Exception("Can't connect to database");
context.Database.Migrate();

return app;
}
}
Loading
Loading