1- using System . Text ;
1+ using System . Collections . Generic ;
2+ using System . Text ;
3+ using System . Text . Json . Serialization ;
24using HwProj . AuthService . Client ;
35using HwProj . ContentService . Client ;
46using HwProj . CoursesService . Client ;
57using HwProj . NotificationsService . Client ;
68using HwProj . SolutionsService . Client ;
7- using HwProj . Utils . Configuration ;
89using HwProj . APIGateway . API . Filters ;
910using Microsoft . AspNetCore . Builder ;
10- using Microsoft . AspNetCore . Hosting ;
1111using Microsoft . AspNetCore . Http . Features ;
1212using Microsoft . Extensions . Configuration ;
1313using Microsoft . Extensions . DependencyInjection ;
14- using Microsoft . IdentityModel . Tokens ;
1514using IStudentsInfo ;
1615using Microsoft . AspNetCore . Authentication . JwtBearer ;
16+ using Microsoft . Extensions . Hosting ;
17+ using Microsoft . IdentityModel . Tokens ;
18+ using Microsoft . OpenApi . Models ;
1719using StudentsInfo ;
1820
1921namespace 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}
0 commit comments