Files
LaDOSE/LaDOSE.Src/LaDOSE.Api/Startup.cs

194 lines
8.3 KiB
C#
Raw Normal View History

2018-10-04 21:13:32 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
2019-03-09 18:44:43 +01:00
using System.Reflection;
2018-10-05 01:51:23 +02:00
using System.Text;
2018-10-04 21:13:32 +02:00
using System.Threading.Tasks;
2018-10-06 13:05:38 +02:00
using LaDOSE.Business.Interface;
using LaDOSE.Business.Provider;
2018-10-06 13:05:38 +02:00
using LaDOSE.Business.Service;
2018-10-05 01:51:23 +02:00
using LaDOSE.Entity;
2018-10-06 13:05:38 +02:00
using LaDOSE.Entity.Context;
2018-10-05 01:51:23 +02:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
2018-10-04 21:13:32 +02:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
//using Microsoft.AspNetCore.HttpsPolicy;
2018-10-05 01:51:23 +02:00
using Microsoft.AspNetCore.Identity;
2018-10-04 21:13:32 +02:00
using Microsoft.AspNetCore.Mvc;
2018-10-05 00:18:37 +02:00
using Microsoft.EntityFrameworkCore;
2018-10-04 21:13:32 +02:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
2018-10-05 01:51:23 +02:00
using Microsoft.IdentityModel.Tokens;
2018-10-06 13:05:38 +02:00
using Pomelo.EntityFrameworkCore.MySql;
2018-10-05 00:18:37 +02:00
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
2019-03-12 22:23:38 +01:00
using AutoMapper;
using LaDOSE.Api.Helpers;
using LaDOSE.Business.Helper;
using LaDOSE.Business.Provider.ChallongProvider;
using LaDOSE.Business.Provider.SmashProvider;
2019-05-29 02:15:31 +02:00
using LaDOSE.Entity.Challonge;
2019-03-12 22:23:38 +01:00
using LaDOSE.Entity.Wordpress;
2022-03-19 22:54:55 +01:00
using Result = LaDOSE.Entity.Challonge.Result;
2018-10-04 21:13:32 +02:00
namespace LaDOSE.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)
{
2019-03-09 18:44:43 +01:00
//Fix Gentoo Issue.
2018-10-06 02:55:29 +02:00
var MySqlServer = this.Configuration["MySql:Server"];
var MySqlDatabase = this.Configuration["MySql:Database"];
var MySqlUser = this.Configuration["MySql:User"];
var MySqlPassword = this.Configuration["MySql:Password"];
2019-03-09 18:44:43 +01:00
if (Convert.ToBoolean(this.Configuration["FixGentoo"]))
{
try
{
var loadFrom = Assembly.LoadFrom("ChallongeCSharpDriver.dll");
Console.WriteLine($"Fix Gentoo Ok : {loadFrom.FullName}");
}
catch(Exception exception)
{
Console.WriteLine($"Fix Gentoo NOK : {exception.Message}");
}
}
2019-03-12 22:23:38 +01:00
2018-10-05 01:51:23 +02:00
services.AddCors();
2020-09-13 22:50:52 +02:00
services.AddMvc().AddNewtonsoftJson(x =>
2019-03-09 14:03:25 +01:00
{
x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
x.SerializerSettings.MaxDepth= 4;
});
2018-10-05 00:18:37 +02:00
services.AddDbContextPool<LaDOSEDbContext>( // replace "YourDbContext" with the class name of your DbContext
2018-10-06 03:08:39 +02:00
options => options.UseMySql($"Server={MySqlServer};Database={MySqlDatabase};User={MySqlUser};Password={MySqlPassword};", // replace with your Connection String
2018-10-05 00:18:37 +02:00
mysqlOptions =>
{
2018-10-06 13:05:38 +02:00
2018-10-05 00:18:37 +02:00
mysqlOptions.ServerVersion(new Version(10, 1, 16), ServerType.MariaDb); // replace with your Server Version and Type
}
));
2018-10-05 01:51:23 +02:00
var key = Encoding.ASCII.GetBytes(this.Configuration["JWTTokenSecret"]);
2018-10-05 01:51:23 +02:00
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.Events = new JwtBearerEvents
{
OnTokenValidated = context =>
{
var userService = context.HttpContext.RequestServices.GetRequiredService<IUserService>();
var userId = int.Parse(context.Principal.Identity.Name);
var user = userService.GetById(userId);
if (user == null)
{
// return unauthorized if user no longer exists
context.Fail("Unauthorized");
}
2018-10-05 01:51:23 +02:00
return Task.CompletedTask;
}
};
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
2019-03-12 22:23:38 +01:00
2018-10-05 01:51:23 +02:00
// configure DI for application services
AddDIConfig(services);
2020-09-13 22:50:52 +02:00
var mapperConfig = new MapperConfiguration(cfg =>
2019-03-12 22:23:38 +01:00
{
cfg.CreateMap<WPUser, LaDOSE.DTO.WPUserDTO>();
cfg.CreateMap<WPUser, LaDOSE.DTO.WPUserDTO>();
cfg.CreateMap<WPEvent, LaDOSE.DTO.WPEventDTO>();
cfg.CreateMap<Result, LaDOSE.DTO.ResultDTO>();
cfg.CreateMap<TournamentsResult, LaDOSE.DTO.TournamentsResultDTO>();
cfg.CreateMap<ChallongeParticipent, LaDOSE.DTO.ParticipentDTO>();
cfg.CreateMap<ChallongeTournament, LaDOSE.DTO.TournamentDTO>();
cfg.CreateMap<ApplicationUser, LaDOSE.DTO.ApplicationUserDTO>();
cfg.CreateMap<WPBooking, LaDOSE.DTO.WPBookingDTO>().ForMember(e=>e.Meta,opt=>opt.MapFrom(s=>s.Meta.CleanWpMeta()));
2019-03-12 22:23:38 +01:00
cfg.CreateMapTwoWay<Game, LaDOSE.DTO.GameDTO>();
cfg.CreateMapTwoWay<Todo, LaDOSE.DTO.TodoDTO>();
2019-03-12 22:23:38 +01:00
});
2020-09-13 22:50:52 +02:00
IMapper mapper = mapperConfig.CreateMapper();
services.AddSingleton(mapper);
}
private void AddDIConfig(IServiceCollection services)
{
2019-03-09 18:44:43 +01:00
2018-10-05 01:51:23 +02:00
services.AddScoped<IUserService, UserService>();
2018-10-06 14:16:42 +02:00
services.AddScoped<IGameService, GameService>();
services.AddScoped<IEventService, EventService>();
services.AddScoped<IWordPressService, WordPressService>();
services.AddScoped<ITodoService, TodoService>();
services.AddScoped<IEventService, EventService>();
2022-03-20 19:36:15 +01:00
services.AddScoped<IPlayerService, PlayerService>();
2022-03-20 19:36:15 +01:00
services.AddTransient<IChallongeProvider>(p => new ChallongeProvider( p.GetRequiredService<IGameService>(),
p.GetRequiredService<IEventService>(),
p.GetRequiredService<IPlayerService>(),
this.Configuration["ApiKey:ChallongeApiKey"]));
services.AddTransient<ISmashProvider>(p => new SmashProvider( p.GetRequiredService<IGameService>(),
p.GetRequiredService<IEventService>(),
p.GetRequiredService<IPlayerService>(),
this.Configuration["ApiKey:SmashApiKey"]));
services.AddScoped<IExternalProviderService, ExternalProviderService>();
2018-10-04 21:13:32 +02:00
}
2018-10-04 21:13:32 +02:00
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2018-10-05 01:51:23 +02:00
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
2018-10-04 21:13:32 +02:00
{
2020-09-13 22:50:52 +02:00
//loggerFactory.AddConsole(Configuration.GetSection("Logging"));
//loggerFactory.AddDebug();
2018-10-05 01:51:23 +02:00
2018-10-04 21:13:32 +02:00
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
2018-10-05 01:51:23 +02:00
app.UseCors(x => x
2020-09-13 22:50:52 +02:00
//.AllowAnyOrigin()
2018-10-05 01:51:23 +02:00
.AllowAnyMethod()
.AllowAnyHeader()
2020-09-13 22:50:52 +02:00
.SetIsOriginAllowed(origin => true)
2018-10-05 01:51:23 +02:00
.AllowCredentials());
2018-10-04 21:13:32 +02:00
//app.UseHttpsRedirection();
2020-09-13 22:50:52 +02:00
app.UseRouting();
2018-10-05 01:51:23 +02:00
app.UseAuthentication();
2020-09-13 22:50:52 +02:00
app.UseAuthorization();
app.UseEndpoints(x => x.MapControllers());
2018-10-04 21:13:32 +02:00
}
}
}