about summary refs log tree commit diff
path: root/main.go
blob: 3c0a34557aba94657d9ed9de3bf3861e72aeb384 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// main.go purpose is to build the interaction layer in between the http endpoints and the http server
// Copyright (C) 2019 Emile Hansmaennel
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"fmt"
	"git.darknebu.la/GalaxySimulator/structs"
	"log"
	"net/http"
	"strconv"

	"github.com/gorilla/mux"
	//"git.darknebu.la/GalaxySimulator/structs"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("[ ] The indexHandler was accessed")
	_, _ = fmt.Fprintf(w, indexEndpoint())
}

func newTreeHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("[ ] The newTreeHandler was accessed")

	// get the width of the most outer box of the tree by parsing http-post parameters
	errParseForm := r.ParseForm() // parse the POST form
	if errParseForm != nil {      // handle errors
		panic(errParseForm)
	}

	// parse the width
	width, _ := strconv.ParseFloat(r.Form.Get("w"), 64)

	// create a new tree in the database width the given width
	newTreeEndpoint(width)

	_, _ = fmt.Fprintf(w, "OK\n")
}

func insertStarHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("[ ] The insertStarHandler was accessed")

	// get the star by parsing http-post parameters
	errParseForm := r.ParseForm() // parse the POST form
	if errParseForm != nil {      // handle errors
		panic(errParseForm)
	}

	// get the star coordinates
	x, _ := strconv.ParseFloat(r.Form.Get("x"), 64)
	y, _ := strconv.ParseFloat(r.Form.Get("y"), 64)
	vx, _ := strconv.ParseFloat(r.Form.Get("vx"), 64)
	vy, _ := strconv.ParseFloat(r.Form.Get("vy"), 64)
	m, _ := strconv.ParseFloat(r.Form.Get("m"), 64)

	// get the tree into which the star should be inserted into
	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)

	// build a star
	star := structs.Star2D{
		C: structs.Vec2{
			X: x,
			Y: y,
		},
		V: structs.Vec2{
			X: vx,
			Y: vy,
		},
		M: m,
	}

	insertStarEndpoint(star, index)
}

func deleteStarsHandler(w http.ResponseWriter, r *http.Request) {
	deleteStarsEndpoint()
}

func deleteNodesHandler(w http.ResponseWriter, r *http.Request) {
	deleteNodesEndpoint()
}

func main() {
	router := mux.NewRouter()

	router.HandleFunc("/", indexHandler).Methods("GET")
	router.HandleFunc("/new", newTreeHandler).Methods("POST")
	router.HandleFunc("/insertStar", insertStarHandler).Methods("POST")
	router.HandleFunc("/deleteStars", deleteStarsHandler).Methods("POST")
	router.HandleFunc("/deleteNodes", deleteNodesHandler).Methods("POST")

	//router.HandleFunc("/printall", printAllHandler).Methods("GET")
	//router.HandleFunc("/insert/{treeindex}", insertStarHandler).Methods("POST")
	//router.HandleFunc("/starlist/{treeindex}", starlistHandler).Methods("GET")
	//router.HandleFunc("/dumptree/{treeindex}", dumptreeHandler).Methods("GET")
	//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")
	//router.HandleFunc("/nrofgalaxies", nrofgalaxiesHandler).Methods("GET")
	//
	//router.HandleFunc("/fastinsertjson/{filename}", fastInsertJSONHandler).Methods("GET")
	//router.HandleFunc("/fastinsertlist/{filename}/{treeindex}", fastInsertListHandler).Methods("GET")
	//
	//router.HandleFunc("/readdir/{dirname}", readdirHandler).Methods("GET")

	fmt.Println("Database Container up on port 8081")
	log.Fatal(http.ListenAndServe(":8081", router))
}