feat: add sub path for actual url

This commit is contained in:
Polianin Nikita 2024-07-05 01:35:19 +03:00
parent 57f4d1b822
commit f42caa3a45
7 changed files with 58 additions and 19 deletions

10
.env
View File

@ -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. # 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= 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 # Internal port configuration
# integer # integer
# (optional) # (optional)

View 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"));
}

View File

@ -61,15 +61,18 @@ public static class EnvironmentConfiguration
.AddInMemoryCollection(environmentVariables!) .AddInMemoryCollection(environmentVariables!)
.AddInMemoryCollection(variablesFromFile!); .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("SWAGGER_SUB_PATH", out var swaggerSubPath))
if (!variablesFromFile.TryGetValue("PATH_TO_SAVE", out var data)) Environment.SetEnvironmentVariable("SWAGGER_SUB_PATH", swaggerSubPath);
return result.Build();
Environment.SetEnvironmentVariable("PATH_TO_SAVE", data);
if (!Directory.Exists(data))
Directory.CreateDirectory(data);
return result.Build(); return result.Build();
} }

View File

@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Mirea.Api.Endpoint.Common.Services;
using Mirea.Api.Endpoint.Configuration.Swagger; using Mirea.Api.Endpoint.Configuration.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
using System; using System;
@ -65,6 +66,7 @@ public static class SwaggerConfiguration
var url = $"/swagger/{description.GroupName}/swagger.json"; var url = $"/swagger/{description.GroupName}/swagger.json";
var name = description.GroupName.ToUpperInvariant(); var name = description.GroupName.ToUpperInvariant();
options.SwaggerEndpoint(url, name); options.SwaggerEndpoint(url, name);
options.RoutePrefix = UrlHelper.GetSubPathSwagger.Trim('/');
} }
}); });

View File

@ -69,11 +69,11 @@ public partial class SetupController(
Response.Cookies.Append("AuthToken", token, new CookieOptions Response.Cookies.Append("AuthToken", token, new CookieOptions
{ {
HttpOnly = false, Path = UrlHelper.GetSubPath + "api",
Secure = false, Domain = UrlHelper.CurrentDomain(ControllerContext.HttpContext),
Path = "/" Secure = true,
HttpOnly = true
}); });
return Ok(true); return Ok(true);
} }

View File

@ -6,6 +6,7 @@ using Microsoft.Extensions.Options;
using Mirea.Api.Dto.Common; using Mirea.Api.Dto.Common;
using Mirea.Api.Dto.Requests; using Mirea.Api.Dto.Requests;
using Mirea.Api.Dto.Responses; using Mirea.Api.Dto.Responses;
using Mirea.Api.Endpoint.Common.Services;
using Mirea.Api.Endpoint.Common.Settings; using Mirea.Api.Endpoint.Common.Settings;
using Mirea.Api.Security.Common.Dto.Requests; using Mirea.Api.Security.Common.Dto.Requests;
using Mirea.Api.Security.Services; using Mirea.Api.Security.Services;
@ -28,8 +29,8 @@ public class AuthController(IOptionsSnapshot<Admin> user, AuthService auth, Pass
var cookieOptions = new CookieOptions var cookieOptions = new CookieOptions
{ {
Expires = expires, Expires = expires,
Path = "/api", Path = UrlHelper.GetSubPath + "api",
Domain = Request.Headers["X-Forwarded-Host"], Domain = UrlHelper.CurrentDomain(ControllerContext.HttpContext),
Secure = true, Secure = true,
HttpOnly = true HttpOnly = true
}; };

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -69,7 +70,7 @@ public class Program
builder.WebHost.ConfigureKestrel(options => builder.WebHost.ConfigureKestrel(options =>
{ {
options.ListenLocalhost( options.ListenLocalhost(
int.Parse(builder.Configuration.GetValue<string>("INTERNAL_PORT")!)); int.Parse(builder.Configuration.GetValue<string>("INTERNAL_PORT") ?? "8080"));
}); });
builder.Services.Configure<ForwardedHeadersOptions>(options => builder.Services.Configure<ForwardedHeadersOptions>(options =>
@ -94,7 +95,7 @@ public class Program
var app = builder.Build(); var app = builder.Build();
app.UseStaticFiles(); app.UseStaticFiles(UrlHelper.GetSubPath.TrimEnd('/'));
app.UseCors("AllowAll"); app.UseCors("AllowAll");
app.UseCustomSerilog(); app.UseCustomSerilog();
app.UseForwardedHeaders(); app.UseForwardedHeaders();