about summary refs log tree commit diff
path: root/main.go
blob: c9736edf73c022abd1108c5f141e5376545d1876 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main

import (
	"bufio"
	"database/sql"
	"encoding/csv"
	"flag"
	"fmt"
	"git.darknebu.la/GalaxySimulator/structs"
	"github.com/gorilla/mux"
	"io"
	"log"
	"net/http"
	"os"
	"strconv"

	"git.darknebu.la/GalaxySimulator/db-controller/backend"

	_ "github.com/lib/pq"
)

var (
	db *sql.DB
)

func requestInfo(r *http.Request) {
	log.Printf("%s %s %s", r.Method, r.Host, r.URL.Path)
}

func errHandler(name string, err error) {
	if err != nil {
		log.Fatalf("Error: %s: %v", name, err)
	}
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)
	indexString := `<html><body><h1>Galaxy Simulator Database Frontend</h1>

		API:
	<h3> / (GET) </h3>

	<h3> /new (POST) </h3>
		Create a new Tree
	<br>
		Parameters:
	<ul>
	<li>
		w float64: width of the tree
	</li>
	</ul>

	<h3> /deleteStars (POST) </h3>
		Delete all stars from the stars Table
	<br>
		Parameters:
	<ul>
	<li>
		none
	</li>
	</ul>

	<h3> /deleteNodes (POST) </h3>
		Delete all nodes from the nodes Table
	<br>
		Parameters:
	<ul>
	<li>
		none
	</li>
	</ul>

	<h3> /starslist/go (GET) </h3>
		List all stars using go-array format
	<br>
		Parameters:
	<ul>
	<li>
		none
	</li>
	</ul>

	<h3> /starslist/csv (GET) </h3>
		List all stars as a csv
	<br>
		Parameters:
	<ul>
	<li>
		none
	</li>
	</ul>

	<h3> /updatetotalmass (POST) </h3>
		Update the total mass of all the nodes in the tree with the given index
	<br>
		Parameters:
	<ul>
	<li>
		index int: index of the tree
	</li>
	</ul>

	<h3> /updatecenterofmass (POST) </h3>
		Update the center of mass of all the nodes in the tree with the given index
	<br>
		Parameters:
	<ul>
	<li>
		index int: index of the tree
	</li>
	</ul>

	<h3> /genforesttree (GET) </h3>
		Generate the forest representation of the tree with the given index
	<br>
		Parameters:
	<ul>
	<li>
		index int: index of the tree
	</li>
	</ul>

	</body>
	</html>
`
	_, _ = fmt.Fprintf(w, "%s", indexString)
}
func newTreeHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)

	width, _ := strconv.ParseFloat(r.Form.Get("w"), 64)
	backend.NewTree(db, width)
}

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

	backend.DeleteAllNodes(db)
}

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

	backend.DeleteAllStars(db)
}

func starslistGoHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)

	listOfStarsGo := backend.GetListOfStarsGo(db)
	_, _ = fmt.Fprintf(w, "%v", listOfStarsGo)
}

func starslistCsvHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)

	listOfStarsCsv := backend.GetListOfStarsCsv(db)
	for _, element := range listOfStarsCsv {
		_, _ = fmt.Fprintf(w, "%s\n", element)
	}
}

func updateTotalMassHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)

	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)
	backend.UpdateTotalMass(db, index)
}

func updateCenterOfMassHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)

	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)
	backend.UpdateCenterOfMass(db, index)
}

func genForestTreeHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)

	parseFormErr := r.ParseForm()
	errHandler("genForestTree Parse Form", parseFormErr)

	// get the index of the tree that should be shown
	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)
	log.Printf("Generating the forest representation of the tree with the index %d", index)

	// generate the forest representation and write it back to the user
	forestTree := backend.GenForestTree(db, index)
	_, _ = fmt.Fprintf(w, "%s\n", forestTree)
}

func createNodesTableHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)

	backend.InitNodesTable(db)
}

func createStarsTableHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)

	backend.InitStarsTable(db)
}

func insertStarHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)

	// parse the http post form
	err := r.ParseForm()
	if err != nil {
		panic(err)
	}

	// parse the star parameters
	x, xParseErr := strconv.ParseFloat(r.PostFormValue("x"), 64)
	errHandler("parse x", xParseErr)
	y, yParseErr := strconv.ParseFloat(r.PostFormValue("y"), 64)
	errHandler("parse y", yParseErr)
	vx, vxParseErr := strconv.ParseFloat(r.PostFormValue("vx"), 64)
	errHandler("parse vx", vxParseErr)
	vy, vyParseErr := strconv.ParseFloat(r.PostFormValue("vy"), 64)
	errHandler("parse vy", vyParseErr)
	m, mParseErr := strconv.ParseFloat(r.PostFormValue("m"), 64)
	errHandler("parse m", mParseErr)

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

	// parse the tree index
	index, indexParseErr := strconv.ParseInt(r.Form.Get("index"), 10, 64)
	errHandler("parse index", indexParseErr)

	// Insert the star into the tree
	backend.InsertStar(db, star, index)
}

