Skip to content

Commit ae716f0

Browse files
committed
Make app.constants being served from the server
1 parent fa2a922 commit ae716f0

File tree

78 files changed

+182
-116
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+182
-116
lines changed

frontend/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" href="favicon.ico" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<script type="text/javascript" src="./js/app.constants.js"></script>
87
<title>ServicePulse</title>
98
</head>
109
<body>

frontend/src/main.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,31 @@ async function conditionallyEnableMocking() {
1717
return worker.start();
1818
}
1919

20-
// eslint-disable-next-line promise/catch-or-return,promise/always-return
21-
conditionallyEnableMocking().then(() => {
22-
mount({ router: makeRouter() });
23-
});
20+
// eslint-disable-next-line promise/catch-or-return
21+
conditionallyEnableMocking()
22+
.then(async () => {
23+
const response = await fetch("js/app.constants.json", {
24+
method: "GET",
25+
});
26+
27+
// eslint-disable-next-line promise/always-return
28+
if (response.ok) {
29+
const appConstants = await response.json();
30+
window.defaultConfig = appConstants;
31+
} else {
32+
const devConstants = await fetch("js/app.constants.js", {
33+
method: "GET",
34+
});
35+
if (devConstants.ok) {
36+
const scriptText = await devConstants.text();
37+
const script = document.createElement("script");
38+
script.type = "text/javascript";
39+
script.text = scriptText;
40+
document.head.appendChild(script);
41+
}
42+
}
43+
})
44+
// eslint-disable-next-line promise/always-return
45+
.then(() => {
46+
mount({ router: makeRouter() });
47+
});

src/ServiceControl/Hosting/Commands/RunCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ public override async Task Execute(HostArguments args, Settings settings)
2525

2626
var app = hostBuilder.Build();
2727
app.UseServiceControl()
28+
.UseMiddleware<AppConstantsMiddleware>()
2829
.UseDefaultFiles()
2930
.UseStaticFiles();
31+
3032
await app.RunAsync(settings.RootUrl);
3133
}
3234
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace ServiceControl.Infrastructure.WebApi
2+
{
3+
using System.Net.Mime;
4+
using System.Text.Json;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Http;
7+
8+
class AppConstantsMiddleware
9+
{
10+
readonly RequestDelegate next;
11+
12+
static AppConstantsMiddleware() => FileVersion = ServiceControlVersion.GetFileVersion();
13+
static readonly string FileVersion;
14+
public AppConstantsMiddleware(RequestDelegate next)
15+
{
16+
this.next = next;
17+
}
18+
19+
public async Task InvokeAsync(HttpContext context)
20+
{
21+
if (context.Request.Path.StartsWithSegments("/js/app.constants.json"))
22+
{
23+
// TODO: Populate some of these settings dynamically from the config settings the user has set
24+
var constants = new
25+
{
26+
default_route = "/dashboard",
27+
service_control_url = "api/",
28+
monitoring_urls = new[] { "http://localhost:33633/" },
29+
showPendingRetry = true,
30+
version = FileVersion
31+
};
32+
33+
context.Response.ContentType = MediaTypeNames.Text.JavaScript;
34+
var options = new JsonSerializerOptions { PropertyNamingPolicy = null };
35+
await context.Response.WriteAsync(JsonSerializer.Serialize(constants, options));
36+
return;
37+
}
38+
39+
await next(context);
40+
}
41+
}
42+
}

src/ServiceControl/wwwroot/assets/CodeEditor-QX4Pux2B.js renamed to src/ServiceControl/wwwroot/assets/CodeEditor-a-l87Jnu.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ServiceControl/wwwroot/assets/CodeEditor-QX4Pux2B.js.map renamed to src/ServiceControl/wwwroot/assets/CodeEditor-a-l87Jnu.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ServiceControl/wwwroot/assets/ConnectionSetupView-DOnHS534.js renamed to src/ServiceControl/wwwroot/assets/ConnectionSetupView-CnHqgatk.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)