about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhanemile <hanemile@protonmail.com>2018-12-24 22:47:07 +0100
committerhanemile <hanemile@protonmail.com>2018-12-24 22:47:07 +0100
commitaad88e499aee506bf2ddd5f4b4476ab08e9a5185 (patch)
tree0c5055e1cd01a2633eb422899bc03e8f31a131fc
parente4127a1b7030755079d5a5e4f93e7db1e0256e16 (diff)
overall push
-rw-r--r--docker-compose.yml3
-rw-r--r--main.go40
2 files changed, 35 insertions, 8 deletions
diff --git a/docker-compose.yml b/docker-compose.yml
index ea73f9f..689bae9 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,6 +1,7 @@
 version: '3'
+
 services:
   nfw:
     build: .
     ports:
-      - "8003:8000"
\ No newline at end of file
+      - "80:80"
\ No newline at end of file
diff --git a/main.go b/main.go
index f76dad1..bf1ce49 100644
--- a/main.go
+++ b/main.go
@@ -2,6 +2,7 @@ package main
 
 import (
 	"encoding/json"
+	"fmt"
 	"github.com/gorilla/mux"
 	"log"
 	"math"
@@ -17,9 +18,14 @@ const (
 	rS    float64 = 1e4
 )
 
+type result struct {
+	NFW float64 `json:"NFW"`
+}
+
 // The actual NFW profile
 // Uses the given distance to the middle of the galaxy to calculate the probability that a star is generated
-func rho(r float64) float64 {
+func rho(x float64, y float64, z float64) float64 {
+	r := math.Sqrt(math.Pow(x, 2) + math.Pow(y, 2) + math.Pow(z, 2))
 	a := (1) / (math.Sqrt(2*math.Pi) * sigma)
 	b := math.Exp(-phi(r) / math.Pow(sigma, 2))
 	return a * b
@@ -39,20 +45,40 @@ func phi(x float64) float64 {
 // It uses the given distance to the Midpoint of the galaxy to calculate and return the probability that a star
 // is generated.
 func NFW(w http.ResponseWriter, r *http.Request) {
-	params := mux.Vars(r)
-	distanceToMidpoint, err := strconv.ParseFloat(params["r"], 64)
+	params := r.URL.Query()
+
+	// parse the parameters
+	x, _ := strconv.ParseFloat(params.Get("x"), 64)
+	y, _ := strconv.ParseFloat(params.Get("y"), 64)
+	z, _ := strconv.ParseFloat(params.Get("z"), 64)
+
+	// calculate the NFW-value
+	var returnValue result = result{rho(x, y, z)}
+	log.Print(returnValue)
+	b, err := json.Marshal(returnValue)
 	if err != nil {
 		panic(err)
 	}
-	var returnValue float64 = rho(distanceToMidpoint)
-	err = json.NewEncoder(w).Encode(returnValue)
+
+	// http response writer
+	w.Header().Set("Content-Type", "application/json")
+	_, err = fmt.Fprintf(w, "%v", string(b))
 	if err != nil {
 		panic(err)
 	}
 }
 
+func Index(w http.ResponseWriter, r *http.Request) {
+	log.Print(fmt.Fprintln(w, "usage: nfw.docker.localhost/NFW?x=<x>&y=<y>&z=<z>"))
+}
+
 func main() {
 	router := mux.NewRouter()
-	router.HandleFunc("/NFW/{r}", NFW).Methods("GET")
-	log.Fatal(http.ListenAndServe(":8000", router))
+
+	// routes
+	router.HandleFunc("/", Index).Methods("GET")
+	router.HandleFunc("/NFW", NFW).Methods("GET")
+
+	log.Println("Starting the service on port 80")
+	log.Fatal(http.ListenAndServe(":80", router))
 }