diff options
author | Emile <hanemile@protonmail.com> | 2019-03-08 12:51:31 +0100 |
---|---|---|
committer | Emile <hanemile@protonmail.com> | 2019-03-08 12:51:31 +0100 |
commit | 5d82b21165ab6fa55230a4476ff60de513414b07 (patch) | |
tree | 6327ef8cf4b99890798a2d484f22241042a349fd | |
parent | 447138f713e063cc9e7f70d4d3b6ad0ba93f6213 (diff) |
updated the api
-rw-r--r-- | docker-compose.yml | 1 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 12 | ||||
-rw-r--r-- | http.go | 113 | ||||
-rw-r--r-- | main.go | 43 |
5 files changed, 127 insertions, 44 deletions
diff --git a/docker-compose.yml b/docker-compose.yml index 77c5f8a..481a6e1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,6 @@ services: - "traefik.port=5432" - "traefik.enable=true" - ################################################################################ # Reverse Proxy / Load Balancer ################################################################################ diff --git a/go.mod b/go.mod index a5ccf62..c3bea12 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module git.darknebu.la/GalaxySimulator/db-container require ( - git.darknebu.la/GalaxySimulator/db_actions v0.0.0-20190213154609-4b94165a34c3 + git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190306191056-33c5bb6c383a 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 index 725c650..873a510 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,11 @@ -git.darknebu.la/GalaxySimulator/db_actions v0.0.0-20190213121420-b93f9305a104 h1:lnhAcsTuY5Cbj1yrlGhIjTY9mOUDoi1VzCf22gftxHU= -git.darknebu.la/GalaxySimulator/db_actions v0.0.0-20190213121420-b93f9305a104/go.mod h1:xiYCANQjztJHGClnE+Akf8Lgvl80EZwDiActPzdIXaQ= -git.darknebu.la/GalaxySimulator/db_actions v0.0.0-20190213154609-4b94165a34c3 h1:FB8RIROLM1v4iib8oBmzjHZGYIJnn3kHoorSRfEs+70= -git.darknebu.la/GalaxySimulator/db_actions v0.0.0-20190213154609-4b94165a34c3/go.mod h1:om84PUzEtYCjSVefmas+eFFadDz6xn+AKzr0alFZ7Ec= +git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190306151439-70a98048d920 h1:qJMQ7p8AQuR25g4sL7ZcQ6aff5mPStgUpz9A8zqIyME= +git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190306151439-70a98048d920/go.mod h1:D5fanr1NuL8GqX+Eslmpf0+l9dMNZVabmEoysbDlKW0= +git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190306185518-ac0afbf77f17 h1:Q98dq1E4BOMM5cUoH9T3XaV8/C0ubVqkr8I/+x5IlUY= +git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190306185518-ac0afbf77f17/go.mod h1:D5fanr1NuL8GqX+Eslmpf0+l9dMNZVabmEoysbDlKW0= +git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190306190851-4e0e07d64e28 h1:RiLkL6TmWU1qTOfcfxwkZmtHESyW7L25NoJX8aiJShw= +git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190306190851-4e0e07d64e28/go.mod h1:D5fanr1NuL8GqX+Eslmpf0+l9dMNZVabmEoysbDlKW0= +git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190306191056-33c5bb6c383a h1:2BKSQG2WwI5su6Nu73eW1z0j1Jp1vr79FtC1+yAGmxQ= +git.darknebu.la/GalaxySimulator/db-actions v0.0.0-20190306191056-33c5bb6c383a/go.mod h1:D5fanr1NuL8GqX+Eslmpf0+l9dMNZVabmEoysbDlKW0= 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= diff --git a/http.go b/http.go index 851fa53..af4fdba 100644 --- a/http.go +++ b/http.go @@ -18,32 +18,99 @@ package main import ( "database/sql" - "git.darknebu.la/GalaxySimulator/db_actions" + "git.darknebu.la/GalaxySimulator/db-actions" "git.darknebu.la/GalaxySimulator/structs" ) // IndexEndpoint gives a basic overview over the api func indexEndpoint() string { - indexString := `Galaxy Simulator Database + indexString := `<html><body><h1>Galaxy Simulator Database</h1> API: - - / ("GET") - - /new ("POST") - - /printall ("GET") - - /insert/{treeindex} ("POST") - - /starlist/{treeindex} ("GET") - - /dumptree/{treeindex} ("GET") - - - /updatetotalmass/{treeindex} ("GET") - - /updatecenterofmass/{treeindex} ("GET") - - - /metrics ("GET") - - /export/{treeindex} ("POST") - - - /fastinsertjson/{filename} ("GET") - - /fastinsertlist/{filename} ("GET") - - - /readdir ("GET") +<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> ` return indexString @@ -95,3 +162,11 @@ func updateCenterOfMassEndpoint(db *sql.DB, index int64) { func genForestTreeEndpoint(db *sql.DB, index int64) string { return db_actions.GenForestTree(db, index) } + +func initStarsTableEndpoint(db *sql.DB) { + db_actions.InitStarsTable(db) +} + +func initNodesTableEndpoint(db *sql.DB) { + db_actions.InitNodesTable(db) +} diff --git a/main.go b/main.go index e021c12..bfe4974 100644 --- a/main.go +++ b/main.go @@ -18,15 +18,14 @@ package main import ( "database/sql" + "flag" "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 ( @@ -166,27 +165,35 @@ func updateCenterOfMassHandler(w http.ResponseWriter, r *http.Request) { 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) - } + index, _ := strconv.ParseInt(r.Form.Get("index"), 10, 64) - tree := genForestTreeEndpoint(db, treeindex) + tree := genForestTreeEndpoint(db, index) _, _ = fmt.Fprintf(w, "%s", tree) } -func testCalcHandler(w http.ResponseWriter, r *http.Request) { - log.Println("[ ] The testCalcHandler was accessed") +func initStarsTableHandler(w http.ResponseWriter, r *http.Request) { + initStarsTableEndpoint(db) +} + +func initNodesTableHandler(w http.ResponseWriter, r *http.Request) { + initNodesTableEndpoint(db) } func main() { + 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() + 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") @@ -196,17 +203,15 @@ func main() { 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("/genforesttree", genForestTreeHandler).Methods("GET") - //router.HandleFunc("/metrics", metricHandler).Methods("GET") - //router.HandleFunc("/export/{treeindex}", exportHandler).Methods("POST") - //router.HandleFunc("/nrofgalaxies", nrofgalaxiesHandler).Methods("GET") + router.HandleFunc("/initStarsTable", initStarsTableHandler).Methods("POST") + router.HandleFunc("/initNodesTable", initNodesTableHandler).Methods("POST") - db = db_actions.ConnectToDB() + connStr := fmt.Sprintf("postgres://postgres:postgres@%s/postgres?sslmode=none", dbURL) + db, _ := sql.Open("postgres", connStr) db.SetMaxOpenConns(75) - fmt.Println("Database Container up on port 8081") - log.Fatal(http.ListenAndServe(":8081", router)) + fmt.Printf("Database Container up on localhost:%s\n", port) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), router)) } |