From c4c15a00a7b50d18a113fb68bb1fe209fa544afc Mon Sep 17 00:00:00 2001 From: hanemile Date: Wed, 9 Jan 2019 21:26:24 +0100 Subject: overall push --- docker-compose.yml | 2 -- main.go | 54 ++++++++++++++++++++++++++++++++++++++++------- shell/structs/boundary.go | 36 +++++++++++++++++++++++++++++++ shell/structs/env.go | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 shell/structs/boundary.go create mode 100644 shell/structs/env.go diff --git a/docker-compose.yml b/docker-compose.yml index 06f2545..a268556 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,5 +3,3 @@ version: '3' services: db: build: . - ports: - - "8015:80" diff --git a/main.go b/main.go index 04368e7..2fecb57 100644 --- a/main.go +++ b/main.go @@ -100,7 +100,7 @@ func newTreeHandler(w http.ResponseWriter, r *http.Request) { log.Printf("The newTree endpoint was accessed.\n") } -// printAllHandler prints all the trees in the treeArray +// printAllHandler prints all the trees in the treeArray router.HandleFunc("/printall", printAllHandler).Methods("GET") func printAllHandler(w http.ResponseWriter, r *http.Request) { // set the content type to json (looks fancy in firefox :D) w.Header().Set("Content-Type", "application/json") @@ -119,6 +119,24 @@ func printAllHandler(w http.ResponseWriter, r *http.Request) { // set the conten log.Printf("The printAll endpoint was accessed.\n") } +func generatePrintTree(quadtree structs.Quadtree) string { + returnString := "[" + fmt.Printf("[") + for i := 0; i < 4; i++ { + if quadtree.Quadrants[i] != nil { + returnString += generatePrintTree(*quadtree.Quadrants[i]) + } + } + returnString += "]" + fmt.Printf("]") + return returnString +} + +func printTreeHandler(w http.ResponseWriter, r *http.Request) { + returnString := generatePrintTree(treeArray[0]) + _, _ = fmt.Fprintln(w, returnString) +} + // this insert handler inserts a given star using http queries func insertHandler(w http.ResponseWriter, r *http.Request) { // get the tree id in which the star should be inserted @@ -132,15 +150,14 @@ func insertHandler(w http.ResponseWriter, r *http.Request) { panic(errParseForm) } - // parse the values from the post parameters + // parse the values from the post parameters router.HandleFunc("/printall", printAllHandler).Methods("GET") x, _ := strconv.ParseFloat(r.Form.Get("x"), 64) y, _ := strconv.ParseFloat(r.Form.Get("y"), 64) vx, _ := strconv.ParseFloat(r.Form.Get("vx"), 64) vy, _ := strconv.ParseFloat(r.Form.Get("vy"), 64) m, _ := strconv.ParseFloat(r.Form.Get("m"), 64) - log.Println("------------------------------------------------------------------------------------------------") - log.Printf("[---] Inserting x: %f\ty: %f", x, y) + log.Printf("[---] Inserting star into the tree") // build the star that should be inserted newStar := structs.Star2D{ @@ -155,7 +172,7 @@ func insertHandler(w http.ResponseWriter, r *http.Request) { M: m, } - treeArray[treeindex].Insert(newStar) + treeArray[treeindex].NewInsert(newStar) } // Simple index Handler @@ -167,7 +184,8 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { var _, _ = fmt.Fprintln(w, "Insert a star using > $ curl --data \"x=250&y=250&vx=0.1&vy=0.2&m=3\" http://localhost:8123/insert/0") } -func drawTreeHandler(w http.ResponseWriter, r *http.Request) { +// drawGalaxyHandler draws the galaxy and returns an image of it +func drawGalaxyHandler(w http.ResponseWriter, r *http.Request) { log.Println("The drawTreeHandler was accessed.") vars := mux.Vars(r) @@ -176,12 +194,29 @@ func drawTreeHandler(w http.ResponseWriter, r *http.Request) { if treeArray[treeindex] != (structs.Quadtree{}) { log.Println(treeArray[treeindex]) - treeArray[treeindex].Draw("/public/quadtree.png") + treeArray[treeindex].DrawGalaxy("/public/quadtree.png") } http.ServeFile(w, r, "/public/quadtree.png") } +// drawTreeHandler draws a tree of the galaxy and returns an image of it +func drawTreeHandler(w http.ResponseWriter, r *http.Request) { + log.Println("The drawTreeHandler was accessed.") + + vars := mux.Vars(r) + treeindex, _ := strconv.ParseInt(vars["treeindex"], 10, 0) + log.Println(treeindex) + + if treeArray[treeindex] != (structs.Quadtree{}) { + log.Println(treeArray[treeindex]) + latex := treeArray[treeindex].DrawTree() + _, _ = fmt.Fprintf(w, "%s", latex) + } else { + _, _ = fmt.Fprintln(w, "error") + } +} + func main() { router := mux.NewRouter() @@ -190,7 +225,10 @@ func main() { router.HandleFunc("/new", newTreeHandler).Methods("POST") router.HandleFunc("/insert/{treeindex}", insertHandler).Methods("POST") router.HandleFunc("/printall", printAllHandler).Methods("GET") + router.HandleFunc("/printtree", printTreeHandler).Methods("GET") + router.HandleFunc("/drawgalaxy/{treeindex}", drawGalaxyHandler).Methods("GET") router.HandleFunc("/drawtree/{treeindex}", drawTreeHandler).Methods("GET") - log.Fatal(http.ListenAndServe(":8015", router)) + log.Println("Serving the database on port 8092 (This is for local testing only, remove when done!)") + log.Fatal(http.ListenAndServe(":8042", router)) } diff --git a/shell/structs/boundary.go b/shell/structs/boundary.go new file mode 100644 index 0000000..b49c7a2 --- /dev/null +++ b/shell/structs/boundary.go @@ -0,0 +1,36 @@ +package structs + +type Boundary struct { + // boundary box values + x int64 + y int64 + width int64 +} + +func NewBoundary(x int64, y int64, width int64) *Boundary { + return &Boundary{x: x, y: y, width: width} +} + +func (b *Boundary) Width() int64 { + return b.width +} + +func (b *Boundary) SetWidth(width int64) { + b.width = width +} + +func (b *Boundary) Y() int64 { + return b.y +} + +func (b *Boundary) SetY(y int64) { + b.y = y +} + +func (b *Boundary) X() int64 { + return b.x +} + +func (b *Boundary) SetX(x int64) { + b.x = x +} diff --git a/shell/structs/env.go b/shell/structs/env.go new file mode 100644 index 0000000..949460f --- /dev/null +++ b/shell/structs/env.go @@ -0,0 +1,43 @@ +package structs + +// Env structure storing the variables used to define what stars are inserted where +type Env struct { + // general values + url string + data string + amount int64 + treeindex int64 +} + +func NewEnv(url string, data string, amount int64, treeindex int64) *Env { + return &Env{url: url, data: data, amount: amount, treeindex: treeindex} +} + +// Getters and setters +func (e *Env) Treeindex() int64 { + return e.treeindex +} +func (e *Env) SetTreeindex(treeindex int64) { + e.treeindex = treeindex +} + +func (e *Env) Amount() int64 { + return e.amount +} +func (e *Env) SetAmount(amount int64) { + e.amount = amount +} + +func (e *Env) Data() string { + return e.data +} +func (e *Env) SetData(data string) { + e.data = data +} + +func (e *Env) Url() string { + return e.url +} +func (e *Env) SetUrl(url string) { + e.url = url +} -- cgit 1.4.1