about summary refs log tree commit diff
path: root/export.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 /export.go
parent9c3837c139dc73f7193768cc5c55bb2835e84a85 (diff)
grouped the functions in individual files and created a detailed README.md
Diffstat (limited to 'export.go')
-rw-r--r--export.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/export.go b/export.go
new file mode 100644
index 0000000..9cc67af
--- /dev/null
+++ b/export.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"strconv"
+
+	"github.com/gorilla/mux"
+)
+
+// export exports all the trees
+func export(treeindex int64) error {
+	// Convert the data to json
+	jsonData, jsonMarshalerError := json.Marshal(treeArray[treeindex])
+	if jsonMarshalerError != nil {
+		panic(jsonMarshalerError)
+	}
+
+	// write the json formatted byte data to a file
+	err := ioutil.WriteFile(fmt.Sprintf("/exports/tree_%d.json", treeindex), jsonData, 0644)
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+// export the selected tree to the selected file
+func exportHandler(w http.ResponseWriter, r *http.Request) {
+	vars := mux.Vars(r)
+	treeindex, _ := strconv.ParseInt(vars["treeindex"], 10, 0)
+
+	err := export(treeindex)
+	if err != nil {
+		panic(err)
+	}
+
+	_, _ = fmt.Fprintf(w, "Exportet Tree %d", treeindex)
+}