func insertStarListHandler(w http.ResponseWriter, r *http.Request) {
	requestInfo(r)

	// parse the http post form
	err := r.ParseForm()
	if err != nil {
		panic(err)
	}

	// get the filename of the file to insert
	filename := r.PostFormValue("filename")

	// open the csv file and parse it
	csvFile, _ := os.Open(fmt.Sprintf("%s.csv", filename))
	reader := csv.NewReader(bufio.NewReader(csvFile))

	// iterate over all the lines
	for {

		// read the line
		line, error := reader.Read()

		// handler errors such as broken syntax and the end of the file
		if error == io.EOF {
			break
		} else if error != nil {
			log.Fatal(error)
		}

		// parse the star parameters
		x, xParseErr := strconv.ParseFloat(line[0], 64)
		errHandler("parse x", xParseErr)
		y, xParseErr := strconv.ParseFloat(line[1], 64)
		errHandler("parse y", xParseErr)

		// define a star
		star := structs.Star2D{
			C: structs.Vec2{
				X: x,
				Y: y,
			},
			V: structs.Vec2{
				X: 0,
				Y: 0,
			},
			M: 1000,
		}

		// insert the star into the database
		backend.InsertStar(db, star, 1)
	}
}

func main() {
	// get the port on which the service should be hosted and the url of the database
	var port string
	flag.StringVar(&port, "port", "8080", "port used to host the service")
	flag.Parse()
	log.Println("[ ] Done loading the flags")

	// get the data that should be used to connect to the database
	var DBURL = os.Getenv("DBURL")
	var DBUSER = os.Getenv("DBUSER")
	var DBPASSWD = os.Getenv("DBPASSWD")
	var DBPORT, _ = strconv.ParseInt(os.Getenv("DBPORT"), 10, 64)
	var DBPROJECTNAME = os.Getenv("DBPROJECTNAME")

	log.Printf("DBURL: %s", DBURL)
	log.Printf("DBUSER: %s", DBUSER)
	log.Printf("DBPASSWD: %s", DBPASSWD)
	log.Printf("DBPORT: %d", DBPORT)
	log.Printf("DBPROJECTNAME: %s", DBPROJECTNAME)
	log.Printf("frontend port: %s", port)

	// connect to the database
	connStr := fmt.Sprintf("host=%s port=%d user=%s "+
		"password=%s dbname=%s sslmode=disable",
		DBURL, DBPORT, DBUSER, DBPASSWD, DBPROJECTNAME)

	var err error
	db, err = sql.Open("postgres", connStr)
	if err != nil {
		log.Fatalf("Error: The data source arguments are not valid: %s", err)
	}
	log.Println("[ ] Done Connecting to the DB")

	// ping the db
	err = db.Ping()
	if err != nil {
		panic(err)
	}

	log.Println("[ ] Done Pinging the DB")

	// define a new mux router
	router := mux.NewRouter()

	// define the endpoints
	router.HandleFunc("/", indexHandler).Methods("GET")
	router.HandleFunc("/newTree", newTreeHandler).Methods("POST")
	router.HandleFunc("/deleteStars", deleteStarsHandler).Methods("POST")
	router.HandleFunc("/deleteNodes", deleteNodesHandler).Methods("POST")
	router.HandleFunc("/starslist/go", starslistGoHandler).Methods("GET")
	router.HandleFunc("/starslist/csv", starslistCsvHandler).Methods("GET")
	router.HandleFunc("/updateTotalMass", updateTotalMassHandler).Methods("POST")
	router.HandleFunc("/updateCenterOfMass", updateCenterOfMassHandler).Methods("POST")
	router.HandleFunc("/genForestTree", genForestTreeHandler).Methods("GET")
	router.HandleFunc("/createNodesTable", createNodesTableHandler).Methods("POST")
	router.HandleFunc("/createStarsTable", createStarsTableHandler).Methods("POST")
	router.HandleFunc("/insertStar", insertStarHandler).Methods("POST")
	router.HandleFunc("/insertStarList", insertStarListHandler).Methods("POST")

	log.Printf("[ ] Starting the API on localhost:%s", port)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), router))
}