initial http server + templating
This commit is contained in:
parent
b340f49965
commit
4a326258bf
5 changed files with 82 additions and 0 deletions
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
|||
module git.exozy.me/exozyme/status
|
||||
|
||||
go 1.21.1
|
||||
|
||||
require github.com/cbroglie/mustache v1.4.0
|
||||
|
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -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=
|
65
main.go
Normal file
65
main.go
Normal file
|
@ -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))
|
||||
}
|
0
public/index.css
Normal file
0
public/index.css
Normal file
13
templates/index.html.mustache
Normal file
13
templates/index.html.mustache
Normal file
|
@ -0,0 +1,13 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>exozyme - status </title>
|
||||
<link rel="stylesheet" href="/public/index.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>exozyme status</h1>
|
||||
|
||||
{{#.}}
|
||||
<li>{{Name}} {{Ok}} - {{Status}}</li>
|
||||
{{/.}}
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue