-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
77 lines (67 loc) · 1.67 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
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/julienschmidt/httprouter"
"github.com/kelseyhightower/envconfig"
_ "github.com/lib/pq"
)
var (
config configuration
db *sql.DB
)
func initServer() *http.Server {
router := httprouter.New()
// Add a default root 200 handler
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {})
// Add a version endpoint
router.GET("/ami-version", makeRequestHandlerFile("version info", "/etc/ami_version"))
addHealthHandlers(router)
addStatusHandlers(router)
if config.EnableDebugEndpoints {
log.Print("Enabling Debug Endpoints")
addDebugHandlers(router)
}
return &http.Server{
Handler: router,
Addr: fmt.Sprintf(":%d", config.Port),
}
}
func initDB() {
var err error
db, err = sql.Open("postgres", config.Connstr)
if err == nil {
log.Printf("Connected to PGBouncer database")
} else {
db = nil
log.Printf("Could not open database: %s", err)
}
}
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() {
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()
log.Printf("Listening on port %d", config.Port)
if err := webserver.ListenAndServe(); err != nil {
log.Fatalf("Error occured while listening for connections: %s", err)
}
}