-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.go
58 lines (42 loc) · 1.18 KB
/
hello.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
package main
import (
"net/http"
"fmt"
"time"
"html"
"log"
"os"
"math/rand"
)
var body = `
<html>
<head><title>Hello, world!</title></head>
<body style="background-color:%s;">
<h1>Hello, World from %s</h1>
<p>
The time is <b>%s</b>.
</p>
<div style="position: fixed;left: 0;bottom: 10;text-align: center;width: 100%%;background-color: white">
Release %s, build date: %s
</div>
</body>
</html>`
var BuildDate string; // https://stackoverflow.com/a/11355611
var Release string; // https://stackoverflow.com/a/11355611
//Go application entrypoint
func main() {
rand.Seed(time.Now().UnixNano());
color := fmt.Sprintf("hsl(%d, 75%%, 85%%)", rand.Intn( 360 ))
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set( "Connection", "Close" )
w.Header().Set( "Refresh", "2; url=http://" + r.Host + r.RequestURI )
fmt.Fprintf(w, body,
color,
html.EscapeString( os.Getenv( "HOSTNAME") ),
html.EscapeString( time.Now().Format(time.Stamp) ),
Release,
BuildDate )
})
fmt.Println("Listening on :8080");
log.Fatal( fmt.Println(http.ListenAndServe(":8080", nil)) );
}