about summary refs log tree commit diff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go252
1 files changed, 86 insertions, 166 deletions
diff --git a/main.go b/main.go
index fbf524a..4fe5ae2 100644
--- a/main.go
+++ b/main.go
@@ -1,195 +1,115 @@
 package main
 
 import (
+	"encoding/json"
 	"fmt"
-	"html/template"
+	"io/ioutil"
 	"log"
 	"net/http"
 	"net/url"
 	"strconv"
 
 	"github.com/gorilla/mux"
+
+	"git.darknebu.la/GalaxySimulator/structs"
 )
 
-type Page struct {
-	NewTree bool
-	X, Y, W float64
-}
+func indexHandler(w http.ResponseWriter, r *http.Request) {
+	infostring := `Galaxy Simulator Manager 
 
-func overviewHandler(w http.ResponseWriter, r *http.Request) {
-	t := template.Must(template.ParseFiles(
-		"tmpl/overview.html",
-		"tmpl/header.html",
-		"tmpl/nav.html",
-		"tmpl/javascript.html",
-	))
-	templateExecuteError := t.ExecuteTemplate(w, "overview", &Page{})
-	if templateExecuteError != nil {
-		log.Println(templateExecuteError)
-	}
+API: 
+	/calcallforces/{treeindex}`
+	_, _ = fmt.Fprintf(w, infostring)
 }
 
