status/main.go

65 lines
1.1 KiB
Go
Raw Normal View History

2023-09-30 15:35:41 +00:00
package main
import (
"log"
"net/http"
"github.com/cbroglie/mustache"
)
type ServiceStatus struct {
Name string
Ok bool
Status string
}
func parseAndRenderTemplate(templateLocation string, data interface{}) (string, error) {
tmpl, err := mustache.ParseFile(templateLocation)
if err != nil {
log.Println("Failed to parse template:", err)
return "", err
}
res, err := tmpl.Render(data)
if err != nil {
log.Println("Failed to render template:", err)
return "", err
}
return res, nil
}
func rootPage(w http.ResponseWriter, r *http.Request) {
//hardcoded for test
data := []ServiceStatus{
{
Name: "test",
Ok: true,
Status: "200",
},
{
Name: "test",
Ok: true,
Status: "200",
},
{
Name: "test",
Ok: true,
Status: "200",
},
}
parsedTemplate, err := parseAndRenderTemplate("templates/index.html.mustache", data)
if err != nil {
http.Error(w, err.Error(), 500)
}
w.Write([]byte(parsedTemplate))
}
func main() {
http.HandleFunc("/", rootPage)
log.Fatal(http.ListenAndServe(":3333", nil))
}