-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (80 loc) · 1.84 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
package main
import (
"fmt"
"html/template"
"net/http"
"strings"
"time"
)
const version = "3.0.0"
var htmlTemplate = `<html>
<head>
<title>Hello Go!</title>
</head>
<body>
{{ .TimeOfTheDay }} <b><i>{{ .Name }}</i></b>
<br><br>
<b>{{ .Date }}</b>
<br><br>
Running version: <u> {{ .Version}} </u>
</body>
</html>`
func sayHello(w http.ResponseWriter, r *http.Request) {
message := r.URL.Path
message = strings.TrimPrefix(message, "/")
message = "Hello " + message + "\nversion: " + version
fmt.Fprintf(w, message)
}
func checkHealth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}
func printVersion(w http.ResponseWriter, r *http.Request) {
message := "\n<b>" + version + "</b>\n"
fmt.Fprintf(w, message)
}
func greet(w http.ResponseWriter, r *http.Request) {
tmpl := template.New("main")
tmpl, _ = tmpl.Parse(htmlTemplate)
t := time.Now()
h := t.Hour()
path := r.URL.Path
user := strings.SplitAfter(path, "/greet/")[1]
const (
morning = "Good Morning, "
afternoon = "Good Afternoon, "
evening = "Good Evening, "
)
var message string
type Data struct {
TimeOfTheDay string
Name string
Date string
Version string
}
switch {
case h >= 0 && h < 12:
message = morning
case h >= 12 && h < 18:
message = afternoon
case h >= 18:
message = evening
}
data := Data{
TimeOfTheDay: message,
Name: user,
Date: t.Format(time.ANSIC),
Version: version,
}
//message = message + "<i> " + user + "!</i>\n\n<br>" + t.Format(time.ANSIC) + "</br>\n"
//fmt.Fprintf(w, message)
tmpl.Execute(w, &data)
}
func main() {
http.HandleFunc("/health", checkHealth)
http.HandleFunc("/", sayHello)
http.HandleFunc("/hello-go/greet/", greet)
http.HandleFunc("/hello-go/version", printVersion)
if err := http.ListenAndServe(":8000", nil); err != nil {
panic(err)
}
}