about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhanemile <hanemile@protonmail.com>2018-12-11 23:24:19 +0100
committerhanemile <hanemile@protonmail.com>2018-12-11 23:24:19 +0100
commit3b84190e10e83d6b782390d2aa3fd91eb92c6c54 (patch)
tree0f5962e4721040391b78b7824492482fb9a105b0
parentb9fa5bb61015a279a40b6e55631a28b8c2ec9302 (diff)
Simple http server exposing an api on localhost:8000/NFW/{value} returning the corresponding NFW-value.
-rw-r--r--main.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..f76dad1
--- /dev/null
+++ b/main.go
@@ -0,0 +1,58 @@
+package main
+
+import (
+	"encoding/json"
+	"github.com/gorilla/mux"
+	"log"
+	"math"
+	"net/http"
+	"strconv"
+)
+
+// Defining the constants used in the NFW profile
+const (
+	sigma float64 = 200
+	f0    float64 = 0.1
+	G     float64 = 4.302e-3
+	rS    float64 = 1e4
+)
+
+// 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 {
+	a := (1) / (math.Sqrt(2*math.Pi) * sigma)
+	b := math.Exp(-phi(r) / math.Pow(sigma, 2))
+	return a * b
+}
+
+// A subset of the NFW function in order to handle the 0 case
+func phi(x float64) float64 {
+	if x == 0 {
+		return -4 * math.Pi * f0 * G * math.Pow(rS, 2)
+	}
+	a := -(4 * math.Pi * f0 * G * math.Pow(rS, 3)) / x
+	b := math.Log(1.0 + (x / rS))
+	return a * b
+}
+
+// NFW is the corresponding HTTP-handler to the NFW Profile
+// 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)
+	if err != nil {
+		panic(err)
+	}
+	var returnValue float64 = rho(distanceToMidpoint)
+	err = json.NewEncoder(w).Encode(returnValue)
+	if err != nil {
+		panic(err)
+	}
+}
+
+func main() {
+	router := mux.NewRouter()
+	router.HandleFunc("/NFW/{r}", NFW).Methods("GET")
+	log.Fatal(http.ListenAndServe(":8000", router))
+}