in colors-e2e/colors-fd/main.go [13:113]
func main() {
// Define a simple webpage to display the color information
tmpl := template.Must(template.New("").Parse(`
<!DOCTYPE html>
<html>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<head>
<title>My Color Application</title>
</head>
<body>
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<span class="navbar-brand mb-0 h1">My Color Application</span>
</div>
</nav>
<div class="container">
<div class="row align-items-start">
<div class="col-4">
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{{range .ConfigValues}}
<tr>
<td>{{.Name}}</td>
<td>{{.Value}}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
<div class="col">
<h1>API Request Value Stream</h1>
<table class="table">
<thead>
<tr>
<th>Time</th>
<th>Name</th>
<th>Color</th>
</tr>
</thead>
<tbody id="apiTable">
</tbody>
</table>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<script>
setInterval(function() {
// Get the data from the API endpoint.
var xhr = new XMLHttpRequest();
xhr.open("GET", "/api/data");
xhr.onload = function() {
// Parse the JSON response.
var data = JSON.parse(xhr.responseText);
// Append the data to the table.
for (var i = 0; i < data.length; i++) {
var row = document.createElement("tr");
row.innerHTML = ` + "`<td>${data[i].time}</td> <td>${data[i].name}</td> <td style=\"background-color:${data[i].color}\">${data[i].color}</td>`;" + `
document.getElementById("apiTable").prepend(row);
}
};
xhr.send();
}, 1000);
</script>
</body>
</html>
`))
hostname := os.Getenv("HOSTNAME")
remoteColorService := os.Getenv("AppClrScv")
// Define a handler to return the webpage
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Render the template.
tmpl.Execute(w, TemplateModel{ConfigValues: GetAppValues()})
})
// Define the route to return the color data queried by the website
http.HandleFunc("/api/data", func(w http.ResponseWriter, r *http.Request) {
if remoteColorService == "" {
ReturnColorData(hostname, "red", w)
} else {
name, color, err := getColorName("http://" + remoteColorService + "/color")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
ReturnColorData(name, color, w)
}
})
// Listen on port 8080.
http.ListenAndServe(":8080", nil)
}