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 database, err := setupDatabase() if err != nil { log.Println(err) return } db = database // 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) }