about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-03-07 20:55:54 +0100
committerEmile <hanemile@protonmail.com>2019-03-07 20:55:54 +0100
commit9afc78d2db4fcad02b0f90aa2c650d61afe6f24a (patch)
treef40b4601baeb2feb07b58144827e98c8ad6c93da
parent5d7a7c0c86457872ecc99743da5f827c8adf0793 (diff)
updated logging and the insertion of stars
-rw-r--r--main.go174
1 files changed, 161 insertions, 13 deletions
diff --git a/main.go b/main.go
index 0b06cde..c9736ed 100644
--- a/main.go
+++ b/main.go
@@ -1,23 +1,40 @@
 package main
 
 import (
+	"bufio"
 	"database/sql"
+	"encoding/csv"
 	"flag"
 	"fmt"
+	"git.darknebu.la/GalaxySimulator/structs"
 	"github.com/gorilla/mux"
+	"io"
 	"log"
 	"net/http"
 	"os"
 	"strconv"
 
 	"git.darknebu.la/GalaxySimulator/db-controller/backend"
+
+	_ "github.com/lib/pq"
 )
 
 var (
 	db *sql.DB
 )
 
+func requestInfo(r *http.Request) {
+	log.Printf("%s %s %s", r.Method, r.Host, r.URL.Path)
+}
+
+func errHandler(name string, err error) {
+	if err != nil {
+		log.Fatalf("Error: %s: %v", name, err)
+	}
+}
+
 func indexHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
 	indexString := `<html><body><h1>Galaxy Simulator Database Frontend</h1>
 
 		API:
@@ -109,78 +126,207 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
 	_, _ = fmt.Fprintf(w, "%s", indexString)
 }
 func newTreeHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
+
 	width, _ := strconv.ParseFloat(r.Form.Get("w"), 64)
 	backend.NewTree(db, width)
 }
 
 func deleteStarsHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
+
 	backend.DeleteAllNodes(db)
 }
 
 func deleteNodesHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
+
 	backend.DeleteAllStars(db)
 }
 
 func starslistGoHandler(w http.ResponseWriter, r *http.Request) {
-	backend.GetListOfStarsGo(db)
+	requestInfo(r)
+
+	listOfStarsGo := backend.GetListOfStarsGo(db)
+	_, _ = fmt.Fprintf(w, "%v", listOfStarsGo)
 }
 
 func starslistCsvHandler(w http.ResponseWriter, r *http.Request) {
-	backend.GetListOfStarsCsv(db)
+	requestInfo(r)
+
+	listOfStarsCsv := backend.GetListOfStarsCsv(db)
+	for _, element := range listOfStarsCsv {
+		_, _ = fmt.Fprintf(w, "%s\n", element)
+	}
 }
 
 func updateTotalMassHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
+
 	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)
 	backend.UpdateTotalMass(db, index)
 }
 
 func updateCenterOfMassHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
+
 	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)
 	backend.UpdateCenterOfMass(db, index)
 }
 
 func genForestTreeHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
+
+	parseFormErr := r.ParseForm()
+	errHandler("genForestTree Parse Form", parseFormErr)
+
+	// get the index of the tree that should be shown
 	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)
-	backend.GenForestTree(db, index)
+	log.Printf("Generating the forest representation of the tree with the index %d", index)
+
+	// generate the forest representation and write it back to the user
+	forestTree := backend.GenForestTree(db, index)
+	_, _ = fmt.Fprintf(w, "%s\n", forestTree)
 }
 
 func createNodesTableHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
+
 	backend.InitNodesTable(db)
 }
 
 func createStarsTableHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
+
 	backend.InitStarsTable(db)
 }
 
+func insertStarHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
+
+	// parse the http post form
+	err := r.ParseForm()
+	if err != nil {
+		panic(err)
+	}
+
+	// parse the star parameters
+	x, xParseErr := strconv.ParseFloat(r.PostFormValue("x"), 64)
+	errHandler("parse x", xParseErr)
+	y, yParseErr := strconv.ParseFloat(r.PostFormValue("y"), 64)
+	errHandler("parse y", yParseErr)
+	vx, vxParseErr := strconv.ParseFloat(r.PostFormValue("vx"), 64)
+	errHandler("parse vx", vxParseErr)
+	vy, vyParseErr := strconv.ParseFloat(r.PostFormValue("vy"), 64)
+	errHandler("parse vy", vyParseErr)
+	m, mParseErr := strconv.ParseFloat(r.PostFormValue("m"), 64)
+	errHandler("parse m", mParseErr)
+
+	// build the star
+	var star = structs.Star2D{
+		C: structs.Vec2{
+			X: x,
+			Y: y,
+		},
+		V: structs.Vec2{
+			X: vx,
+			Y: vy,
+		},
+		M: m,
+	}
+
+	// parse the tree index
+	index, indexParseErr := strconv.ParseInt(r.Form.Get("index"), 10, 64)
+	errHandler("parse index", indexParseErr)
+
+	// Insert the star into the tree
+	backend.InsertStar(db, star, index)
+}
+
+func insertStarListHandler(w http.ResponseWriter, r *http.Request) {
+	requestInfo(r)
+
+	// parse the http post form
+	err := r.ParseForm()
+	if err != nil {
+		panic(err)
+	}
+
+	// get the filename of the file to insert
+	filename := r.PostFormValue("filename")
+
+	// open the csv file and parse it
+	csvFile, _ := os.Open(fmt.Sprintf("%s.csv", filename))
+	reader := csv.NewReader(bufio.NewReader(csvFile))
+
+	// iterate over all the lines
+	for {
+
+		// read the line
+		line, error := reader.Read()
+
+		// handler errors such as broken syntax and the end of the file
+		if error == io.EOF {
+			break
+		} else if error != nil {
+			log.Fatal(error)
+		}
+
+		// parse the star parameters
+		x, xParseErr := strconv.ParseFloat(line[0], 64)
+		errHandler("parse x", xParseErr)
+		y, xParseErr := strconv.ParseFloat(line[1], 64)
+		errHandler("parse y", xParseErr)
+
+		// define a star
+		star := structs.Star2D{
+			C: structs.Vec2{
+				X: x,
+				Y: y,
+			},
+			V: structs.Vec2{
+				X: 0,
+				Y: 0,
+			},
+			M: 1000,
+		}
+
+		// insert the star into the database
+		backend.InsertStar(db, star, 1)
+	}
+}
+
 func main() {
 	// get the port on which the service should be hosted and the url of the database
 	var port string
 	flag.StringVar(&port, "port", "8080", "port used to host the service")
-	var dbURL string
-	flag.StringVar(&dbURL, "DBURL", "postgres", "url of the database used")
 	flag.Parse()
-
 	log.Println("[ ] Done loading the flags")
 
 	// get the data that should be used to connect to the database
+	var DBURL = os.Getenv("DBURL")
 	var DBUSER = os.Getenv("DBUSER")
 	var DBPASSWD = os.Getenv("DBPASSWD")
 	var DBPORT, _ = strconv.ParseInt(os.Getenv("DBPORT"), 10, 64)
 	var DBPROJECTNAME = os.Getenv("DBPROJECTNAME")
 
-	log.Println("[ ] Done loading the envs")
+	log.Printf("DBURL: %s", DBURL)
+	log.Printf("DBUSER: %s", DBUSER)
+	log.Printf("DBPASSWD: %s", DBPASSWD)
+	log.Printf("DBPORT: %d", DBPORT)
+	log.Printf("DBPROJECTNAME: %s", DBPROJECTNAME)
+	log.Printf("frontend port: %s", port)
 
 	// connect to the database
 	connStr := fmt.Sprintf("host=%s port=%d user=%s "+
 		"password=%s dbname=%s sslmode=disable",
-		dbURL, DBPORT, DBUSER, DBPASSWD, DBPROJECTNAME)
-	log.Printf("[ ] Done assembling the connString: %s", connStr)
+		DBURL, DBPORT, DBUSER, DBPASSWD, DBPROJECTNAME)
 
-	db, err := sql.Open("postgres", connStr)
+	var err error
+	db, err = sql.Open("postgres", connStr)
 	if err != nil {
-		_ = fmt.Errorf("%s", err)
+		log.Fatalf("Error: The data source arguments are not valid: %s", err)
 	}
-
 	log.Println("[ ] Done Connecting to the DB")
 
 	// ping the db
@@ -206,7 +352,9 @@ func main() {
 	router.HandleFunc("/genForestTree", genForestTreeHandler).Methods("GET")
 	router.HandleFunc("/createNodesTable", createNodesTableHandler).Methods("POST")
 	router.HandleFunc("/createStarsTable", createStarsTableHandler).Methods("POST")
+	router.HandleFunc("/insertStar", insertStarHandler).Methods("POST")
+	router.HandleFunc("/insertStarList", insertStarListHandler).Methods("POST")
 
-	// start the http server on the port reached in via a flag
+	log.Printf("[ ] Starting the API on localhost:%s", port)
 	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), router))
 }