From aca7f8a8b0331c6f1f264a6bda160690b4d7d3b5 Mon Sep 17 00:00:00 2001 From: The_Spider Date: Sat, 30 Oct 2021 13:49:17 -0500 Subject: [PATCH] some template stuff --- cmd/go-temper/httpServer.go | 32 +++++++++++++++++++++---- cmd/go-temper/httpTemplates.go | 43 ++++++++++++++++++++++++++++++++++ cmd/go-temper/main.go | 18 ++++++++++---- 3 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 cmd/go-temper/httpTemplates.go diff --git a/cmd/go-temper/httpServer.go b/cmd/go-temper/httpServer.go index 480cf1d..51d8486 100644 --- a/cmd/go-temper/httpServer.go +++ b/cmd/go-temper/httpServer.go @@ -1,11 +1,14 @@ package main import ( + "bytes" "log" - "net/http" "strconv" "time" + "html/template" + "net/http" + "github.com/prometheus/client_golang/prometheus/promhttp" ) @@ -30,10 +33,7 @@ func httpServer(host string, port int) { IdleTimeout: time.Duration(config.WebSrvIdleTimeout) * time.Second, } - path.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - httpAccessLog(r) - w.Write([]byte("web-root")) - }) + path.HandleFunc("/", webRoot) path.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) { httpAccessLog(r) crossSiteOrigin(w) @@ -44,3 +44,25 @@ func httpServer(host string, port int) { log.Fatalf("[ERROR] %s\n", err) } } + +func webRoot(w http.ResponseWriter, r *http.Request) { + outputData := struct { + Title string + ErrorCode int + }{ + Title: "forbidden", + ErrorCode: http.StatusForbidden, + } + + tmplt, err := template.New("webRoot").Parse(webErrorTmplt()) + if err != nil { + log.Fatalf("[FATAL] Unable to parse webRoot template: %v\n", err) + } + + var output bytes.Buffer + if err := tmplt.Execute(&output, outputData); err != nil { + log.Fatalf("[FATAL] Unable to process webRoot template: %v\n", err) + } + + w.Write(output.Bytes()) +} diff --git a/cmd/go-temper/httpTemplates.go b/cmd/go-temper/httpTemplates.go new file mode 100644 index 0000000..22a363f --- /dev/null +++ b/cmd/go-temper/httpTemplates.go @@ -0,0 +1,43 @@ +package main + +func webErrorTmplt() string { + return ` + + + + {{ .Title }} + + + + +

{{ printf "%d" .ErrorCode }}

+ +` +} diff --git a/cmd/go-temper/main.go b/cmd/go-temper/main.go index 2fdadde..114cd42 100644 --- a/cmd/go-temper/main.go +++ b/cmd/go-temper/main.go @@ -2,15 +2,24 @@ package main import ( "log" - "time" - - "gitea.smoothnet.org/nhyatt/go-temper/internal/temper" + "os" + "os/signal" + "syscall" ) +func forever() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + <-c + + log.Println("[INFO] Shutdown signal received...") +} + func main() { go httpServer("0.0.0.0", 8080) + forever() - for { + /*for { temp, err := temper.GetTemperature() if err != nil { log.Printf("[WARNING] Unable to get temperature from device: %v\n", err) @@ -25,4 +34,5 @@ func main() { time.Sleep(time.Duration(5 * time.Second)) } + */ }