about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-10-18 22:42:32 +0200
committerEmile <hanemile@protonmail.com>2019-10-18 22:42:32 +0200
commit217b6d72f4c1ecb7586ab51ff6fa6c3f010016b2 (patch)
treee14b0966061eba9c3ddcebb78767918e9ab18ec4
parent02e63b59571e356ab36b67c456b88c524241ccc6 (diff)
basic setup
-rw-r--r--src/http.go33
-rw-r--r--src/main.go63
2 files changed, 96 insertions, 0 deletions
diff --git a/src/http.go b/src/http.go
new file mode 100644
index 0000000..a813ae6
--- /dev/null
+++ b/src/http.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+	"net/http"
+
+	"github.com/gorilla/mux"
+)
+
+var (
+	port *int
+)
+
+func registerHTTPFlags() {
+	port = flag.Int("port", 8080, "The port for HTTP")
+}
+
+func setupHTTPServer() http.Server {
+	r := mux.NewRouter()
+
+	r.HandleFunc("/", indexHandler)
+
+	return http.Server{
+		Addr:    fmt.Sprintf("0.0.0.0:%d", *port),
+		Handler: r,
+	}
+}
+
+// Host the index file
+func indexHandler(w http.ResponseWriter, r *http.Request) {
+	fmt.Fprintf(w, "%s", "Hello World!\n")
+}
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)
+}