also this is my program.cs
using server.Data;
using server.Services;
using Microsoft.EntityFrameworkCore;
using server.Helpers;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.Limits.MaxRequestBodySize = 524288; // 50 MB
// Other Kestrel settings
});
// Add services to the container.
builder.Services.AddControllers();
// builder.Services.AddEndpointsApiExplorer();
// builder.Services.AddSwaggerGen();
builder.Services.AddScoped();
// Add database context
// builder.Services.AddDbContext(options =>
// options.UseNpgsql(builder.Configuration.GetConnectionString(“DefaultConnection”)));
var connString = “”;
if (builder.Environment.IsDevelopment())
connString = builder.Configuration.GetConnectionString(“DefaultConnection”);
else
{
// Original connection URL from environment variable
var connUrl = Environment.GetEnvironmentVariable(“DATABASE_URL”);
// Check if the connection URL is null or empty
if (string.IsNullOrEmpty(connUrl))
{
Console.WriteLine("Error: Connection URL is missing.");
return;
}
// Remove 'postgres://' prefix
connUrl = connUrl.Replace("postgres://", string.Empty);
Console.WriteLine($"After removing 'postgres://': {connUrl}");
// Split the connection string to extract user info and host details
var pgUserPassHost = connUrl.Split("@");
if (pgUserPassHost.Length != 2)
{
Console.WriteLine("Error: Invalid connection string format.");
return;
}
var pgUserPass = pgUserPassHost[0];
var pgHostPortDb = pgUserPassHost[1];
// Split user info into username and password
var userPassSplit = pgUserPass.Split(":");
if (userPassSplit.Length != 2)
{
Console.WriteLine("Error: Invalid user and password format.");
return;
}
var pgUser = userPassSplit[0];
var pgPass = userPassSplit[1];
// Split host and port from the database name
var hostPortDbSplit = pgHostPortDb.Split("/");
if (hostPortDbSplit.Length != 2)
{
Console.WriteLine("Error: Host and database split error.");
return;
}
var pgHostPort = hostPortDbSplit[0];
var pgDb = hostPortDbSplit[1];
// Split host and port
var hostPortSplit = pgHostPort.Split(":");
if (hostPortSplit.Length != 2)
{
Console.WriteLine("Error: Invalid host and port format.");
return;
}
var pgHost = hostPortSplit[0];
var pgPort = hostPortSplit[1];
// Construct the connection string for Npgsql
connString = $"Server={pgHost};Port={pgPort};User Id={pgUser};Password={pgPass};Database={pgDb};";
Console.WriteLine($"Final connection string: {connString}");
}
builder.Services.AddDbContext(opt =>
{
opt.UseNpgsql(connString);
});
// Add Identity services
builder.Services.AddIdentityServices();
// Add JWT services
builder.Services.AddJwtServices(builder.Configuration);
if (builder.Environment.IsDevelopment())
{
builder.Services.AddCors(options =>
{
options.AddPolicy(“AllowSpecificOrigin”,
builder => builder.WithOrigins(
“http://localhost:8100”,
“capacitor://localhost”,
“ionic://localhost”)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
}
else
{
builder.Services.AddCors(options =>
{
options.AddPolicy(“AllowSpecificOrigin”,
builder => builder.WithOrigins(
“https://bookin.fly.dev”,
“capacitor://localhost”,
“ionic://localhost”)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
}
var app = builder.Build();
app.Lifetime.ApplicationStarted.Register(() =>
{
var addresses = app.Urls;
foreach (var address in addresses)
{
Console.WriteLine($“Kestrel is listening on {address}”);
}
});
// Initialize the database
await DatabaseInitializer.InitializeAsync(app.Services);
app.UseCors(“AllowSpecificOrigin”);
app.UseAuthentication();
app.UseAuthorization();
app.UseDefaultFiles();
app.UseStaticFiles();
app.MapControllers();
app.MapFallbackToController(“Index”, “FallBack”);
app.Run();