139 lines
4.5 KiB
C#
Executable File
139 lines
4.5 KiB
C#
Executable File
using System.Reflection;
|
|
using System.Text;
|
|
using GamificationService.Database;
|
|
using GamificationService.Database.Repositories;
|
|
using GamificationService.Loggging;
|
|
using GamificationService.Mapper;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using Serilog;
|
|
|
|
namespace GamificationService.Extensions;
|
|
|
|
public static class MappingExtensions
|
|
{
|
|
public static IServiceCollection AddMapping(this IServiceCollection services)
|
|
{
|
|
services.AddAutoMapper(typeof(MappingProfile));
|
|
return services;
|
|
}
|
|
}
|
|
|
|
public static class CachingExtensions
|
|
{
|
|
public static IServiceCollection AddRedisCaching(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.AddStackExchangeRedisCache(options =>
|
|
{
|
|
options.Configuration = configuration["Redis:ConnectionString"] ?? "localhost:6379";
|
|
options.InstanceName = configuration["Redis:InstanceName"] ?? "default";
|
|
});
|
|
return services;
|
|
}
|
|
}
|
|
|
|
public static class LoggingExtensions
|
|
{
|
|
public static IHostBuilder UseCustomSerilog(this IHostBuilder hostBuilder)
|
|
{
|
|
LoggingConfigurator.ConfigureLogging();
|
|
return hostBuilder.UseSerilog();
|
|
}
|
|
}
|
|
|
|
public static class DatabaseExtensions
|
|
{
|
|
public static IServiceCollection AddDatabase(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.AddDbContext<ApplicationContext>(x =>
|
|
{
|
|
var dbSettings = configuration.GetSection("DatabaseSettings");
|
|
var hostname = dbSettings["Hostname"] ?? "localhost";
|
|
var port = dbSettings["Port"] ?? "5432";
|
|
var name = dbSettings["Name"] ?? "postgres";
|
|
var username = dbSettings["Username"] ?? "postgres";
|
|
var password = dbSettings["Password"] ?? "postgres";
|
|
x.UseNpgsql($"Server={hostname}:{port};Database={name};Uid={username};Pwd={password};");
|
|
});
|
|
services.AddScoped<UnitOfWork>(sp => new UnitOfWork(sp.GetRequiredService<ApplicationContext>()));
|
|
services.AddScoped(typeof(GenericRepository<>));
|
|
return services;
|
|
}
|
|
}
|
|
|
|
public static class SwaggerExtensions
|
|
{
|
|
public static IServiceCollection AddSwagger(this IServiceCollection services)
|
|
{
|
|
string projectName = Assembly.GetExecutingAssembly().GetName().Name!;
|
|
services.AddOpenApi();
|
|
services.AddEndpointsApiExplorer();
|
|
services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = projectName, Version = "v1" });
|
|
|
|
// Set the comments path for the Swagger JSON and UI
|
|
var xmlFile = $"{projectName}.xml";
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
c.IncludeXmlComments(xmlPath);
|
|
});
|
|
return services;
|
|
}
|
|
}
|
|
|
|
public static class JwtAuthExtensions
|
|
{
|
|
public static IServiceCollection AddJwtAuth(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var jwtSettings = configuration.GetSection("JwtSettings");
|
|
var secretKey = jwtSettings["SecretKey"] ?? throw new SystemException("JwtSettings:SecretKey not found");
|
|
var key = Encoding.ASCII.GetBytes(secretKey);
|
|
|
|
services.AddAuthentication(x =>
|
|
{
|
|
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(x =>
|
|
{
|
|
x.RequireHttpsMetadata = false;
|
|
x.SaveToken = true;
|
|
x.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(key),
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false
|
|
};
|
|
});
|
|
|
|
return services;
|
|
}
|
|
}
|
|
|
|
public static class BackendServicesExtensions
|
|
{
|
|
public static IServiceCollection AddBackendServices(this IServiceCollection services)
|
|
{
|
|
return services;
|
|
}
|
|
}
|
|
|
|
public static class UtilServicesExtensions
|
|
{
|
|
public static IServiceCollection AddUtilServices(this IServiceCollection services)
|
|
{
|
|
return services;
|
|
}
|
|
}
|
|
|
|
public static class FactoryExtensions
|
|
{
|
|
public static IServiceCollection AddFactories(this IServiceCollection services)
|
|
{
|
|
return services;
|
|
}
|
|
}
|