-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
113 lines (91 loc) · 3.37 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using dotenv.net;
using Microsoft.EntityFrameworkCore;
using DotnetNBA.Data;
using System.Text.RegularExpressions;
using Microsoft.OpenApi.Models;
using System.Security.Cryptography.X509Certificates;
var builder = WebApplication.CreateBuilder(args);
// Load environment variables
DotEnv.Load();
// Read the configuration and replace placeholders
var configuration = builder.Configuration.GetSection("ConnectionStrings:DefaultConnection").Value;
configuration = Regex.Replace(configuration, @"\$\{(.*?)\}", match =>
{
var envVar = match.Groups[1].Value;
return Environment.GetEnvironmentVariable(envVar) ?? match.Value;
});
// Override the connection string in the configuration
builder.Configuration["ConnectionStrings:DefaultConnection"] = configuration;
builder.Services.AddControllers();
// Add Swagger services
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "MyAPI", Version = "v1" });
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
// HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseCors("AllowAllOrigins");
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyAPI v1");
c.RoutePrefix = string.Empty; // Set Swagger UI at the app's root
});
}
// Enable CORS
app.UseCors("AllowAllOrigins");
// Use HTTPS Redirection and configure Kestrel with a certificate only in Production
if (app.Environment.IsProduction())
{
var pfxFilePath = Environment.GetEnvironmentVariable("PFX_FILE_PATH");
var pfxPassword = Environment.GetEnvironmentVariable("PFX_PASSWORD");
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyAPI v1");
c.RoutePrefix = string.Empty; // Set Swagger UI at the app's root
});
if (!string.IsNullOrEmpty(pfxFilePath) && !string.IsNullOrEmpty(pfxPassword))
{
var certificate = new X509Certificate2(pfxFilePath, pfxPassword);
app.UseHttpsRedirection();
app.Urls.Add("https://*:7012");
app.Urls.Add("http://*:5214");
}
else
{
app.Logger.LogWarning("HTTPS is configured in production but no certificate was provided.");
}
}
else
{
// If not in production, configure HTTP only
app.Urls.Add("http://*:5214");
}
app.UseAuthorization();
app.MapControllers();
app.Run();