Use loading template, correctly wait for load

This commit is contained in:
Christopher LaPointe
2023-05-21 10:21:00 -04:00
parent 57db4e3f2f
commit 8afc25766f
2 changed files with 33 additions and 20 deletions

View File

@@ -4,13 +4,13 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="60">
<meta http-equiv="refresh" content="30">
<link rel="stylesheet" type="text/css" href="/__llassets/splash.css">
<title>Loading...</title>
</head>
<body>
<div>
<h1>Booting Application</h1>
<h1>Loading {{.Name}}</h1>
</div>
<div class="container">
<div class="circle" data-index="0"></div>
@@ -26,22 +26,20 @@
<div class="circle" data-index="10"></div>
</div>
<script>
function main() {
async function testFor200(url) {
const response = await fetch(url, {
method: "HEAD",
});
return response.status / 100 === 2;
}
// TODO: Would self-detect as fine, but isn't ready
// setInterval(() => {
// console.log("testing...");
// if (testFor200("/")) {
// location.reload();
// }
// }, 1000);
async function testForOk(url) {
const response = await fetch(url, {
method: "HEAD",
});
console.log(`Got ${response.status}`);
return response.status === {{.WaitForCode}};
}
setInterval(async () => {
console.log("testing...");
if (await testForOk("{{.WaitForPath}}")) {
console.log("Found! Reloading...")
location.reload();
}
}, 1000);
</script>
</body>
</html>

21
main.go
View File

@@ -6,6 +6,7 @@ import (
_ "embed"
"encoding/json"
"errors"
"html/template"
"io"
"io/fs"
"net/http"
@@ -25,6 +26,14 @@ var httpAssets embed.FS
const httpAssetPrefix = "/__llassets/"
type SplashModel struct {
Name string
WaitForCode int
WaitForPath string
}
var splashTemplate = template.Must(template.ParseFS(httpAssets, "assets/splash.html"))
var dockerClient *client.Client
type containerState struct {
@@ -157,11 +166,17 @@ func ContainerHandler(w http.ResponseWriter, r *http.Request) {
}
ct, _ := findContainerByHostname(r.Context(), host)
if ct != nil {
if ct != nil || true {
// TODO: Send response before querying anything about the container (the slow bit)
splash, _ := httpAssets.Open(path.Join("assets", Config.Splash))
w.WriteHeader(http.StatusAccepted)
io.Copy(w, splash)
renderErr := splashTemplate.Execute(w, SplashModel{
Name: host,
WaitForCode: 200, // TODO Config-based
WaitForPath: "/",
})
if renderErr != nil {
logrus.Error(renderErr)
}
// Look to start the container
state := getOrCreateState(ct.ID)