[+] Nginx support and Database connection

This commit is contained in:
Charles Le Maux 2024-10-08 13:32:42 +02:00
parent 3859a84c02
commit 0c11b2643c

View File

@ -1,6 +1,9 @@
using System.Text; using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Simple_API;
using Simple_API.Database;
//Builder configuration //Builder configuration
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -8,8 +11,8 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddControllers();
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
builder.Services.AddControllers();
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
options.AddPolicy("AllowAllOrigins", corsBuilder => options.AddPolicy("AllowAllOrigins", corsBuilder =>
@ -19,7 +22,10 @@ builder.Services.AddCors(options =>
.AllowAnyHeader(); .AllowAnyHeader();
}); });
}); });
builder.Services.AddDbContext<Database>(options =>
options.UseMySql(builder.Configuration.GetConnectionString("DATABASE"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("DATABASE"))));
builder.Services.AddScoped<UserService>();
// JWT Configuration // JWT Configuration
var jwtSettings = builder.Configuration.GetSection("Jwt"); var jwtSettings = builder.Configuration.GetSection("Jwt");
@ -58,15 +64,39 @@ var app = builder.Build();
// App configuration // App configuration
app.MapControllers(); app.MapControllers();
/* TODO : Uncomment if you're using nginx for https redirections.
Also ensure that your configuration looks like this :
server {
listen 443 ssl;
server_name your_domain.com;
ssl_certificate /path/to/localhost.pem;
ssl_certificate_key /path/to/localhost-key.pem;
location / {
proxy_pass http://localhost:your_backend_port; # Change this to your backend server
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
app.UseForwardedHeaders(new ForwardedHeadersOptions
{ ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });
*/
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI();
} }
app.UseHttpsRedirection();
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
app.UseMiddleware<Middlewares.ClaimsValidationMiddleware>();
app.UseCors("AllowAllOrigins"); app.UseCors("AllowAllOrigins");