From fdd4f8ff060c1c690c794ed3fdbb0f160e3fdf82 Mon Sep 17 00:00:00 2001 From: Emile Date: Thu, 7 Mar 2019 16:32:39 +0100 Subject: used the code from the db-actions package to build a simple http-api split up into a front- and backend --- go.mod | 6 ++ go.sum | 4 ++ main.go | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e35280c --- /dev/null +++ b/go.mod @@ -0,0 +1,6 @@ +module git.darknebu.la/GalaxySimulator/db-controller + +require ( + git.darknebu.la/GalaxySimulator/structs v0.0.0-20190205205735-9dd56b9448e5 + github.com/gorilla/mux v1.7.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5c82375 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +git.darknebu.la/GalaxySimulator/structs v0.0.0-20190205205735-9dd56b9448e5 h1:aEQHEERwdLfRJrXb867wZzRMs1ym+j0zDys3opLWPew= +git.darknebu.la/GalaxySimulator/structs v0.0.0-20190205205735-9dd56b9448e5/go.mod h1:LSDIBBC7IcWERm4wlDAroMpsUP7zJ1yDZFlQnP/UIsQ= +github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U= +github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= diff --git a/main.go b/main.go new file mode 100644 index 0000000..0b06cde --- /dev/null +++ b/main.go @@ -0,0 +1,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 := `

Galaxy Simulator Database Frontend

+ + API: +

/ (GET)

+ +

/new (POST)

+ Create a new Tree +
+ Parameters: + + +

/deleteStars (POST)

+ Delete all stars from the stars Table +
+ Parameters: + + +

/deleteNodes (POST)

+ Delete all nodes from the nodes Table +
+ Parameters: + + +

/starslist/go (GET)

+ List all stars using go-array format +
+ Parameters: + + +

/starslist/csv (GET)

+ List all stars as a csv +
+ Parameters: + + +

/updatetotalmass (POST)

+ Update the total mass of all the nodes in the tree with the given index +
+ Parameters: + + +

/updatecenterofmass (POST)

+ Update the center of mass of all the nodes in the tree with the given index +
+ Parameters: + + +

/genforesttree (GET)

+ Generate the forest representation of the tree with the given index +
+ Parameters: + + + + +` + _, _ = 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)) +} -- cgit 1.4.1