-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpservice.go
67 lines (57 loc) · 1.41 KB
/
httpservice.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"time"
)
type Profile struct {
GitHash string
NameOfTheProject string
}
var GitCommit string
func camelRegexp(str string) string {
re := regexp.MustCompile(`([A-Z])`)
//(^[^A-Z]*|[A-Z]*)([A-Z][^A-Z]+|$)
str = re.ReplaceAllString(str, ` $1`)
str = strings.Trim(str, " ")
return str
}
func world(w http.ResponseWriter, r *http.Request) {
value := r.URL.Query().Get("name")
if len(value) > 0 {
fmt.Fprintln(w, "Hello", camelRegexp(value))
CheckHTTPStatus()
} else if r.URL.Path[1:] == "helloworld" {
fmt.Fprintf(w, "Hello Stranger!")
CheckHTTPStatus()
} else if r.URL.Path[1:] == "versionz" {
profile := Profile{GitCommit, "httpservice"}
js, err := json.Marshal(profile)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
CheckHTTPStatus()
}
}
func CheckHTTPStatus() {
resp, _ := http.Get("http://localhost:8080")
log.Println("HTTP Response Status:", resp.StatusCode, http.StatusText(resp.StatusCode))
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
log.Println("HTTP Status is in the 2xx range")
} else {
log.Println("Argh! Broken")
}
}
func main() {
http.HandleFunc("/", world)
t := time.Now()
fmt.Println(t.Format("2006-01-02-15:04:05"))
log.Fatal(http.ListenAndServe(":8080", nil))
}