about summary refs log tree commit diff
path: root/main.go
blob: 0b06cdee0b3f1c7101ace8e3ac8c3feb1e41b124 (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
package main

import (
	"database/sql"
	"flag"
	"fmt"
	"github.com/gorilla/mux"
	"log"
	"net/http"
	"os"
	"strconv"

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

var (
	db *sql.DB
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
	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) {
	width, _ := strconv.ParseFloat(r.Form.Get("w"), 64)
	backend.NewTree(db, width)
}

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

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

func starslistGoHandler(w http.ResponseWriter, r *http.Request) {
	backend.GetListOfStarsGo(db)
}

func starslistCsvHandler(w http.ResponseWriter, r *http.Request) {
	backend.GetListOfStarsCsv(db)
}

func updateTotalMassHandler(w http.ResponseWriter, r *http.Request) {
	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)
	backend.UpdateTotalMass(db, index)
}

func updateCenterOfMassHandler(w http.ResponseWriter, r *http.Request) {
	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)
	backend.UpdateCenterOfMass(db, index)
}

func genForestTreeHandler(w http.ResponseWriter, r *http.Request) {
	index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64)
	backend.GenForestTree(db, index)
}

func createNodesTableHandler(w http.ResponseWriter, r *http.Request) {
	backend.InitNodesTable(db)
}

func createStarsTableHandler(w http.ResponseWriter, r *http.Request) {
	backend.InitStarsTable(db)
}

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")
	var dbURL string
	flag.StringVar(&dbURL, "DBURL", "postgres", "url of the database used")
	flag.Parse()

	log.Println("[ ] Done loading the flags")

	// get the data that should be used to connect to the database
	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.Println("[ ] Done loading the envs")

	// connect to the database
	connStr := fmt.Sprintf("host=%s port=%d user=%s "+
		"password=%s dbname=%s sslmode=disable",
		dbURL, DBPORT, DBUSER, DBPASSWD, DBPROJECTNAME)
	log.Printf("[ ] Done assembling the connString: %s", connStr)

	db, err := sql.Open("postgres", connStr)
	if err != nil {
		_ = fmt.Errorf("%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")

	// start the http server on the port reached in via a flag
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), router))
}