about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhanemile <hanemile@protonmail.com>2019-01-21 21:06:53 +0100
committerhanemile <hanemile@protonmail.com>2019-01-21 21:06:53 +0100
commit9c3837c139dc73f7193768cc5c55bb2835e84a85 (patch)
tree2927606d8f797473d0f4c2d51012311136f7c9c6
parent5612307880256486dae143f5c2af44ec0a535c1f (diff)
.
-rw-r--r--Dockerfile1
-rw-r--r--main.go30
2 files changed, 31 insertions, 0 deletions
diff --git a/Dockerfile b/Dockerfile
index 2396314..95a29eb 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -4,6 +4,7 @@ WORKDIR /home
 
 COPY main.go /home/main.go
 
+RUN ["mkdir", "/exports"]
 RUN ["go", "get", "github.com/gorilla/mux"]
 RUN ["go", "get", "git.darknebu.la/GalaxySimulator/structs"]
 
diff --git a/main.go b/main.go
index 098eeb8..b7e246c 100644
--- a/main.go
+++ b/main.go
@@ -3,6 +3,7 @@ package main
 import (
 	"encoding/json"
 	"fmt"
+	"io/ioutil"
 	"log"
 	"net/http"
 	"net/url"
@@ -252,6 +253,34 @@ func metricHandler(w http.ResponseWriter, r *http.Request) {
 	_, _ = fmt.Fprintf(w, metricsString)
 }
 
+// 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
+}
+
+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)
+}
+
 func main() {
 	router := mux.NewRouter()
 
@@ -264,6 +293,7 @@ func main() {
 	router.HandleFunc("/updatetotalmass/{treeindex}", updateTotalMassHandler).Methods("GET")
 	router.HandleFunc("/updatecenterofmass/{treeindex}", updateCenterOfMassHandler).Methods("GET")
 	router.HandleFunc("/metrics", metricHandler).Methods("GET")
+	router.HandleFunc("/export/{treeindex}", exportHandler).Methods("POST")
 
 	fmt.Println("Database Container up")
 	log.Fatal(http.ListenAndServe(":80", router))