blob: 4af5cc4456f4459b4cd48c8b03f09e7051485780 (
plain)
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
|
package main
import (
"database/sql"
"flag"
"log"
"net/http"
"os"
"os/signal"
_ "github.com/lib/pq"
)
var (
db *sql.DB
)
func main() {
// Set up flags
log.Println("Setting up components")
registerHTTPFlags()
flag.Parse()
// Set up HTTP server
log.Printf("Running HTTP server on port %d", *port)
httpServer := setupHTTPServer()
// Set up the DB connection
db = setupDatabase()
// React to system signals
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel)
go cleanup(signalChannel, httpServer)
// Start our HTTP server
log.Fatalln(httpServer.ListenAndServe())
}
func cleanup(signalChannel chan os.Signal, server http.Server) {
// Block until we receive a signal
<-signalChannel
log.Println("Requested shutdown")
// Stop everything
log.Println("Stopping HTTP server")
server.Close()
os.Exit(0)
}
|