forked from deliveroo/pgbouncer-healthcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (93 loc) · 2.32 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
96
97
98
99
100
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"time"
_ "github.com/jackc/pgx/stdlib"
"github.com/kelseyhightower/envconfig"
)
var (
config configuration
db *sql.DB
)
func initServer(ctx context.Context) *http.Server {
var mux Mux
// Add a default root 200 handler
mux.OK("/")
// Add a version endpoint
mux.File("/ami-version", "version info", "/etc/ami_version")
// Add a health handler
mux.GET("/health", requestHandler(handleHealth))
addStatusHandlers(&mux)
if config.EnableDebugEndpoints {
log.Print("Enabling Debug Endpoints")
addDebugHandlers(&mux)
}
return &http.Server{
Addr: fmt.Sprintf(":%d", config.Port),
Handler: &mux,
ReadHeaderTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
BaseContext: func(net.Listener) context.Context {
return ctx
},
}
}
func initDB() {
var err error
db, err = sql.Open("pgx", config.Connstr)
if err != nil {
log.Printf("Connection string is invalid: %s", err)
os.Exit(1)
}
if err := db.Ping(); err != nil {
log.Printf("Could not connect to database: %s", err)
os.Exit(1)
}
log.Printf("Connected to PGBouncer database")
}
func version() {
fmt.Fprintf(os.Stderr, "%s version %s\n", os.Args[0], VERSION)
fmt.Fprintf(os.Stderr, "built %s\n", BUILD_DATE)
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := envconfig.Process("", &config); err != nil {
log.Fatalf("Could not process configuration: %s", err)
}
flag.Usage = func() {
version()
fmt.Fprintln(os.Stderr, "\nUsage:")
flag.PrintDefaults()
if err := envconfig.Usage("", &config); err != nil {
log.Fatalf("Could not process configuration: %s", err)
}
}
flag.Parse()
initDB()
webserver := initServer(ctx)
log.Printf("Listening on port %d", config.Port)
go func() {
if err := webserver.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("Error occured while listening for connections: %s", err)
}
}()
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt)
<-s
cancel()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownCancel()
if err := webserver.Shutdown(shutdownCtx); err != nil {
log.Printf("HTTP server Shutdown: %v", err)
}
}