128 lines
4.8 KiB
C#
128 lines
4.8 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.Api.Framework;
|
|
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, IWebHostEnvironment env)
|
|
{
|
|
Configuration = configuration;
|
|
WebRootPath = env.WebRootPath;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
protected string WebRootPath { get; set; }
|
|
|
|
// 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 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>() }
|
|
});
|
|
});
|
|
|
|
var hostEnviroment = new HostEnviroment { RootPath = WebRootPath };
|
|
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);
|
|
services.AddSingleton<IMapper>(AutoMapperConfig.Initialize());
|
|
services.AddSingleton<IJwtHandler, JwtHandler>(sp => new JwtHandler(jwtSettings));
|
|
services.AddScoped<IUserRepository, UserRepository>();
|
|
services.AddScoped<IHistoryRepository, HistoryRepository>();
|
|
services.AddScoped<IImageHandler, ImageHandler>();
|
|
services.AddScoped<IUserService, UserService>();
|
|
services.AddScoped<IHistoryService, HistoryService>();
|
|
services.AddScoped<ISearchService, SearchService>();
|
|
}
|
|
|
|
// 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.UseMiddleware(typeof(ExceptionHandlerMiddleware));
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
}
|
|
}
|
|
}
|