diff --git a/.env b/.env index 6a7047e..1e663c4 100644 --- a/.env +++ b/.env @@ -18,6 +18,16 @@ # If you want to change this value, you need to change the values in Settings.json and move the file itself to the desired location. PATH_TO_SAVE= +# The actual sub path to the api +# string +# (optional) +ACTUAL_SUB_PATH= + +# The sub path to the swagger +# string +# (optional) +SWAGGER_SUB_PATH=swagger + # Internal port configuration # integer # (optional) diff --git a/Endpoint/Common/Services/UrlHelper.cs b/Endpoint/Common/Services/UrlHelper.cs new file mode 100644 index 0000000..428ebdd --- /dev/null +++ b/Endpoint/Common/Services/UrlHelper.cs @@ -0,0 +1,22 @@ +using Microsoft.AspNetCore.Http; +using System; +using System.Linq; + +namespace Mirea.Api.Endpoint.Common.Services; + +public static class UrlHelper +{ + public static string CurrentDomain(HttpContext context) => + context.Request.Headers["X-Forwarded-Host"].FirstOrDefault() ?? context.Request.Host.Value; + + private static string CreateSubPath(string? path) + { + if (string.IsNullOrEmpty(path)) + return "/"; + + return "/" + path.Trim('/') + "/"; + } + + public static string GetSubPath => CreateSubPath(Environment.GetEnvironmentVariable("ACTUAL_SUB_PATH")); + public static string GetSubPathSwagger => CreateSubPath(Environment.GetEnvironmentVariable("SWAGGER_SUB_PATH")); +} \ No newline at end of file diff --git a/Endpoint/Configuration/AppConfig/EnvironmentConfiguration.cs b/Endpoint/Configuration/AppConfig/EnvironmentConfiguration.cs index e729071..796ee3f 100644 --- a/Endpoint/Configuration/AppConfig/EnvironmentConfiguration.cs +++ b/Endpoint/Configuration/AppConfig/EnvironmentConfiguration.cs @@ -61,15 +61,18 @@ public static class EnvironmentConfiguration .AddInMemoryCollection(environmentVariables!) .AddInMemoryCollection(variablesFromFile!); + if (variablesFromFile.TryGetValue("PATH_TO_SAVE", out var pathToSave)) + { + Environment.SetEnvironmentVariable("PATH_TO_SAVE", pathToSave); + if (!Directory.Exists(pathToSave)) + Directory.CreateDirectory(pathToSave); + } + if (variablesFromFile.TryGetValue("ACTUAL_SUB_PATH", out var actualSubPath)) + Environment.SetEnvironmentVariable("ACTUAL_SUB_PATH", actualSubPath); - - if (!variablesFromFile.TryGetValue("PATH_TO_SAVE", out var data)) - return result.Build(); - - Environment.SetEnvironmentVariable("PATH_TO_SAVE", data); - if (!Directory.Exists(data)) - Directory.CreateDirectory(data); + if (variablesFromFile.TryGetValue("SWAGGER_SUB_PATH", out var swaggerSubPath)) + Environment.SetEnvironmentVariable("SWAGGER_SUB_PATH", swaggerSubPath); return result.Build(); } diff --git a/Endpoint/Configuration/AppConfig/SwaggerConfiguration.cs b/Endpoint/Configuration/AppConfig/SwaggerConfiguration.cs index 4c2ff9f..7a973ce 100644 --- a/Endpoint/Configuration/AppConfig/SwaggerConfiguration.cs +++ b/Endpoint/Configuration/AppConfig/SwaggerConfiguration.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; +using Mirea.Api.Endpoint.Common.Services; using Mirea.Api.Endpoint.Configuration.Swagger; using Swashbuckle.AspNetCore.SwaggerGen; using System; @@ -65,6 +66,7 @@ public static class SwaggerConfiguration var url = $"/swagger/{description.GroupName}/swagger.json"; var name = description.GroupName.ToUpperInvariant(); options.SwaggerEndpoint(url, name); + options.RoutePrefix = UrlHelper.GetSubPathSwagger.Trim('/'); } }); diff --git a/Endpoint/Controllers/Configuration/SetupController.cs b/Endpoint/Controllers/Configuration/SetupController.cs index 46a4d26..827257f 100644 --- a/Endpoint/Controllers/Configuration/SetupController.cs +++ b/Endpoint/Controllers/Configuration/SetupController.cs @@ -69,11 +69,11 @@ public partial class SetupController( Response.Cookies.Append("AuthToken", token, new CookieOptions { - HttpOnly = false, - Secure = false, - Path = "/" + Path = UrlHelper.GetSubPath + "api", + Domain = UrlHelper.CurrentDomain(ControllerContext.HttpContext), + Secure = true, + HttpOnly = true }); - return Ok(true); } diff --git a/Endpoint/Controllers/V1/AuthController.cs b/Endpoint/Controllers/V1/AuthController.cs index 0f70204..1ecb0fe 100644 --- a/Endpoint/Controllers/V1/AuthController.cs +++ b/Endpoint/Controllers/V1/AuthController.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; using Mirea.Api.Dto.Common; using Mirea.Api.Dto.Requests; using Mirea.Api.Dto.Responses; +using Mirea.Api.Endpoint.Common.Services; using Mirea.Api.Endpoint.Common.Settings; using Mirea.Api.Security.Common.Dto.Requests; using Mirea.Api.Security.Services; @@ -28,8 +29,8 @@ public class AuthController(IOptionsSnapshot user, AuthService auth, Pass var cookieOptions = new CookieOptions { Expires = expires, - Path = "/api", - Domain = Request.Headers["X-Forwarded-Host"], + Path = UrlHelper.GetSubPath + "api", + Domain = UrlHelper.CurrentDomain(ControllerContext.HttpContext), Secure = true, HttpOnly = true }; diff --git a/Endpoint/Program.cs b/Endpoint/Program.cs index b336aa3..4a2e108 100644 --- a/Endpoint/Program.cs +++ b/Endpoint/Program.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -66,11 +67,11 @@ public class Program }); }); - builder.WebHost.ConfigureKestrel(options => - { - options.ListenLocalhost( - int.Parse(builder.Configuration.GetValue("INTERNAL_PORT")!)); - }); + builder.WebHost.ConfigureKestrel(options => + { + options.ListenLocalhost( + int.Parse(builder.Configuration.GetValue("INTERNAL_PORT") ?? "8080")); + }); builder.Services.Configure(options => { @@ -94,7 +95,7 @@ public class Program var app = builder.Build(); - app.UseStaticFiles(); + app.UseStaticFiles(UrlHelper.GetSubPath.TrimEnd('/')); app.UseCors("AllowAll"); app.UseCustomSerilog(); app.UseForwardedHeaders();