forked from grepory/dummy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
95 lines (78 loc) · 2.04 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/quipo/statsd"
)
var healthy = true
var db_host = os.Getenv("DB_HOST")
var db_port = os.Getenv("DB_PORT")
var db_user = os.Getenv("DB_USER")
var db_pass = os.Getenv("DB_PASS")
var db_name = os.Getenv("DB_NAME")
func main() {
prefix := "demo-app."
statsdclient := statsd.NewStatsdClient("127.0.0.1:8125", prefix)
err := statsdclient.CreateSocket()
if nil != err {
log.Println(err)
os.Exit(1)
}
interval := time.Second * 5
stats := statsd.NewStatsdBuffer(interval, statsdclient)
defer stats.Close()
psqlconn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", db_host, db_port, db_user, db_pass, db_name)
db, err := sql.Open("postgres", psqlconn)
if err != nil {
log.Println(err)
}
defer db.Close()
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
stats.Incr("http.requests.get.root", 1)
hostname, err := os.Hostname()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Write([]byte(hostname))
}
}).Methods(http.MethodGet)
r.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
stats.Incr("http.requests.get.healthz", 1)
err = db.Ping()
if err != nil {
healthy = false
log.Println(err)
}
if healthy {
w.Write([]byte("healthy"))
} else {
http.Error(w, "unhealthy", http.StatusInternalServerError)
}
}).Methods(http.MethodGet)
r.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
stats.Incr("http.requests.post.healthz", 1)
err = db.Ping()
if err != nil {
healthy = false
log.Println(err)
} else {
healthy = !healthy
}
}).Methods(http.MethodPost)
r.Handle("/metrics", promhttp.Handler())
srv := &http.Server{
Handler: r,
Addr: ":8080",
WriteTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}