about summary refs log tree commit diff
path: root/import.go
diff options
context:
space:
mode:
authorhanemile <hanemile@protonmail.com>2019-01-21 22:11:25 +0100
committerhanemile <hanemile@protonmail.com>2019-01-21 22:11:25 +0100
commit6ae19dcc07980f4daaa337edd624d6f68ae3578b (patch)
treeb5c4e2f057e1abaff521d8b6d541cdc80eb728d9 /import.go
parent9c3837c139dc73f7193768cc5c55bb2835e84a85 (diff)
grouped the functions in individual files and created a detailed README.md
Diffstat (limited to 'import.go')
-rw-r--r--import.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/import.go b/import.go
new file mode 100644
index 0000000..c52090f
--- /dev/null
+++ b/import.go
@@ -0,0 +1,38 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+
+	"github.com/gorilla/mux"
+
+	"git.darknebu.la/GalaxySimulator/structs"
+)
+
+// fastInsertHandler gets a tree index and a filename and tries to read
+func fastInsertHandler(w http.ResponseWriter, r *http.Request) {
+	// read the mux variables
+	vars := mux.Vars(r)
+	filename, _ := vars["filename"]
+
+	// read the content using the given filename
+	content, readErr := ioutil.ReadFile(filename)
+	if readErr != nil {
+		panic(readErr)
+	}
+
+	// unmarshal the file content
+	tree := &structs.Node{}
+	jsonUnmarshalErr := json.Unmarshal(content, tree)
+	if jsonUnmarshalErr != nil {
+		panic(jsonUnmarshalErr)
+	}
+
+	// append the tree to the treeArray
+	treeArray = append(treeArray, tree)
+
+	// return the treeArray index the tree was inserted into (== the length of the array)
+	_, _ = fmt.Fprintln(w, len(treeArray))
+}