-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
103 lines (88 loc) · 1.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"html/template"
"net/http"
"os"
"strconv"
"fmt"
)
type config struct {
Message string
BackgroundColor string
PageReloadTime int
}
const htmlTemplate = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CloudAcademy</title>
</head>
<link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet">
<style>
body {
position: relative;
height: 100vh;
margin: 0;
background-color: {{.BackgroundColor}};
}
h1 {
position: absolute;
top: 40%;
transform: translateY(-50%);
width: 100%;
text-align: center;
margin: 0;
font-family: 'Montserrat', sans-serif;
}
</style>
{{ if gt .PageReloadTime 0 }}
<script language="javascript">
setInterval(function(){
fetch(window.location.href)
.then(response => {
window.location.reload(1);
})
.catch(err => {
console.log(err);
});
}, {{.PageReloadTime}});
</script>
{{end}}
<body>
<h1>{{.Message}}</h1>
</body>
</html>
`
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func main() {
hostport := getEnv("HOSTPORT", "0.0.0.0:8080")
message := getEnv("MESSAGE", "follow the white rabbit...")
bgcolor := getEnv("BACKGROUND_COLOR", "yellow")
pagereloadtime := 0
if os.Getenv("AUTO_RELOAD") != "" {
pagereloadtime, _ = strconv.Atoi(os.Getenv("AUTO_RELOAD"))
pagereloadtime = pagereloadtime * 1000
}
tmpl := template.Must(template.New("main").Parse(htmlTemplate))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := config{
Message: message,
BackgroundColor: bgcolor,
PageReloadTime: pagereloadtime,
}
tmpl.Execute(w, data)
})
fmt.Println("web server launched successfully...")
fmt.Println(hostport)
fmt.Println(message)
fmt.Println(bgcolor)
fmt.Println(pagereloadtime)
http.ListenAndServe(hostport, nil)
}