feat: add sub path for actual url
This commit is contained in:
		
							
								
								
									
										10
									
								
								.env
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								.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) | ||||
|   | ||||
							
								
								
									
										22
									
								
								Endpoint/Common/Services/UrlHelper.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								Endpoint/Common/Services/UrlHelper.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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")); | ||||
| } | ||||
| @@ -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(); | ||||
|     } | ||||
|   | ||||
| @@ -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('/'); | ||||
|             } | ||||
|         }); | ||||
|  | ||||
|   | ||||
| @@ -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); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -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<Admin> 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 | ||||
|         }; | ||||
|   | ||||
| @@ -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<string>("INTERNAL_PORT")!)); | ||||
|             }); | ||||
|         builder.WebHost.ConfigureKestrel(options => | ||||
|         { | ||||
|             options.ListenLocalhost( | ||||
|                 int.Parse(builder.Configuration.GetValue<string>("INTERNAL_PORT") ?? "8080")); | ||||
|         }); | ||||
|  | ||||
|         builder.Services.Configure<ForwardedHeadersOptions>(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(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user