-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (41 loc) · 1.1 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"
"math/rand"
"net/http"
"strconv"
"time"
)
var (
buildVersion string
buildDate string
)
func hello(w http.ResponseWriter, r *http.Request) {
// Get the HTTP method from the request
method := r.Method
// 10% return 500
randomNumber := rand.Intn(100)
var statusCode int
if randomNumber < 90 {
statusCode = http.StatusOK
} else {
statusCode = http.StatusInternalServerError
}
w.WriteHeader(statusCode)
strStatusCode := strconv.Itoa(statusCode)
currentTime := time.Now().Format("2006/01/02 15:04:05")
fmt.Fprintf(w, "<h1>KiKi Api</h1>")
fmt.Fprintf(w, "Build Version: %s</br>", buildVersion)
fmt.Fprintf(w, "Build Date: %s</br>", buildDate)
fmt.Fprintf(w, "%s %s request received</br>", currentTime, method)
fmt.Fprintf(w, "%s %d response returned</br>", currentTime, statusCode)
// Return the HTTP method as the response
log.Println(method + " request received")
log.Println(strStatusCode + " response returned")
}
func main() {
http.HandleFunc("/", hello)
log.Println("Listening on port 8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}