diff options
-rw-r--r-- | docker-compose.yml | 2 | ||||
-rw-r--r-- | main.go | 252 | ||||
-rw-r--r-- | tmpl/addstars.html | 84 | ||||
-rw-r--r-- | tmpl/header.html | 11 | ||||
-rw-r--r-- | tmpl/index.html | 24 | ||||
-rw-r--r-- | tmpl/javascript.html | 5 | ||||
-rw-r--r-- | tmpl/nav.html | 20 | ||||
-rw-r--r-- | tmpl/newtree.html | 60 | ||||
-rw-r--r-- | tmpl/overview.html | 49 | ||||
-rw-r--r-- | web/index.html | 41 |
10 files changed, 87 insertions, 461 deletions
diff --git a/docker-compose.yml b/docker-compose.yml index b7a8e13..dddd22a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,4 +4,4 @@ services: manager: build: . ports: - - "80:80" \ No newline at end of file + - 8031:80 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)) } diff --git a/tmpl/addstars.html b/tmpl/addstars.html deleted file mode 100644 index cffc31e..0000000 --- a/tmpl/addstars.html +++ /dev/null @@ -1,84 +0,0 @@ -{{define "addstars"}} - <head> - {{template "header"}} - <style> - .jumbotron{ - margin: 10px; - } - </style> - </head> - <body> - {{template "nav"}} - - <div class="container"> - <div class="tab-content" id="nav-tabContent"> - <div class="tab-pane fade" id="nav-overview" role="tabpanel" aria-labelledby="nav-overview-tab">There will be an overview here!</div> - <div class="tab-pane fade" id="nav-new-tree" role="tabpanel" aria-labelledby="nav-new-tree-tab">add tree</div> - <div class="tab-pane fade" id="nav-add-stars" role="tabpanel" aria-labelledby="nav-add-stars-tab">Add some stars!</div> - <div class="tab-pane fade" id="nav-draw-tree" role="tabpanel" aria-labelledby="nav-draw-tree-tab">Draw one of the galaxies!</div> - </div> - </div> - - <div class="container"> - <div class="jumbotron"> - <h1 class="display-4">Add Stars</h1> - <p class="lead">This is the "Add your star to a tree" page! Fill out the form below for adding one of your stars into one of the trees!</p> - <form action="/addstars" method="post"> - - <hr class="my-4"> - <div class="form-group"> - <input type="text" class="form-control" id="dbip" placeholder="db-ip" name="dbip"> - </div> - <div class="form-group"> - <input type="text" class="form-control" id="tree" placeholder="tree index" name="tree"> - </div> - <hr class="my-4"> - - <div class="form-group"> - <input type="text" class="form-control" id="x" placeholder="x" name="x"> - </div> - <div class="form-group"> - <input type="text" class="form-control" id="y" placeholder="y" name="y"> - </div> - <div class="form-group"> - <input type="text" class="form-control" id="vx" placeholder="vx" name="vx"> - </div> - <div class="form-group"> - <input type="text" class="form-control" id="vy" placeholder="vy" name="vy"> - </div> - <div class="form-group"> - <input type="text" class="form-control" id="m" placeholder="mass" name="mass"> - </div> - <button type="submit" class="btn btn-primary">Submit</button> - </form> - </div> - </div> - - <div class="container"> - <div class="jumbotron"> - <p class="lead">Optionally: select a .csv:</p> - <div class="input-group"> - <div class="input-group-prepend"> - <span class="input-group-text" id="inputGroupFileAddon01">Upload</span> - </div> - <div class="custom-file"> - <input type="file" class="custom-file-input" id="inputGroupFile01" aria-describedby="inputGroupFileAddon01"> - <label class="custom-file-label" for="inputGroupFile01">Choose file</label> - </div> - </div> - </div> - </div> - - <div class="container"> - <div class="jumbotron"> - <form enctype="multipart/form-data" action="http://127.0.0.1:9090/upload" method="post"> - <input type="file" name="uploadfile" /> - <input type="hidden" name="token" value="{{.}}"/> - <input type="submit" value="upload" /> - </form> - </div> - </div> - - {{template "javascript"}} - </body> -{{end}} diff --git a/tmpl/header.html b/tmpl/header.html deleted file mode 100644 index 57e749e..0000000 --- a/tmpl/header.html +++ /dev/null @@ -1,11 +0,0 @@ -{{define "header"}} - <meta charset="utf-8"> - <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> - <title>emile.space</title> - <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> - <style> - .nav { - margin: 10px; - } - </style> -{{end}} diff --git a/tmpl/index.html b/tmpl/index.html deleted file mode 100644 index 1f22908..0000000 --- a/tmpl/index.html +++ /dev/null @@ -1,24 +0,0 @@ -{{define "index"}} - <head> - {{template "header"}} - <style> - .jumbotron{ - margin: 10px; - } - </style> - </head> - <body> - {{template "nav"}} - - <div class="container"> - <div class="tab-content" id="nav-tabContent"> - <div class="tab-pane fade" id="nav-overview" role="tabpanel" aria-labelledby="nav-overview-tab">There will be an overview here!</div> - <div class="tab-pane fade" id="nav-new-tree" role="tabpanel" aria-labelledby="nav-new-tree-tab">add tree</div> - <div class="tab-pane fade" id="nav-add-stars" role="tabpanel" aria-labelledby="nav-add-stars-tab">Add some stars!</div> - <div class="tab-pane fade" id="nav-draw-tree" role="tabpanel" aria-labelledby="nav-draw-tree-tab">Draw one of the galaxies!</div> - </div> - </div> - - {{template "javascript"}} - </body> -{{end}} diff --git a/tmpl/javascript.html b/tmpl/javascript.html deleted file mode 100644 index 54b2810..0000000 --- a/tmpl/javascript.html +++ /dev/null @@ -1,5 +0,0 @@ -{{define "javascript"}} -<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> -<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> -<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> -{{end}} diff --git a/tmpl/nav.html b/tmpl/nav.html deleted file mode 100644 index 9c7a94b..0000000 --- a/tmpl/nav.html +++ /dev/null @@ -1,20 +0,0 @@ -{{define "nav"}} -<div class="container"> - <nav> - <ul class="nav nav-tabs"> - <li class="nav-item"> - <a class="nav-link" href="/">Overview</a> - </li> - <li class="nav-item"> - <a class="nav-link" href="/newtree">New Tree</a> - </li> - <li class="nav-item"> - <a class="nav-link" href="/addstars">Add Stars</a> - </li> - <li class="nav-item"> - <a class="nav-link" href="/drawtree">Draw Tree</a> - </li> - </ul> - </nav> -</div> -{{end}} diff --git a/tmpl/newtree.html b/tmpl/newtree.html deleted file mode 100644 index 974e45c..0000000 --- a/tmpl/newtree.html +++ /dev/null @@ -1,60 +0,0 @@ -{{define "newtree"}} - <head> - {{template "header"}} - <style> - .jumbotron{ - margin: 10px; - } - </style> - </head> - <body> - {{template "nav"}} - - <div class="container"> - <div class="tab-content" id="nav-tabContent"> - <div class="tab-pane fade" id="nav-overview" role="tabpanel" aria-labelledby="nav-overview-tab">There will be an overview here!</div> - <div class="tab-pane fade" id="nav-new-tree" role="tabpanel" aria-labelledby="nav-new-tree-tab">add tree</div> - <div class="tab-pane fade" id="nav-add-stars" role="tabpanel" aria-labelledby="nav-add-stars-tab">Add some stars!</div> - <div class="tab-pane fade" id="nav-draw-tree" role="tabpanel" aria-labelledby="nav-draw-tree-tab">Draw one of the galaxies!</div> - </div> - </div> - - <div class="container"> - <div class="jumbotron"> - <h1 class="display-4">New Tree</h1> - <p class="lead">This is the "Create your own tree" page! Fill out the form below to create a new (quad)tree storing a complete galaxy!</p> - <hr class="my-4"> - <form action="/newtree" method="post"> - <div class="form-group"> - <label for="db-ip">The ip of the database</label> - <input type="text" class="form-control" id="x-pos" placeholder="db-ip" name="db-ip"> - </div> - <div class="form-group"> - <label for="x-pos">(Center) X Position</label> - <input type="text" class="form-control" id="x-pos" placeholder="x-pos" name="x"> - </div> - <div class="form-group"> - <label for="y-pos">(Center) Y Position</label> - - <input type="text" class="form-control" id="y-pos" placeholder="y-pos" name="y"> - </div> - <div class="form-group"> - <label for="width">Width</label> - <input type="text" class="form-control" id="width" placeholder="width" name="w"> - </div> - <button type="submit" class="btn btn-primary">Submit</button> - </form> - </div> - {{if .NewTree}} - <div class="alert alert-success alert-dismissible fade show" role="success"> - <strong>Successfully created the tree!</strong> (x:{{.X}}, y:{{.Y}}), width: {{.W}} - <button type="button" class="close" data-dismiss="alert" aria-label="Close"> - <span aria-hidden="true">×</span> - </button> - </div> - {{end}} - </div> - - {{template "javascript"}} - </body> -{{end}} diff --git a/tmpl/overview.html b/tmpl/overview.html deleted file mode 100644 index 7f20c2c..0000000 --- a/tmpl/overview.html +++ /dev/null @@ -1,49 +0,0 @@ -{{define "overview"}} - <head> - {{template "header"}} - </head> - <body> - {{template "nav"}} - <div id="wrap"> - <div class="container"> - <h3>Existing galaxies</h3> - <table cellpadding="0" cellspacing="0" border="0" class="datatable table table-striped table-bordered"> - <thead> - <tr> - <th>Index</th> - <th>Boundary</th> - <th>Amounf of stars</th> - <th>Range</th> - </tr> - </thead> - - <tbody> - <tr class="gradeX"> - <td>1</td> - <td></td> - <td></td> - <td></td> - </tr> - <tr class="gradeX"> - <td>2</td> - <td></td> - <td></td> - <td></td> - </tr> - <tr class="gradeX"> - <td>3</td> - <td></td> - <td></td> - <td></td> - </tr> - </tbody> - - <tfoot> - </tfoot> - </table> - </div> - </div> - - {{template "javascript"}} - </body> -{{end}} \ No newline at end of file diff --git a/web/index.html b/web/index.html deleted file mode 100644 index 51627b0..0000000 --- a/web/index.html +++ /dev/null @@ -1,41 +0,0 @@ -<!doctype html> -<html lang="en"> -<head> - <!-- Required meta tags --> - <meta charset="utf-8"> - <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> - - <!-- Bootstrap CSS --> - <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> - - <title>Hello, world!</title> - - <style> - .nav { - margin: 10px; - } - </style> -</head> -<body> - <nav> - <div class="nav nav-tabs" id="nav-tab" role="tablist"> - <a class="nav-item nav-link active" id="nav-overview-tab" data-toggle="tab" href="#nav-overview" role="tab" aria-controls="nav-overview" aria-selected="true">Overview</a> - <a class="nav-item nav-link" id="nav-new-tree-tab" data-toggle="tab" href="#nav-new-tree" role="tab" aria-controls="nav-new-tree" aria-selected="false">New Tree</a> - <a class="nav-item nav-link" id="nav-add-stars-tab" data-toggle="tab" href="#nav-add-stars" role="tab" aria-controls="nav-add-stars" aria-selected="false">Add Stars</a> - <a class="nav-item nav-link" id="nav-draw-tree-tab" data-toggle="tab" href="#nav-draw-tree" role="tab" aria-controls="nav-contact" aria-selected="false">Draw Tree</a> - </div> - </nav> - <div class="tab-content" id="nav-tabContent"> - <div class="tab-pane fade show active" id="nav-overview" role="tabpanel" aria-labelledby="nav-overview-tab">This is the main overview page!</div> - <div class="tab-pane fade" id="nav-new-tree" role="tabpanel" aria-labelledby="nav-new-tree-tab">Create a new tree!</div> - <div class="tab-pane fade" id="nav-add-stars" role="tabpanel" aria-labelledby="nav-add-stars-tab">Add some stars!</div> - <div class="tab-pane fade" id="nav-draw-tree" role="tabpanel" aria-labelledby="nav-draw-tree-tab">Draw one of the galaxies!</div> - </div> - - <!-- Optional JavaScript --> - <!-- jQuery first, then Popper.js, then Bootstrap JS --> - <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> - <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> - <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> -</body> -</html> \ No newline at end of file |