Poszukiwacz/Serwer/Serwer.Api/Startup.cs
2020-12-12 14:18:31 +01:00

99 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Serwer.Core.Repositories;
using Serwer.Infrastructure.Mappers;
using Serwer.Infrastructure.Repositories;
using Serwer.Infrastructure.Services;
using Serwer.Infrastructure.Settings;
namespace Serwer.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Serwer.Api", Version = "v1" });
});
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
};
});
services.AddSingleton<IMapper>(AutoMapperConfig.Initialize());
services.AddSingleton<IJwtHandler, JwtHandler>(sp => new JwtHandler(jwtSettings));
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<IImageService, ImageService>();
services.AddScoped<IUserService, UserService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseCors(builder =>
{
builder.AllowAnyHeader();
builder.AllowAnyMethod();
builder.AllowAnyOrigin();
});
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Serwer.Api v1"));
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}