some template stuff
This commit is contained in:
parent
508babd9c9
commit
aca7f8a8b0
@ -1,11 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,10 +33,7 @@ func httpServer(host string, port int) {
|
|||||||
IdleTimeout: time.Duration(config.WebSrvIdleTimeout) * time.Second,
|
IdleTimeout: time.Duration(config.WebSrvIdleTimeout) * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
path.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
path.HandleFunc("/", webRoot)
|
||||||
httpAccessLog(r)
|
|
||||||
w.Write([]byte("web-root"))
|
|
||||||
})
|
|
||||||
path.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
|
path.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
|
||||||
httpAccessLog(r)
|
httpAccessLog(r)
|
||||||
crossSiteOrigin(w)
|
crossSiteOrigin(w)
|
||||||
@ -44,3 +44,25 @@ func httpServer(host string, port int) {
|
|||||||
log.Fatalf("[ERROR] %s\n", err)
|
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())
|
||||||
|
}
|
||||||
|
43
cmd/go-temper/httpTemplates.go
Normal file
43
cmd/go-temper/httpTemplates.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func webErrorTmplt() string {
|
||||||
|
return `<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>{{ .Title }}</title>
|
||||||
|
<style type="text/css">
|
||||||
|
body {
|
||||||
|
width: 100%;
|
||||||
|
height: 100vh;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
flex-direction: column;
|
||||||
|
family: 'Poppins', sans-serif;
|
||||||
|
background-image: linear-gradient(45deg, #f6d200 25%, #181617 25%, #181617 50%, #f6d200 50%, #f6d200 75%, #181617 75%, #181617 100%);
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
text-transform: uppercase;
|
||||||
|
background: repeating-linear-gradient(
|
||||||
|
45deg,
|
||||||
|
#f6d200 ,
|
||||||
|
#f6d200 10px,
|
||||||
|
#181617 10px,
|
||||||
|
#181617 20px
|
||||||
|
);
|
||||||
|
-webkit-background-clip: text;
|
||||||
|
-webkit-text-fill-color: transparent;
|
||||||
|
font-size: 384px;
|
||||||
|
margin: 0;
|
||||||
|
line-height: .7;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body id="home">
|
||||||
|
<h1 class="text"><span>{{ printf "%d" .ErrorCode }}</span></h1>
|
||||||
|
</body>
|
||||||
|
</html>`
|
||||||
|
}
|
@ -2,15 +2,24 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"gitea.smoothnet.org/nhyatt/go-temper/internal/temper"
|
"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() {
|
func main() {
|
||||||
go httpServer("0.0.0.0", 8080)
|
go httpServer("0.0.0.0", 8080)
|
||||||
|
forever()
|
||||||
|
|
||||||
for {
|
/*for {
|
||||||
temp, err := temper.GetTemperature()
|
temp, err := temper.GetTemperature()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[WARNING] Unable to get temperature from device: %v\n", err)
|
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))
|
time.Sleep(time.Duration(5 * time.Second))
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user