From da40f50361a117d762a432cf275ab6b11858034f Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 24 Dec 2018 22:51:18 +0100 Subject: added all files from the old dir to here --- main.go | 183 +++++++++++++++++++++++++++++++++++++++++++++++++-- tmpl/addstars.html | 84 +++++++++++++++++++++++ tmpl/header.html | 11 ++++ tmpl/index.html | 24 +++++++ tmpl/javascript.html | 5 ++ tmpl/nav.html | 20 ++++++ tmpl/newtree.html | 60 +++++++++++++++++ tmpl/overview.html | 49 ++++++++++++++ web/index.html | 41 ++++++++++++ 9 files changed, 473 insertions(+), 4 deletions(-) create mode 100644 tmpl/addstars.html create mode 100644 tmpl/header.html create mode 100644 tmpl/index.html create mode 100644 tmpl/javascript.html create mode 100644 tmpl/nav.html create mode 100644 tmpl/newtree.html create mode 100644 tmpl/overview.html create mode 100644 web/index.html diff --git a/main.go b/main.go index 4088fb1..fbf524a 100644 --- a/main.go +++ b/main.go @@ -2,19 +2,194 @@ package main import ( "fmt" + "html/template" "log" "net/http" + "net/url" + "strconv" "github.com/gorilla/mux" ) -func indexHandler(w http.ResponseWriter, r *http.Request) { - _, _ = fmt.Fprintln(w, "Hello World\nThis is the manager managing all the processes") +type Page struct { + NewTree bool + X, Y, W float64 +} + +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) + } +} + +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 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) + } + + 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"], + } + + 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 drawtreeHandler(w http.ResponseWriter, r *http.Request) { + } func main() { router := mux.NewRouter() - router.HandleFunc("/", indexHandler).Methods("GET") - log.Fatal(http.ListenAndServe(":80", router)) + 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)) } diff --git a/tmpl/addstars.html b/tmpl/addstars.html new file mode 100644 index 0000000..cffc31e --- /dev/null +++ b/tmpl/addstars.html @@ -0,0 +1,84 @@ +{{define "addstars"}} + + {{template "header"}} + + + + {{template "nav"}} + +
+ +
+ +
+
+

Add Stars

+

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!

+
+ +
+
+ +
+
+ +
+
+ +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ +
+
+
+ +
+
+

Optionally: select a .csv:

+
+
+ Upload +
+
+ + +
+
+
+
+ +
+
+
+ + + +
+
+
+ + {{template "javascript"}} + +{{end}} diff --git a/tmpl/header.html b/tmpl/header.html new file mode 100644 index 0000000..57e749e --- /dev/null +++ b/tmpl/header.html @@ -0,0 +1,11 @@ +{{define "header"}} + + + emile.space + + +{{end}} diff --git a/tmpl/index.html b/tmpl/index.html new file mode 100644 index 0000000..1f22908 --- /dev/null +++ b/tmpl/index.html @@ -0,0 +1,24 @@ +{{define "index"}} + + {{template "header"}} + + + + {{template "nav"}} + +
+ +
+ + {{template "javascript"}} + +{{end}} diff --git a/tmpl/javascript.html b/tmpl/javascript.html new file mode 100644 index 0000000..54b2810 --- /dev/null +++ b/tmpl/javascript.html @@ -0,0 +1,5 @@ +{{define "javascript"}} + + + +{{end}} diff --git a/tmpl/nav.html b/tmpl/nav.html new file mode 100644 index 0000000..9c7a94b --- /dev/null +++ b/tmpl/nav.html @@ -0,0 +1,20 @@ +{{define "nav"}} +
+ +
+{{end}} diff --git a/tmpl/newtree.html b/tmpl/newtree.html new file mode 100644 index 0000000..974e45c --- /dev/null +++ b/tmpl/newtree.html @@ -0,0 +1,60 @@ +{{define "newtree"}} + + {{template "header"}} + + + + {{template "nav"}} + +
+ +
+ +
+
+

New Tree

+

This is the "Create your own tree" page! Fill out the form below to create a new (quad)tree storing a complete galaxy!

+
+
+
+ + +
+
+ + +
+
+ + + +
+
+ + +
+ +
+
+ {{if .NewTree}} +
+ Successfully created the tree! (x:{{.X}}, y:{{.Y}}), width: {{.W}} + +
+ {{end}} +
+ + {{template "javascript"}} + +{{end}} diff --git a/tmpl/overview.html b/tmpl/overview.html new file mode 100644 index 0000000..7f20c2c --- /dev/null +++ b/tmpl/overview.html @@ -0,0 +1,49 @@ +{{define "overview"}} + + {{template "header"}} + + + {{template "nav"}} +
+
+

Existing galaxies

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IndexBoundaryAmounf of starsRange
1
2
3
+
+
+ + {{template "javascript"}} + +{{end}} \ No newline at end of file diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..51627b0 --- /dev/null +++ b/web/index.html @@ -0,0 +1,41 @@ + + + + + + + + + + + Hello, world! + + + + + + + + + + + + + + \ No newline at end of file -- cgit 1.4.1