about summary refs log tree commit diff
path: root/main.go
blob: e021c12ba147d3081109aae20d105452426efc0a (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// 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 (
	"database/sql"
	"fmt"
	"git.darknebu.la/GalaxySimulator/db_actions"
	"git.darknebu.la/GalaxySimulator/structs"
	"log"
	"net/http"
	"strconv"

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

var (
	db *sql.DB
)

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(db, 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(db, star, index)
}

func insertListHandler(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)
	}

	filename := r.Form.Get("filename")

	insertListEndpoint(db, filename)
}

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

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

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

	listOfStars := listOfStarsGoEndpoint(db)

	for _, star := range listOfStars {
		_, _ = fmt.Fprintf(w, "%v\n", star)
	}
}

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

	listOfStars := listOfStarsCsvEndpoint(db)

	for _, star := range listOfStars {
		_, _ = fmt.Fprintf(w, "%v\n", star)
	}
}

func updateTotalMassHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("[ ] The updateTotalMassHandler 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 tree into which the star should be inserted into
	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)

	updateTotalMassEndpoint(db, index)
}

func updateCenterOfMassHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("[ ] The updateCenterOfMassHandler 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 tree into which the star should be inserted into
	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)

	updateCenterOfMassEndpoint(db, index)
}

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

	vars := mux.Vars(r)
	treeindex, parseIntErr := strconv.ParseInt(vars["treeindex"], 10, 64)
	if parseIntErr != nil {
		panic(parseIntErr)
	}

	tree := genForestTreeEndpoint(db, treeindex)
	_, _ = fmt.Fprintf(w, "%s", tree)
}

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

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

	router.HandleFunc("/", indexHandler).Methods("GET")
	router.HandleFunc("/new", newTreeHandler).Methods("POST")
	router.HandleFunc("/deleteStars", deleteStarsHandler).Methods("POST")
	router.HandleFunc("/deleteNodes", deleteNodesHandler).Methods("POST")
	router.HandleFunc("/starlist/go", getListOfStarsGoHandler).Methods("GET")
	router.HandleFunc("/starlist/csv", getListOfStarsCsvHandler).Methods("GET")

	router.HandleFunc("/insertStar", insertStarHandler).Methods("POST")
	router.HandleFunc("/insertList", insertListHandler).Methods("POST")

	router.HandleFunc("/updatetotalmass", updateTotalMassHandler).Methods("POST")
	router.HandleFunc("/updatecenterofmass", updateCenterOfMassHandler).Methods("POST")

	router.HandleFunc("/genforesttree/{treeindex}", genForestTreeHandler).Methods("GET")

	router.HandleFunc("/test", testCalcHandler).Methods("POST")

	//router.HandleFunc("/metrics", metricHandler).Methods("GET")
	//router.HandleFunc("/export/{treeindex}", exportHandler).Methods("POST")
	//router.HandleFunc("/nrofgalaxies", nrofgalaxiesHandler).Methods("GET")

	db = db_actions.ConnectToDB()
	db.SetMaxOpenConns(75)

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