/
main.go
51 lines (45 loc) · 1.37 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
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"time"
)
func main() {
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("/healthy called")
fmt.Fprintf(w, "healthy")
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("/ called")
fmt.Fprintf(w, "hello-world")
})
// Handle "/timeout" endpoint
// Example usage: /timeout?sleep=5
http.HandleFunc("/timeout", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("/timeout called")
// Get the sleep time from the query parameters
sleepTimeStr := r.URL.Query().Get("sleep")
if sleepTimeStr != "" {
// Convert the sleep time to an integer
sleepTime, err := strconv.Atoi(sleepTimeStr)
if err != nil {
fmt.Println("Invalid sleep time provided")
fmt.Fprintf(w, "Invalid sleep time provided")
} else {
// Sleep for the specified amount of time
fmt.Printf("Sleeping for %d seconds\n", sleepTime)
time.Sleep(time.Duration(sleepTime) * time.Second)
fmt.Fprintf(w, "Slept for %d seconds", sleepTime)
}
} else {
// If no sleep time is provided, default to 10 seconds
fmt.Println("No sleep time provided, defaulting to 10 seconds")
time.Sleep(10 * time.Second)
fmt.Fprintf(w, "Slept for 10 seconds")
}
})
log.Println("Starting server on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}