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 ;
2018-10-07 15:15:11 +02:00
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 ;
2018-10-06 02:05:00 +02:00
//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 ;
2019-03-12 22:40:58 +01:00
using LaDOSE.Business.Helper ;
2019-05-29 02:15:31 +02:00
using LaDOSE.Entity.Challonge ;
2019-03-12 22:23:38 +01:00
using LaDOSE.Entity.Wordpress ;
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 ( ) ;
2019-03-09 14:03:25 +01:00
services . AddMvc ( ) . AddJsonOptions ( x = >
{
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
2018-10-06 02:05:00 +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" ) ;
}
2019-03-27 00:37:11 +01:00
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
2018-10-07 15:15:11 +02:00
AddDIConfig ( services ) ;
2019-03-12 22:23:38 +01:00
Mapper . Initialize ( cfg = >
{
cfg . CreateMap < WPUser , LaDOSE . DTO . WPUserDTO > ( ) ;
cfg . CreateMap < WPUser , LaDOSE . DTO . WPUserDTO > ( ) ;
cfg . CreateMap < WPEvent , LaDOSE . DTO . WPEventDTO > ( ) ;
2019-05-30 00:42:12 +02:00
cfg . CreateMap < Result , LaDOSE . DTO . ResultDTO > ( ) ;
cfg . CreateMap < TournamentsResult , LaDOSE . DTO . TournamentsResultDTO > ( ) ;
2019-08-09 21:22:07 +02:00
cfg . CreateMap < ChallongeParticipent , LaDOSE . DTO . ParticipentDTO > ( ) ;
cfg . CreateMap < ChallongeTournament , LaDOSE . DTO . TournamentDTO > ( ) ;
2019-05-30 00:42:12 +02:00
2019-03-27 00:37:11 +01:00
cfg . CreateMap < ApplicationUser , LaDOSE . DTO . ApplicationUserDTO > ( ) ;
2019-03-12 22:40:58 +01:00
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 > ( ) ;
2019-03-27 00:37:11 +01:00
cfg . CreateMapTwoWay < Todo , LaDOSE . DTO . TodoDTO > ( ) ;
2019-03-12 22:23:38 +01:00
} ) ;
2018-10-07 15:15:11 +02:00
}
private void AddDIConfig ( IServiceCollection services )
{
2019-03-09 18:44:43 +01:00
services . AddTransient < IChallongeProvider > ( p = > new ChallongeProvider ( this . Configuration [ "ApiKey:ChallongeApiKey" ] ) ) ;
2018-10-05 01:51:23 +02:00
services . AddScoped < IUserService , UserService > ( ) ;
2018-10-06 14:16:42 +02:00
services . AddScoped < IGameService , GameService > ( ) ;
2018-10-07 15:15:11 +02:00
services . AddScoped < IEventService , EventService > ( ) ;
2018-10-12 20:52:59 +02:00
services . AddScoped < ISeasonService , SeasonService > ( ) ;
2019-02-26 01:26:03 +01:00
services . AddScoped < IWordPressService , WordPressService > ( ) ;
2019-03-27 00:37:11 +01:00
services . AddScoped < ITodoService , TodoService > ( ) ;
2019-05-30 00:42:12 +02:00
services . AddScoped < ITournamentService , TournamentService > ( ) ;
2019-03-09 18:44:43 +01:00
2018-10-04 21:13:32 +02:00
}
2018-10-07 15:15:11 +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
{
2018-10-05 01:51:23 +02:00
loggerFactory . AddConsole ( Configuration . GetSection ( "Logging" ) ) ;
loggerFactory . AddDebug ( ) ;
2018-10-04 21:13:32 +02:00
if ( env . IsDevelopment ( ) )
{
app . UseDeveloperExceptionPage ( ) ;
}
2018-10-06 02:05:00 +02:00
2018-10-05 01:51:23 +02:00
app . UseCors ( x = > x
. AllowAnyOrigin ( )
. AllowAnyMethod ( )
. AllowAnyHeader ( )
. AllowCredentials ( ) ) ;
2018-10-04 21:13:32 +02:00
2018-10-06 02:05:00 +02:00
//app.UseHttpsRedirection();
2018-10-05 01:51:23 +02:00
app . UseAuthentication ( ) ;
2018-10-04 21:13:32 +02:00
app . UseMvc ( ) ;
}
}
}