-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
65 lines (43 loc) · 1.13 KB
/
server.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
package main
import ("net/http"
"os"
"fmt"
"log"
"io/ioutil"
"time"
)
var startedAt = time.Now()
func main() {
http.HandleFunc("/", Hello)
http.HandleFunc("/healthz", Healthz)
http.HandleFunc("/secrets", Secret)
http.HandleFunc("/configmap", ConfigMap)
http.ListenAndServe(":80", nil)
}
func Hello(w http.ResponseWriter, r *http.Request) {
name := os.Getenv("NAME")
age := os.Getenv("AGE")
fmt.Fprintf(w, "Hello, I'm %s. I'm %s.",name,age)
}
func ConfigMap(w http.ResponseWriter, r *http.Request){
data, err := ioutil.ReadFile("myfamily/family.txt")
if err != nil {
log.Fatalf("Error reading file",err)
}
fmt.Fprintf(w, "My Family: %s. ",string(data))
}
func Secret(w http.ResponseWriter, r *http.Request) {
user := os.Getenv("USER")
password := os.Getenv("PASSWORD")
fmt.Fprintf(w, "User: %s. Password: %s.",user,password)
}
func Healthz(w http.ResponseWriter, r *http.Request){
duration := time.Since(startedAt)
if duration.Seconds() < 10 {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("Duration: %v", duration.Seconds())))
}else{
w.WriteHeader(200)
w.Write([]byte(fmt.Sprintf("ok")))
}
}