From 4a326258bfa973ddd26f08131b9a12aac27cf07b Mon Sep 17 00:00:00 2001 From: dragongoose Date: Sat, 30 Sep 2023 11:35:41 -0400 Subject: [PATCH] initial http server + templating --- go.mod | 2 ++ go.sum | 2 ++ main.go | 65 +++++++++++++++++++++++++++++++++++ public/index.css | 0 templates/index.html.mustache | 13 +++++++ 5 files changed, 82 insertions(+) create mode 100644 go.sum create mode 100644 main.go create mode 100644 public/index.css create mode 100644 templates/index.html.mustache diff --git a/go.mod b/go.mod index e3c1dda..ec0bc9c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.exozy.me/exozyme/status go 1.21.1 + +require github.com/cbroglie/mustache v1.4.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7d69337 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/cbroglie/mustache v1.4.0 h1:Azg0dVhxTml5me+7PsZ7WPrQq1Gkf3WApcHMjMprYoU= +github.com/cbroglie/mustache v1.4.0/go.mod h1:SS1FTIghy0sjse4DUVGV1k/40B1qE1XkD9DtDsHo9iM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..954544a --- /dev/null +++ b/main.go @@ -0,0 +1,65 @@ +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 + } + + // Render the template with the data and write the output to the response writer + 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)) +} diff --git a/public/index.css b/public/index.css new file mode 100644 index 0000000..e69de29 diff --git a/templates/index.html.mustache b/templates/index.html.mustache new file mode 100644 index 0000000..5d21e39 --- /dev/null +++ b/templates/index.html.mustache @@ -0,0 +1,13 @@ + + +exozyme - status + + + +

exozyme status

+ +{{#.}} +
  • {{Name}} {{Ok}} - {{Status}}
  • +{{/.}} + + \ No newline at end of file