Poszukiwacz/Serwer/Serwer.Api/Startup.cs

101 lines
3.6 KiB
C#
Raw Normal View History

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;
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
{
public Startup(IConfiguration configuration, IWebHostEnvironment env)
2020-12-03 13:24:47 +01:00
{
Configuration = configuration;
WebRootPath = env.WebRootPath;
2020-12-03 13:24:47 +01:00
}
public IConfiguration Configuration { get; }
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" });
});
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
};
});
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-12 14:18:31 +01:00
services.AddScoped<IImageService, ImageService>();
2020-12-06 16:01:38 +01:00
services.AddScoped<IUserService, UserService>();
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-12 14:18:31 +01:00
app.UseCors(builder =>
{
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-06 18:31:05 +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
}
}
}