about summary refs log tree commit diff
path: root/import.go
blob: c52090f60822372572d21d21206458672020804c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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))
}