-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
61 lines (51 loc) · 1.24 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
package main
import (
"fmt"
"io"
"net/http"
"os"
)
var happy bool = true
func hostname() string {
return os.Getenv("HOSTNAME")
}
func serverDescription() string {
if happy {
return "Very Happy server on host " + hostname()
} else {
return "Very Sad server on host " + hostname()
}
}
func MakeSad(w http.ResponseWriter, r *http.Request) {
response := serverDescription() + " is now sad\n"
happy = false
fmt.Println(response)
io.WriteString(w, response)
return
}
func MakeHappy(w http.ResponseWriter, r *http.Request) {
response := serverDescription() + " is now happy\n"
happy = true
fmt.Println(response)
io.WriteString(w, response)
return
}
func Something(w http.ResponseWriter, r *http.Request) {
if !happy {
w.WriteHeader(http.StatusInternalServerError)
}
response := serverDescription() + " is handling request: " + r.URL.String() + "\n"
fmt.Println(response)
io.WriteString(w, response)
}
func main() {
http.HandleFunc("/something", Something)
http.HandleFunc("/sad", MakeSad)
http.HandleFunc("/happy", MakeHappy)
serverAddr := os.Getenv("SERVER_ADDR")
fmt.Printf("Starting server on address %s\n", serverAddr)
err := http.ListenAndServe(serverAddr, nil)
if err != nil {
fmt.Println("Error starting server", err)
}
}