-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
52 lines (41 loc) · 1.11 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/kelseyhightower/app-healthz/healthz"
)
var version = "1.0.0"
func main() {
log.Println("Starting app...")
httpAddr := os.Getenv("HTTP_ADDR")
databaseUsername := os.Getenv("DATABASE_USERNAME")
databasePassword := os.Getenv("DATABASE_PASSWORD")
databaseHost := os.Getenv("DATABASE_HOST")
databaseName := os.Getenv("DATABASE_NAME")
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
log.Println("Initializing database connection pool...")
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s)/%s",
databaseUsername, databasePassword, databaseHost, databaseName)
hc := &healthz.Config{
Hostname: hostname,
Database: healthz.DatabaseConfig{
DriverName: "mysql",
DataSourceName: dataSourceName,
},
}
healthzHandler, err := healthz.Handler(hc)
if err != nil {
log.Fatal(err)
}
http.Handle("/healthz", healthzHandler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, html, hostname, version)
})
log.Printf("HTTP service listening on %s", httpAddr)
http.ListenAndServe(httpAddr, nil)
}