From 0c11b2643cf4eae79b2e7a975be5d425217b78f1 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Tue, 8 Oct 2024 13:32:42 +0200 Subject: [PATCH] [+] Nginx support and Database connection --- Program.cs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/Program.cs b/Program.cs index 944f553..2010ec3 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,9 @@ using System.Text; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; +using Simple_API; +using Simple_API.Database; //Builder configuration var builder = WebApplication.CreateBuilder(args); @@ -8,8 +11,8 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddControllers(); builder.Services.AddControllersWithViews(); +builder.Services.AddControllers(); builder.Services.AddCors(options => { options.AddPolicy("AllowAllOrigins", corsBuilder => @@ -19,7 +22,10 @@ builder.Services.AddCors(options => .AllowAnyHeader(); }); }); - +builder.Services.AddDbContext(options => + options.UseMySql(builder.Configuration.GetConnectionString("DATABASE"), + ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("DATABASE")))); +builder.Services.AddScoped(); // JWT Configuration var jwtSettings = builder.Configuration.GetSection("Jwt"); @@ -58,15 +64,39 @@ var app = builder.Build(); // App configuration 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()) { app.UseSwagger(); app.UseSwaggerUI(); } -app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); +app.UseMiddleware(); app.UseCors("AllowAllOrigins");