-func newtreeHandler(w http.ResponseWriter, r *http.Request) {
-	log.Println("the new tree handler was accessed")
-	// if the method used to get the page is GET, simply respond with the tree-creation form
-	if r.Method == "GET" {
-		log.Println("using a GET method")
-		t := template.Must(template.ParseFiles(
-			"tmpl/newtree.html",
-			"tmpl/header.html",
-			"tmpl/nav.html",
-			"tmpl/javascript.html",
-		))
-		templateExecuteError := t.ExecuteTemplate(w, "newtree", &Page{})
-		if templateExecuteError != nil {
-			log.Println(templateExecuteError)
-		}
-	} else {
-		log.Println("using a POST method")
-
-		// get the values from the form
-		err := r.ParseForm()
-		log.Println(err)
-		log.Println("parsed the form")
-
-		x, _ := strconv.ParseFloat(r.Form["x"][0], 64)     // x position of the midpoint of the tree
-		y, _ := strconv.ParseFloat(r.Form["y"][0], 64)     // y position of the midpoint of the tree
-		width, _ := strconv.ParseFloat(r.Form["w"][0], 64) // width the the tree
-		dbip := r.Form["db-ip"][0]                         // ip address of the database
-
-		log.Println("parsed the form arguments")
-
-		// parse the template files
-		t := template.Must(template.ParseFiles(
-			"tmpl/newtree.html",
-			"tmpl/header.html",
-			"tmpl/nav.html",
-			"tmpl/javascript.html",
-		))
-		log.Println("parsed the template file")
-
-		// execute the template
-		templateExecuteError := t.ExecuteTemplate(w, "newtree", &Page{NewTree: true, X: x, Y: y, W: width})
-
-		log.Println("executed the template")
-
-		// handle potential errors
-		if templateExecuteError != nil {
-			log.Println(templateExecuteError)
-		}
-
-		log.Println("handled potential errors")
-
-		// create the new tree by accessing the api endpoint of the database
-		postUrl := fmt.Sprintf("http://%s/new", dbip)
-
-		log.Printf("the postURL: %s", postUrl)
-		log.Println("created the url for the post request")
-
-		// define the data that should be posted
-		formData := url.Values{
-			"x": r.Form["x"],
-			"y": r.Form["y"],
-			"w": r.Form["w"],
-		}
-
-		log.Println("created the form to be sent in the post request")
-
-		// make the http Post request
-		resp, err := http.PostForm(postUrl, formData)
-
-		log.Println("sent the post request")
-
-		// some debug messages
-		log.Printf("[   ] POST response: %v", resp)
-		log.Printf("[   ] POST err: %v", err)
+func calcallforcesHandler(w http.ResponseWriter, r *http.Request) {
+	// get the tree index
+	vars := mux.Vars(r)
+	treeindex, _ := strconv.ParseInt(vars["treeindex"], 10, 0)
+
+	_, _ = fmt.Fprintf(w, "Got the treeindex, it's %d\n", treeindex)
+	fmt.Printf("treeindex: %d\n", treeindex)
+
+	// ################################################################################
+	// Calculate the center of mass and total mass of each tree
+	// ################################################################################
+
+	// db.updatestufff()
+
+	// ################################################################################
+	// Get a list of all stars in the tree
+	// ################################################################################
+	// make the following post request:
+	// db.docker.localhost/getallstars/{treeindex}
+	requesturl := fmt.Sprintf("http://db.docker.localhost/starlist/%d", treeindex)
+	_, _ = fmt.Fprintf(w, "Got the requesturl, it's %s\n", requesturl)
+	fmt.Printf("requesturl: %s\n", requesturl)
+	resp, err := http.Get(requesturl)
+	fmt.Println("After http GET, but before the error handling")
+	if err != nil {
+		fmt.Println("PANIC")
+		panic(err)
+	}
+	fmt.Println("After http GET and after error handling")
+	defer resp.Body.Close()
+
+	// read the response containing a list of all stars in json format
+	body, err := ioutil.ReadAll(resp.Body)
+	_, _ = fmt.Fprintf(w, "Got the body, it's %v\n", string(body))
+	fmt.Printf("body: %v\n", string(body))
+
+	// unmarshal the json
+	listofstars := &[]structs.Star2D{}
+	jsonUnmarschalErr := json.Unmarshal(body, listofstars)
+	if jsonUnmarschalErr != nil {
+		panic(jsonUnmarschalErr)
 	}
-}
-
-func addstarsHandler(w http.ResponseWriter, r *http.Request) {
-	// if the method used to get the page is GET, simply respond with the add-stars form
-	if r.Method == "GET" {
-		log.Println("/addstars was accessed using a GET method")
-		t := template.Must(template.ParseFiles(
-			"tmpl/addstars.html",
-			"tmpl/header.html",
-			"tmpl/nav.html",
-			"tmpl/javascript.html",
-		))
-		templateExecuteError := t.ExecuteTemplate(w, "addstars", &Page{})
-		if templateExecuteError != nil {
-			log.Println(templateExecuteError)
-		}
-	} else {
-		log.Println("/addstars was accessed using a POST method")
-
-		// get the values from the form
-		_ = r.ParseForm()
-
-		log.Println("parsed the form")
-
-		// extract some information from the form
-		dbip := r.Form["dbip"][0]                                   // ip address of the database
-		treeindex, _ := strconv.ParseInt(r.Form["tree"][0], 10, 64) // index of the tree
-		log.Println(dbip)
-		log.Println(treeindex)
-
-		log.Println("parsed the form arguments")
-
-		t := template.Must(template.ParseFiles(
-			"tmpl/addstars.html",
-			"tmpl/header.html",
-			"tmpl/nav.html",
-			"tmpl/javascript.html",
-		))
-
-		log.Println("parsed the template file")
 
-		templateExecuteError := t.ExecuteTemplate(w, "addstars", &Page{})
-		if templateExecuteError != nil {
-			log.Println(templateExecuteError)
+	fmt.Printf("listofstars: %v\n", listofstars)
+	_, _ = fmt.Fprintf(w, "Got the listofstars, it's %v\n", listofstars)
+	fmt.Printf("listofstars: %v\n", listofstars)
+
+	// ################################################################################
+	// Send the star to the simulator in order to simulate it's new position
+	// ################################################################################
+
+	// make a post request to the simulator with every star
+	for _, star := range *listofstars {
+		fmt.Printf("star: %v", star)
+		_, _ = fmt.Fprintf(w, "One of the stars is %v\n", star)
+
+		requesturlSimu := fmt.Sprintf("http://simulator.docker.localhost/calcallforces/%d", treeindex)
+
+		response, err := http.PostForm(requesturlSimu, url.Values{
+			"x":  {fmt.Sprintf("%f", star.C.X)},
+			"y":  {fmt.Sprintf("%f", star.C.Y)},
+			"vx": {fmt.Sprintf("%f", star.V.X)},
+			"vy": {fmt.Sprintf("%f", star.V.Y)},
+			"m":  {fmt.Sprintf("%f", star.M)},
+		})
+		if err != nil {
+			panic(err)
 		}
 
-		log.Println("executed the template")
-
-		// create the new tree by accessing the api endpoint of the database
-		postUrl := fmt.Sprintf("http://%s/insert/%d", dbip, treeindex)
-
-		log.Printf("the postURL: %s", postUrl)
-		log.Println("created the url for the post request")
-
-		// define the data that should be posted
-		formData := url.Values{
-			"x":  r.Form["x"],
-			"y":  r.Form["y"],
-			"vx": r.Form["vx"],
-			"vy": r.Form["vy"],
-			"m":  r.Form["mass"],
+		respBody, readAllErr := ioutil.ReadAll(response.Body)
+		if readAllErr != nil {
+			panic(readAllErr)
 		}
-
-		log.Println("created the form to be sent in the post request")
-
-		// make the http Post request
-		resp, err := http.PostForm(postUrl, formData)
-
-		log.Println("sent the post request")
-
-		// some debug messages
-		log.Printf("[   ] POST response: %v", resp)
-		log.Printf("[   ] POST err: %v", err)
+		_, _ = fmt.Fprintf(w, "%s\n", string(respBody))
 	}
+	_, _ = fmt.Fprintf(w, "DONE!\n")
+	fmt.Printf("DONE!\n")
 }
 
-func drawtreeHandler(w http.ResponseWriter, r *http.Request) {
-
+func printhiHandler(w http.ResponseWriter, r *http.Request) {
+	_, _ = fmt.Fprintf(w, "Hi there!")
 }
 
 func main() {
 	router := mux.NewRouter()
 
-	router.HandleFunc("/", overviewHandler).Methods("GET")
-	router.HandleFunc("/newtree", newtreeHandler).Methods("GET", "POST")
-	router.HandleFunc("/addstars", addstarsHandler).Methods("GET", "POST")
-	router.HandleFunc("/drawtree", drawtreeHandler).Methods("GET")
-	log.Fatal(http.ListenAndServe(":8429", router))
+	router.HandleFunc("/", indexHandler).Methods("GET")
+	router.HandleFunc("/calcallforces/{treeindex}", calcallforcesHandler).Methods("GET")
+	router.HandleFunc("/printhi", printhiHandler).Methods("GET")
+	log.Fatal(http.ListenAndServe(":8031", router))
 }