210 lines
6.4 KiB
C#
210 lines
6.4 KiB
C#
using System.Text;
|
|
using Marco.Pms.DataAccess.Data;
|
|
using Marco.Pms.Model.Authentication;
|
|
using Marco.Pms.Model.Entitlements;
|
|
using Marco.Pms.Model.Utilities;
|
|
using Marco.Pms.Services.Service;
|
|
using MarcoBMS.Services.Helpers;
|
|
using MarcoBMS.Services.Middleware;
|
|
using MarcoBMS.Services.Service;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using Serilog;
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add Serilog Configuration
|
|
string? mongoConn = builder.Configuration["MongoDB:SerilogDatabaseUrl"];
|
|
string timeString = "00:00:30";
|
|
TimeSpan.TryParse(timeString, out TimeSpan timeSpan);
|
|
|
|
// Add Serilog Configuration
|
|
builder.Host.UseSerilog((context, config) =>
|
|
{
|
|
config.ReadFrom.Configuration(context.Configuration) // Taking all configuration from appsetting.json
|
|
.WriteTo.MongoDB(
|
|
databaseUrl: mongoConn ?? string.Empty,
|
|
collectionName: "api-logs",
|
|
batchPostingLimit: 100,
|
|
period: timeSpan
|
|
);
|
|
|
|
});
|
|
|
|
// Add services
|
|
var corsSettings = builder.Configuration.GetSection("Cors");
|
|
var allowedOrigins = corsSettings.GetValue<string>("AllowedOrigins")?.Split(',');
|
|
var allowedMethods = corsSettings.GetValue<string>("AllowedMethods")?.Split(',');
|
|
var allowedHeaders = corsSettings.GetValue<string>("AllowedHeaders")?.Split(',');
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("Policy", policy =>
|
|
{
|
|
if (allowedOrigins != null && allowedMethods != null && allowedHeaders != null)
|
|
{
|
|
policy.WithOrigins(allowedOrigins)
|
|
.WithMethods(allowedMethods)
|
|
.WithHeaders(allowedHeaders);
|
|
}
|
|
});
|
|
}).AddCors(options =>
|
|
{
|
|
options.AddPolicy("DevCorsPolicy", policy =>
|
|
{
|
|
policy.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader();
|
|
});
|
|
});
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddHostedService<StartupUserSeeder>();
|
|
builder.Services.AddControllers();
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
builder.Services.AddSwaggerGen(option =>
|
|
{
|
|
option.SwaggerDoc("v1", new OpenApiInfo { Title = "Demo API", Version = "v1" });
|
|
option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
In = ParameterLocation.Header,
|
|
Description = "Please enter a valid token",
|
|
Name = "Authorization",
|
|
Type = SecuritySchemeType.Http,
|
|
BearerFormat = "JWT",
|
|
Scheme = "Bearer"
|
|
});
|
|
|
|
option.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type=ReferenceType.SecurityScheme,
|
|
Id="Bearer"
|
|
}
|
|
},
|
|
new string[]{}
|
|
}
|
|
});
|
|
});
|
|
|
|
builder.Services.Configure<SmtpSettings>(builder.Configuration.GetSection("EmailSettings"));
|
|
builder.Services.AddTransient<IEmailSender, EmailSender>();
|
|
|
|
builder.Services.Configure<AWSSettings>(builder.Configuration.GetSection("AWS")); // For uploading images to aws s3
|
|
builder.Services.AddTransient<S3UploadService>();
|
|
|
|
builder.Services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
|
|
|
|
|
|
string? connString = builder.Configuration.GetConnectionString("DefaultConnectionString");
|
|
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
{
|
|
options.UseMySql(connString, ServerVersion.AutoDetect(connString));
|
|
});
|
|
|
|
|
|
builder.Services.AddMemoryCache();
|
|
|
|
|
|
//builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
|
|
//builder.Services.AddScoped<IProjectRepository, ProjectRepository>();
|
|
//builder.Services.AddScoped<IEmployeeRepository, EmployeeRepository>();
|
|
//builder.Services.AddScoped<IActivityMasterRepository, ActivityMasterRepository>();
|
|
//builder.Services.AddScoped<IAttendenceRepository, AttendenceRepository>();
|
|
//builder.Services.AddScoped<IProjectAllocationRepository, ProjectAllocationRepository>();
|
|
|
|
builder.Services.AddScoped<RefreshTokenService>();
|
|
|
|
builder.Services.AddScoped<UserHelper>();
|
|
builder.Services.AddScoped<RolesHelper>();
|
|
builder.Services.AddScoped<EmployeeHelper>();
|
|
builder.Services.AddScoped<ProjectsHelper>();
|
|
builder.Services.AddSingleton<ILoggingService, LoggingService>();
|
|
|
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
var jwtSettings = builder.Configuration.GetSection("Jwt").Get<JwtSettings>()
|
|
?? throw new InvalidOperationException("JwtSettings section is missing or invalid.");
|
|
|
|
if (jwtSettings != null && jwtSettings.Key != null)
|
|
{
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = jwtSettings.Issuer,
|
|
ValidAudience = jwtSettings.Audience,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Key))
|
|
};
|
|
});
|
|
builder.Services.AddSingleton(jwtSettings);
|
|
}
|
|
|
|
builder.WebHost.ConfigureKestrel(options =>
|
|
{
|
|
options.AddServerHeader = false; // Disable the "Server" header
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
|
app.UseMiddleware<TenantMiddleware>();
|
|
app.UseMiddleware<LoggingMiddleware>();
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
// Use CORS in the pipeline
|
|
app.UseCors("DevCorsPolicy");
|
|
}
|
|
else
|
|
{
|
|
//if (app.Environment.IsProduction())
|
|
//{
|
|
// app.UseCors("ProdCorsPolicy");
|
|
//}
|
|
|
|
//app.UseCors("AllowAll");
|
|
app.UseCors("DevCorsPolicy");
|
|
}
|
|
|
|
app.UseStaticFiles(); // Enables serving static files
|
|
|
|
//app.UseSerilogRequestLogging(); // This is Default Serilog Logging Middleware we are not using this because we're using custom logging middleware
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|