2024-09-28 14:58:36 -04:00
using Api.SeaHavenIndustries.Helper ;
using Data.SeaHavenIndustries ;
using Microsoft.AspNetCore.Authentication.JwtBearer ;
using Microsoft.AspNetCore.Identity ;
using Microsoft.AspNetCore.ResponseCompression ;
using Microsoft.EntityFrameworkCore ;
using Microsoft.IdentityModel.Tokens ;
using Microsoft.OpenApi.Models ;
using System.Text ;
var builder = WebApplication . CreateBuilder ( args ) ;
ConfigurationManager configuration = builder . Configuration ;
builder . Services . AddDbContext < ApplicationDbContext > ( options = > options . UseSqlServer ( configuration . GetConnectionString ( "DefaultConnection" ) ) ) ;
builder . Services . AddIdentity < ApplicationUser , IdentityRole > ( options = >
{
options . User . RequireUniqueEmail = false ;
} )
. AddEntityFrameworkStores < ApplicationDbContext > ( )
. AddDefaultTokenProviders ( ) ;
builder . Services . AddControllers ( ) . AddJsonOptions ( options = >
{
options . JsonSerializerOptions . MaxDepth = 264 ; // or any other desired value
} ) ;
builder . Services . AddResponseCompression ( opts = >
{
opts . MimeTypes = ResponseCompressionDefaults . MimeTypes . Concat (
new [ ] { "application/octet-stream" } ) ;
} ) ;
builder . Services . AddCors ( option = >
option . AddDefaultPolicy ( builder = > builder . AllowAnyOrigin ( ) . AllowAnyHeader ( ) . AllowAnyMethod ( ) ) ) ;
builder . Services . AddScoped < SendMessage > ( ) ;
builder . Services . AddAuthentication ( options = >
{
options . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
options . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
options . DefaultScheme = JwtBearerDefaults . AuthenticationScheme ;
} )
. AddJwtBearer ( options = >
{
options . SaveToken = true ;
options . RequireHttpsMetadata = false ;
options . TokenValidationParameters = new TokenValidationParameters ( )
{
ValidateIssuer = true ,
ValidateAudience = true ,
ValidAudience = configuration [ "JWT:ValidAudience" ] ,
ValidIssuer = configuration [ "JWT:ValidIssuer" ] ,
IssuerSigningKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( configuration [ "JWT:Secret" ] ) )
} ;
} ) ;
builder . Services . AddEndpointsApiExplorer ( ) ;
builder . Services . AddSwaggerGen ( c = >
{
c . SwaggerDoc ( "v1" , new OpenApiInfo { Title = "Seahaven" , Version = "v1" } ) ;
c . AddSecurityDefinition ( "Bearer" , new OpenApiSecurityScheme ( )
{
Name = "Authorization" ,
Type = SecuritySchemeType . ApiKey ,
Scheme = "Bearer" ,
BearerFormat = "JWT" ,
In = ParameterLocation . Header ,
Description = "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"" ,
} ) ;
c . AddSecurityRequirement ( new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType . SecurityScheme ,
Id = "Bearer"
}
} ,
new string [ ] { }
}
} ) ;
} ) ;
var app = builder . Build ( ) ;
// Configure the HTTP request pipeline.
if ( app . Environment . IsDevelopment ( ) | | app . Environment . IsProduction ( ) )
{
app . UseSwagger ( ) ;
app . UseSwaggerUI ( c = >
{
c . SwaggerEndpoint ( "/swagger/v1/swagger.json" , "Sari" ) ;
c . RoutePrefix = string . Empty ;
} ) ;
}
2026-04-16 16:25:03 -04:00
if ( ! app . Environment . IsDevelopment ( ) )
app . UseHttpsRedirection ( ) ;
2024-09-28 14:58:36 -04:00
app . UseStaticFiles ( ) ;
app . UseCors ( ) ;
app . UseAuthentication ( ) ;
app . UseAuthorization ( ) ;
app . MapControllers ( ) ;
2026-04-16 16:25:03 -04:00
if ( app . Environment . IsDevelopment ( ) )
{
using var scope = app . Services . CreateScope ( ) ;
var userManager = scope . ServiceProvider . GetRequiredService < UserManager < ApplicationUser > > ( ) ;
var roleManager = scope . ServiceProvider . GetRequiredService < RoleManager < IdentityRole > > ( ) ;
if ( ! await roleManager . RoleExistsAsync ( "Admin" ) )
await roleManager . CreateAsync ( new IdentityRole ( "Admin" ) ) ;
var adminEmail = "admin@seahavenind.com" ;
if ( await userManager . FindByNameAsync ( adminEmail ) = = null )
{
var admin = new ApplicationUser
{
UserName = adminEmail ,
Email = adminEmail ,
FirstName = "Admin" ,
LastName = "User" ,
EmailConfirmed = true ,
UniqueName = "Active" ,
CreatedDate = DateTime . Now
} ;
var result = await userManager . CreateAsync ( admin , "Admin@123" ) ;
if ( result . Succeeded )
await userManager . AddToRoleAsync ( admin , "Admin" ) ;
}
}
2024-09-28 14:58:36 -04:00
app . Run ( ) ;