From 6ae19dcc07980f4daaa337edd624d6f68ae3578b Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 21 Jan 2019 22:11:25 +0100 Subject: grouped the functions in individual files and created a detailed README.md --- import.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 import.go (limited to 'import.go') 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)) +} -- cgit 1.4.1