-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (56 loc) · 1.34 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"flag"
"fmt"
"net/http"
"os"
"path"
"runtime"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
Version = "0.1.0"
GitTag string
GitBranch string
GitBranchState string
GitCommit string
GitLastCommitTime string
BinaryName string
BuildTime string
port string
)
func version() string {
return fmt.Sprintf("build time: %s\n%s: %s\n%s %s\nBranch: %s\nCommit: %s\n",
BuildTime,
path.Base(os.Args[0]), Version,
runtime.Version(), runtime.GOARCH,
GitBranch,
GitCommit)
}
// show prints the version var above
func show() {
fmt.Println(version())
}
func hello(w http.ResponseWriter, req *http.Request) {
fmt.Println("http <hello> request")
fmt.Fprintf(w, "Hello there, the time is %s. The app version is: %s\n", time.Now(), version())
}
func headers(w http.ResponseWriter, req *http.Request) {
fmt.Println("http <headers> request")
for name, headers := range req.Header {
for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h)
}
}
}
func main() {
flag.StringVar(&port, "port", ":8080", "http service listen port")
flag.Parse()
show()
fmt.Println("serve http service")
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/hello", hello)
http.HandleFunc("/headers", headers)
http.ListenAndServe(port, nil)
}