/
Startup.cs
113 lines (95 loc) · 3.63 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Microsoft.Extensions.FileProviders;
using Microsoft.Net.Http.Headers;
using Squidex.Areas.Frontend.Middlewares;
using Squidex.Hosting.Web;
using Squidex.Pipeline.Squid;
using Squidex.Web;
using Squidex.Web.Pipeline;
namespace Squidex.Areas.Frontend;
public static class Startup
{
public static void UseFrontend(this IApplicationBuilder app)
{
var environment = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
var fileProvider = environment.WebRootFileProvider;
app.UseMiddleware<EmbedMiddleware>();
if (!environment.IsDevelopment())
{
var buildFolder = new PhysicalFileProvider(Path.Combine(environment.WebRootPath, "build"));
var buildProvider = new IgnoreHashFileProvider(buildFolder);
fileProvider = new CompositeFileProvider(fileProvider, buildFolder, buildProvider);
}
app.Map("/squid.svg", builder =>
{
builder.UseMiddleware<SquidMiddleware>();
});
app.UseMiddleware<NotifoMiddleware>();
app.UseWhen(c => c.IsSpaFile(), builder =>
{
builder.UseMiddleware<SetupMiddleware>();
});
app.UseWhen(c => c.IsSpaFile() || c.IsHtmlPath(), builder =>
{
// Adjust the base for all potential html files.
builder.UseHtmlTransform(new HtmlTransformOptions
{
Transform = (html, context) =>
{
return new ValueTask<string>(html.AddOptions(context));
}
});
});
app.Use((context, next) =>
{
return next();
});
app.UseSquidexStaticFiles(fileProvider);
if (!environment.IsDevelopment())
{
// Try static files again to serve index.html.
app.UsePathOverride("/index.html");
app.UseSquidexStaticFiles(fileProvider);
}
else
{
// Forward requests to SPA development server.
app.UseSpa(builder =>
{
builder.UseProxyToSpaDevelopmentServer("https://localhost:3000");
});
}
}
private static void UseSquidexStaticFiles(this IApplicationBuilder app, IFileProvider fileProvider)
{
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
var response = context.Context.Response;
if (!string.IsNullOrWhiteSpace(context.Context.Request.QueryString.ToString()))
{
response.Headers[HeaderNames.CacheControl] = "max-age=5184000";
}
else if (string.Equals(response.ContentType, "text/html", StringComparison.OrdinalIgnoreCase))
{
response.Headers[HeaderNames.CacheControl] = "no-cache";
}
},
FileProvider = fileProvider
});
}
private static bool IsSpaFile(this HttpContext context)
{
return (context.IsIndex() || !Path.HasExtension(context.Request.Path)) && !context.IsDevServer();
}
private static bool IsDevServer(this HttpContext context)
{
return context.Request.Path.StartsWithSegments("/ws", StringComparison.OrdinalIgnoreCase);
}
}