about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-03-24 14:51:50 +0100
committerEmile <hanemile@protonmail.com>2019-03-24 14:51:50 +0100
commitda201854e9f9f25bad1720d5368648d5bc49c480 (patch)
treeda81375a4a47e35feaedb31b91b181f2a45ae1fc
parentfd94a3102282effc9fbed8075063b97fe2c11ab6 (diff)
committing untracked files HEAD main
-rw-r--r--db.go28
-rw-r--r--distributor.env6
-rw-r--r--go.mod6
-rw-r--r--go.sum8
-rw-r--r--http.go44
-rw-r--r--main.go125
6 files changed, 217 insertions, 0 deletions
diff --git a/db.go b/db.go
new file mode 100644
index 0000000..147528b
--- /dev/null
+++ b/db.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+	"database/sql"
+	"fmt"
+	"log"
+)
+
+// connectToDB returns a pointer to an sql database writing to the database
+func connectToDB() *sql.DB {
+	//connStr := fmt.Sprintf("user=%s dbname=%s sslmode=%s", DBUSER, DBNAME, DBSSLMODE)
+	connStr := fmt.Sprintf("host=%s port=%d user=%s "+
+		"password=%s dbname=%s sslmode=disable",
+		DBHOST, DBPORT, DBUSER, DBPASSWD, DBNAME)
+	db := dbConnect(connStr)
+	return db
+}
+
+// dbConnect connects to a PostgreSQL database
+func dbConnect(connStr string) *sql.DB {
+	// connect to the database
+	db, err := sql.Open("postgres", connStr)
+	if err != nil {
+		log.Fatalf("[ E ] connection: %v", err)
+	}
+
+	return db
+}
diff --git a/distributor.env b/distributor.env
new file mode 100644
index 0000000..16014ce
--- /dev/null
+++ b/distributor.env
@@ -0,0 +1,6 @@
+DBURL=postgresql
+DBUSER=postgres
+DBPASSWD=
+DBPORT=5432
+DBPROJECTNAME=postgres
+
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..4e3b49d
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,6 @@
+module distributor
+
+require (
+	git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190320193312-4bf9f11232ce
+	github.com/gorilla/mux v1.7.0
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..76131c2
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,8 @@
+git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190320193312-4bf9f11232ce h1:YHFibkO9NFOKSM1BzFFBZgzk7COxZ7cNgA0am2V5mrY=
+git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190320193312-4bf9f11232ce/go.mod h1:D5fanr1NuL8GqX+Eslmpf0+l9dMNZVabmEoysbDlKW0=
+git.darknebu.la/GalaxySimulator/structs v0.0.0-20190205205735-9dd56b9448e5 h1:aEQHEERwdLfRJrXb867wZzRMs1ym+j0zDys3opLWPew=
+git.darknebu.la/GalaxySimulator/structs v0.0.0-20190205205735-9dd56b9448e5/go.mod h1:LSDIBBC7IcWERm4wlDAroMpsUP7zJ1yDZFlQnP/UIsQ=
+github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
+github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
+github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
+github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
diff --git a/http.go b/http.go
new file mode 100644
index 0000000..91c75e3
--- /dev/null
+++ b/http.go
@@ -0,0 +1,44 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"net/http"
+)
+
+func indexHandler(w http.ResponseWriter, r *http.Request) {
+	responseString := `
+<html>
+	<head>
+		<title>Distributor Container</title>
+	</head>
+	<body>
+		<h1>Distributor</h1>
+		<p>
+			<a href="/distributor">Distributor</a>
+		</p>
+	</body>
+</html>`
+	_, _ = fmt.Fprintf(w, responseString)
+}
+
+func distributorHandler(w http.ResponseWriter, r *http.Request) {
+	log.Println("The distributorHandler was accessed")
+
+	// if the starIdBufferChannel is not filled yet, fill it
+	if len(idBufferChannel) == 0 {
+		log.Println("The idBufferChannel is empty, fetching new stars")
+		fillStarIdBufferChannel()
+	}
+
+	// get a single id from the idBufferChannel
+	log.Println("Getting an id from the idBufferChannel")
+	id := <-idBufferChannel
+	log.Println("Done...")
+
+	// return the id using the http.ResponseWriter w
+	_, _ = fmt.Fprintf(w, "%d", id)
+
+	log.Printf("Done providing a starID (%d) from the StarBufferHandler", id)
+
+}
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..36a7630
--- /dev/null
+++ b/main.go
@@ -0,0 +1,125 @@
+package main
+
+import (
+	"database/sql"
+	"flag"
+	"fmt"
+	"github.com/gorilla/mux"
+	"log"
+	"net/http"
+	"os"
+	"strconv"
+
+	"git.darknebu.la/GalaxySimulator/db-actions"
+)
+
+var (
+	db                *sql.DB
+	currentStarBuffer int64 = 1
+	idBufferChannel         = make(chan int64, 100000000)
+
+	// local http api
+	port string
+
+	// define the parameters needed to connect to the database
+	DBHOST    = "postgres.docker.localhost"
+	DBPORT    = 5432
+	DBUSER    = "postgres"
+	DBPASSWD  = ""
+	DBNAME    = "postgres"
+	DBSSLMODE = "disable"
+)
+
+func listOfStarIDs(treeindex int64) []int64 {
+	log.Printf("getting a list of stars using the treeindex %d", treeindex)
+
+	// get a list of all stars inside of the treeindex
+	listofstars := db_actions.GetListOfStarIDsTimestep(db, treeindex)
+
+	return listofstars
+}
+
+func fillStarIdBufferChannel() {
+	log.Println("Filling the idBufferChannel")
+
+	// get a list of ids using the currentStarBuffer value
+	// the currentStarBuffer value is a counter keeping track of which galaxy is going to
+	// be inserted into the idBufferChannel next
+	listOfStarIDs := listOfStarIDs(currentStarBuffer)
+	log.Printf("len(listOfStarIDs: %d)", len(listOfStarIDs))
+
+	// insert all the ids from the list of ids into the idBufferChannel
+	for _, id := range listOfStarIDs {
+		idBufferChannel <- id
+	}
+
+	// increase the currentStarBuffer counter
+	currentStarBuffer += 1
+}
+
+func getFlags() {
+	// get the port on which the service should be hosted and the url of the database
+	flag.StringVar(&port, "port", "8080", "port used to host the service")
+	flag.Parse()
+	log.Println("[ ] Done loading the flags")
+}
+
+func pingDB() {
+	// ping the db
+	err := db.Ping()
+	if err != nil {
+		panic(err)
+	}
+	log.Println("[ ] Done Pinging the DB")
+}
+
+func getEnvironmentVariables() {
+	// get the data that should be used to connect to the database
+	DBHOST = os.Getenv("DBURL")
+	if DBHOST == "" {
+		DBHOST = "postgresql.docker.localhost"
+	}
+
+	DBUSER = os.Getenv("DBUSER")
+	if DBUSER == "" {
+		DBUSER = "postgres"
+	}
+
+	DBPASSWD = os.Getenv("DBPASSWD")
+	if DBPASSWD == "" {
+		DBPASSWD = ""
+	}
+
+	DBPORT, _ := strconv.ParseInt(os.Getenv("DBPORT"), 10, 64)
+	if DBPORT == 0 {
+		DBPORT = 5432
+	}
+
+	DBNAME = os.Getenv("DBNAME")
+	if DBNAME == "" {
+		DBNAME = "postgres"
+	}
+
+	log.Printf("DBURL: %s", DBHOST)
+	log.Printf("DBUSER: %s", DBUSER)
+	log.Printf("DBPASSWD: %s", DBPASSWD)
+	log.Printf("DBPORT: %d", DBPORT)
+	log.Printf("DBPROJECTNAME: %s", DBNAME)
+	log.Printf("frontend port: %s", port)
+}
+
+func main() {
+	getFlags()
+	getEnvironmentVariables()
+
+	db = connectToDB()
+	db.SetMaxOpenConns(75)
+	pingDB()
+
+	router := mux.NewRouter()
+	router.HandleFunc("/", indexHandler).Methods("GET")
+	router.HandleFunc("/distributor", distributorHandler).Methods("GET")
+
+	log.Printf("[ ] Distributor on localhost:%s", port)
+	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), router))
+}