From 217b6d72f4c1ecb7586ab51ff6fa6c3f010016b2 Mon Sep 17 00:00:00 2001 From: Emile Date: Fri, 18 Oct 2019 22:42:32 +0200 Subject: basic setup --- src/http.go | 33 ++++++++++++++++++++++++++++++++ src/main.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/http.go create mode 100644 src/main.go (limited to 'src') 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) +} -- cgit 1.4.1