2024-05-20 10:46:31 -05:00
using API.Authentication ;
using API.Authentication.Interfaces ;
2024-07-09 18:03:05 -05:00
using API.Hashing ;
using API.Hashing.Interfaces ;
2024-04-22 16:54:28 -05:00
using API.Services ;
2024-07-09 18:03:42 -05:00
using API.Services.Interfaces ;
2024-04-16 18:38:11 -05:00
using DAL.Contexts ;
2024-07-09 18:03:42 -05:00
using DAL.Models ;
2024-04-16 18:38:11 -05:00
using Microsoft.EntityFrameworkCore ;
2024-04-22 18:04:14 -05:00
using Serilog ;
2024-04-16 18:38:11 -05:00
using System.Reflection ;
2024-07-09 18:03:42 -05:00
using ConfigurationManager = Microsoft . Extensions . Configuration . ConfigurationManager ;
2024-04-16 18:38:11 -05:00
using InvalidOperationException = System . InvalidOperationException ;
namespace API
{
internal class Program
{
public static void Main ( string [ ] args )
{
WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
ConfigurationManager configManager = builder . Configuration ;
builder . Services . AddControllers ( ) ;
builder . Services . AddEndpointsApiExplorer ( ) ;
builder . Services . AddSwaggerGen ( options = >
{
string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml" ;
string xmlPath = Path . Combine ( AppContext . BaseDirectory , xmlFile ) ;
options . IncludeXmlComments ( xmlPath , true ) ;
} ) ;
2024-04-22 18:04:14 -05:00
builder . Host . UseSerilog ( ( context , configuration ) = > configuration . ReadFrom . Configuration ( context . Configuration ) ) ;
2024-04-22 16:54:28 -05:00
builder . Services . AddDbContext < SASGContext > ( options = > { options . UseMySQL ( builder . Configuration [ "connectionString" ] ? ? throw new InvalidOperationException ( "Connection String is null" ) ) ; } ) ;
builder . Services . AddTransient < ColorService > ( ) ;
2024-04-22 18:04:14 -05:00
builder . Services . AddTransient < EventService > ( ) ;
builder . Services . AddTransient < GrantService > ( ) ;
builder . Services . AddTransient < ImageService > ( ) ;
builder . Services . AddTransient < PermissionService > ( ) ;
builder . Services . AddTransient < SavedEventService > ( ) ;
builder . Services . AddTransient < UserService > ( ) ;
2024-04-16 18:38:11 -05:00
2024-05-20 10:46:31 -05:00
builder . Services . AddTransient < IYesAuthentication , YesAuthentication > ( ) ;
builder . Services . AddTransient < IColorAuthentication , ColorAuthentication > ( ) ;
2024-07-09 18:03:05 -05:00
builder . Services . AddTransient < IHashingFactory , HashingFactory > ( ) ;
builder . Services . AddTransient < IHashingAlgorithm , Pbkdf2 > ( ) ;
2024-07-09 18:03:42 -05:00
builder . Services . AddTransient < IUserManager , UserManager > ( options = >
{
UserService userService = options . GetRequiredService < UserService > ( ) ;
IHashingFactory hashingFactory = options . GetRequiredService < IHashingFactory > ( ) ;
ILogger < UserManager > logger = options . GetRequiredService < ILogger < UserManager > > ( ) ;
HashingType hashingType ;
if ( ! Enum . TryParse ( builder . Configuration [ "preferredHashingType" ] , out hashingType ) )
throw new InvalidOperationException ( $"preferredHashingType not one of {String.Join(" , ", Enum.GetNames(typeof(HashingType)))}" ) ;
return new UserManager ( userService , hashingFactory , logger , hashingType ) ;
} ) ;
2024-04-16 18:38:11 -05:00
WebApplication app = builder . Build ( ) ;
if ( app . Environment . IsDevelopment ( ) )
{
app . UseSwagger ( ) ;
app . UseSwaggerUI ( ) ;
}
app . UseCookiePolicy ( new CookiePolicyOptions
{
MinimumSameSitePolicy = SameSiteMode . Strict
} ) ;
app . UseHttpsRedirection ( ) ;
app . MapControllers ( ) ;
app . Run ( ) ;
}
}
2024-04-22 16:54:28 -05:00
}