about summary refs log tree commit diff
path: root/src/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.go')
-rw-r--r--src/main.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main.go b/src/main.go
new file mode 100644
index 0000000..2c99242
--- /dev/null
+++ b/src/main.go
@@ -0,0 +1,63 @@
+package main
+
+import (
+	"database/sql"
+	"flag"
+	"fmt"
+	"log"
+	"net/http"
+	"os"
+	"os/signal"
+
+	_ "github.com/lib/pq"
+)
+
+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()
+
+	// React to system signals
+	signalChannel := make(chan os.Signal, 1)
+	signal.Notify(signalChannel)
+	go cleanup(signalChannel, httpServer)
+
+	////////////////////////////////////////////////////////////////////////////////
+
+	connStr := "user=postgres dbname=postgres sslmode=disable"
+	db, err := sql.Open("postgres", connStr)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	// get the current name of the challenges
+	query := fmt.Sprintf("SELECT name FROM challenges")
+	var name string
+	err = db.QueryRow(query).Scan(&name)
+	if err != nil {
+		log.Fatalf("[ E ] :", err)
+	}
+
+	fmt.Println(name)
+
+	////////////////////////////////////////////////////////////////////////////////
+
+	// 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)
+}