feat: Enhance static file serving and routing for development and production environments #3

This commit is contained in:
Marek Lesko
2025-07-27 16:40:13 +00:00
parent b991f009c7
commit af3b0aefe4
4 changed files with 47 additions and 3 deletions

View File

@@ -7,7 +7,6 @@ stages: # Define the stages of the pipeline.
web-build: # This job runs in the build stage, which runs first.
stage: build
image: node:24
parallel: 2
script:
- cd Web
- npm install
@@ -22,7 +21,6 @@ web-build: # This job runs in the build stage, which runs first.
api-build:
stage: build
image: mcr.microsoft.com/dotnet/sdk:8.0
parallel: 2
script:
- cd Api
- dotnet publish Api.csproj --output ./build --runtime linux-x64 --configuration Release --self-contained true

3
Api/.gitignore vendored
View File

@@ -1,4 +1,5 @@
obj/
bin/
.vs/
build/
build/
**/core

View File

@@ -56,6 +56,51 @@ namespace Api
app.UseHttpsRedirection();
}
if (app.Environment.IsDevelopment())
{
var staticFilePath = "/workspaces/centrum/Web/dist/Web/browser";
app.UseDefaultFiles(new DefaultFilesOptions
{
FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(staticFilePath),
DefaultFileNames = new List<string> { "index.html" }
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(staticFilePath),
RequestPath = ""
});
// Angular routing fallback
app.Use(async (context, next) =>
{
await next();
var path = context.Request.Path.Value ?? string.Empty;
if (context.Response.StatusCode == 404 &&
!System.IO.Path.HasExtension(path) &&
!path.StartsWith("/api"))
{
context.Request.Path = "/index.html";
await next();
}
});
}
else
{
app.UseDefaultFiles(); // Uses wwwroot by default
app.UseStaticFiles();
// Angular routing fallback for production
app.Use(async (context, next) =>
{
await next();
var path = context.Request.Path.Value ?? string.Empty;
if (context.Response.StatusCode == 404 &&
!System.IO.Path.HasExtension(path) &&
!path.StartsWith("/api"))
{
context.Request.Path = "/index.html";
await next();
}
});
}
app.UseAuthentication();
app.UseAuthorization();

BIN
Api/core

Binary file not shown.