From aad88e499aee506bf2ddd5f4b4476ab08e9a5185 Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 24 Dec 2018 22:47:07 +0100 Subject: overall push --- main.go | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'main.go') 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=&y=&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)) } -- cgit 1.4.1