From 9c3837c139dc73f7193768cc5c55bb2835e84a85 Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 21 Jan 2019 21:06:53 +0100 Subject: . --- Dockerfile | 1 + main.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) 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)) -- cgit 1.4.1