2020-12-06 16:01:38 +01:00
|
|
|
using System;
|
2020-12-03 13:24:47 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-12-06 18:31:05 +01:00
|
|
|
using System.Text;
|
2020-12-03 13:24:47 +01:00
|
|
|
using System.Threading.Tasks;
|
2020-12-06 18:31:05 +01:00
|
|
|
using AutoMapper;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2020-12-03 13:24:47 +01:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.HttpsPolicy;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-12-06 16:01:38 +01:00
|
|
|
using Microsoft.Extensions.Hosting;
|
2020-12-03 13:24:47 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-12-06 18:31:05 +01:00
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2020-12-06 16:01:38 +01:00
|
|
|
using Microsoft.OpenApi.Models;
|
2020-12-16 16:26:13 +01:00
|
|
|
using Serwer.Api.Framework;
|
2020-12-06 16:01:38 +01:00
|
|
|
using Serwer.Core.Repositories;
|
2020-12-06 18:31:05 +01:00
|
|
|
using Serwer.Infrastructure.Mappers;
|
2020-12-06 16:01:38 +01:00
|
|
|
using Serwer.Infrastructure.Repositories;
|
|
|
|
using Serwer.Infrastructure.Services;
|
2020-12-06 18:31:05 +01:00
|
|
|
using Serwer.Infrastructure.Settings;
|
2020-12-03 13:24:47 +01:00
|
|
|
|
2020-12-06 16:01:38 +01:00
|
|
|
namespace Serwer.Api
|
2020-12-03 13:24:47 +01:00
|
|
|
{
|
|
|
|
public class Startup
|
|
|
|
{
|
2020-12-12 18:11:56 +01:00
|
|
|
public Startup(IConfiguration configuration, IWebHostEnvironment env)
|
2020-12-03 13:24:47 +01:00
|
|
|
{
|
|
|
|
Configuration = configuration;
|
2020-12-12 18:11:56 +01:00
|
|
|
WebRootPath = env.WebRootPath;
|
2020-12-03 13:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
2020-12-12 18:11:56 +01:00
|
|
|
protected string WebRootPath { get; set; }
|
2020-12-03 13:24:47 +01:00
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
2020-12-06 16:01:38 +01:00
|
|
|
services.AddControllers();
|
|
|
|
services.AddSwaggerGen(c =>
|
|
|
|
{
|
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Serwer.Api", Version = "v1" });
|
2020-12-18 16:06:10 +01:00
|
|
|
var jwtSecurityScheme = new OpenApiSecurityScheme
|
|
|
|
{
|
|
|
|
Scheme = "bearer",
|
|
|
|
BearerFormat = "JWT",
|
|
|
|
Name = "JWT Authentication",
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
Type = SecuritySchemeType.Http,
|
|
|
|
Description = "Put **_ONLY_** your JWT Bearer token on textbox below!",
|
|
|
|
|
|
|
|
Reference = new OpenApiReference
|
|
|
|
{
|
|
|
|
Id = JwtBearerDefaults.AuthenticationScheme,
|
|
|
|
Type = ReferenceType.SecurityScheme
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
c.AddSecurityDefinition(jwtSecurityScheme.Reference.Id, jwtSecurityScheme);
|
|
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
{
|
|
|
|
{ jwtSecurityScheme, Array.Empty<string>() }
|
|
|
|
});
|
2020-12-06 16:01:38 +01:00
|
|
|
});
|
|
|
|
|
2020-12-18 16:04:07 +01:00
|
|
|
var hostEnviroment = new HostEnviroment { RootPath = WebRootPath };
|
2020-12-06 18:31:05 +01:00
|
|
|
var jwtSettings = new JwtSettings()
|
|
|
|
{
|
|
|
|
Issuer = "PoszukiwaczInc",
|
|
|
|
ExpiryMinutes = 120,
|
|
|
|
Key = "Fjjji0Hdsa4$JgrwIO1j678dCelgFymdo"
|
|
|
|
};
|
|
|
|
|
|
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
.AddJwtBearer(c =>
|
|
|
|
{
|
|
|
|
c.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
{
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Key)),
|
|
|
|
ValidIssuer = jwtSettings.Issuer,
|
|
|
|
ValidateAudience = false,
|
|
|
|
ValidateLifetime = true
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-12-12 18:11:56 +01:00
|
|
|
services.AddSingleton<IHostEnviroment>(hostEnviroment);
|
2020-12-06 18:31:05 +01:00
|
|
|
services.AddSingleton<IMapper>(AutoMapperConfig.Initialize());
|
|
|
|
services.AddSingleton<IJwtHandler, JwtHandler>(sp => new JwtHandler(jwtSettings));
|
2020-12-06 16:01:38 +01:00
|
|
|
services.AddScoped<IUserRepository, UserRepository>();
|
2020-12-20 18:03:20 +01:00
|
|
|
services.AddScoped<IImageHandler, ImageHandler>();
|
2020-12-06 16:01:38 +01:00
|
|
|
services.AddScoped<IUserService, UserService>();
|
2020-12-20 18:03:20 +01:00
|
|
|
services.AddScoped<ISearchService, SearchService>();
|
2020-12-03 13:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
2020-12-06 16:01:38 +01:00
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
2020-12-03 13:24:47 +01:00
|
|
|
{
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
2020-12-18 16:04:07 +01:00
|
|
|
app.UseCors(builder =>
|
2020-12-12 14:18:31 +01:00
|
|
|
{
|
|
|
|
builder.AllowAnyHeader();
|
|
|
|
builder.AllowAnyMethod();
|
|
|
|
builder.AllowAnyOrigin();
|
|
|
|
});
|
2020-12-03 13:24:47 +01:00
|
|
|
app.UseDeveloperExceptionPage();
|
2020-12-06 16:01:38 +01:00
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Serwer.Api v1"));
|
2020-12-03 13:24:47 +01:00
|
|
|
}
|
2020-12-06 16:01:38 +01:00
|
|
|
app.UseRouting();
|
|
|
|
|
2020-12-16 16:26:13 +01:00
|
|
|
app.UseMiddleware(typeof(ExceptionHandlerMiddleware));
|
2020-12-18 16:04:07 +01:00
|
|
|
|
|
|
|
app.UseAuthentication();
|
2020-12-06 16:01:38 +01:00
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
{
|
|
|
|
endpoints.MapControllers();
|
|
|
|
});
|
2020-12-03 13:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|