about summary refs log tree commit diff
path: root/main.go
blob: 12fc8526ea9c511618559f701817a78cfcf9078c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main

import (
	"encoding/json"
	"fmt"
	"github.com/gorilla/mux"
	"io/ioutil"
	"log"
	"math/rand"
	"net/http"
	"os"
	"strconv"
	"time"
)

type point struct {
	x, y, z float64
}

// make a request to the nfw api and return the result
func netNFW(x float64, y float64, z float64) (float64, error) {

	// get the url of the nfw container or a reverse proxy handling the request to the containers
	var nfwurl = os.Getenv("nfwurl")

	// if no url is given, ask
	if nfwurl == "" {
		return 0, fmt.Errorf("No nfwurl given!")
	}

	// build the request string
	var randMinRequestURL string = fmt.Sprintf("http://%s/NFW?x=%f&y=%f&z=%f", nfwurl, x, y, z)

	// make the request
	resp, err := http.Get(randMinRequestURL)
	defer resp.Body.Close()
	if err != nil {
		panic(err)
	}

	// read the body
	body, bodyerr := ioutil.ReadAll(resp.Body)
	if bodyerr != nil {
		panic(bodyerr)
	}

	// define an interface into which the nfw gets unpacked
	var dat map[string]interface{}
	jsonerr := json.Unmarshal(body, &dat)
	if jsonerr != nil {
		panic(jsonerr)
	}

	result := dat["NFW"].(float64)

	return result, nil
}

func gen(galaxyRange float64) point {

	// Define variables
	var length float64 = galaxyRange

	// define the range of the galaxy
	var rangeMin float64 = -length
	var rangeMax float64 = length

	// get the minimal NFW value
	var randMin, errGetMinValue = netNFW(0, 0, 0)
	if errGetMinValue != nil {
		panic(errGetMinValue)
	}

	// get the maximal NFW value
	var randMax, errGetMaxValue = netNFW(length, length, length)
	if errGetMaxValue != nil {
		panic(errGetMaxValue)
	}

	var starFound bool = false

	for starFound == false {

		// define a new random source (without this, the numbers would not be random!)
		randomSource := rand.New(rand.NewSource(time.Now().UnixNano()))

		// generate random coordinates
		var x float64 = ((rangeMax - rangeMin) * randomSource.Float64()) + rangeMin
		var y float64 = ((rangeMax - rangeMin) * randomSource.Float64()) + rangeMin
		var z float64 = ((rangeMax - rangeMin) * randomSource.Float64()) + rangeMin

		// generate a random value in the (randmin, randmax) range
		var randomValue = randomSource.Float64()
		var randVal = ((randMax - randMin) * randomValue) + randMin

		// calculate the nfw-value of the previously generated star
		var nfwVal, err = netNFW(x, y, z)
		if err != nil {
			panic(err)
		}

		// check if th star should be kept or not
		if randVal < nfwVal {
			var newStar point = point{x, y, z}
			starFound = true
			return newStar
		}
	}

	// if no star is found at all, return (0, 0, 0)
	// this code should actually never be reached
	return point{0, 0, 0}
}

// the generate handler gets a number of stars and a range in which the stars should be generated
// and generated them
func generateHandler(w http.ResponseWriter, r *http.Request) {
	params := r.URL.Query()

	// parse the url arguments
	amount, _ := strconv.ParseInt(params.Get("num"), 10, 64)
	galaxyRange, _ := strconv.ParseFloat(params.Get("range"), 64)

	// generate the given amount of stars
	for i := 0; i < int(amount); i++ {

		// generate the star
		result := gen(galaxyRange)

		log.Printf("galaxy range: %f", galaxyRange)
		log.Printf("%v\n", result)

		log.SetOutput(w)
		log.Printf("%f, %f, %f\n", result.x, result.y, result.z)
	}
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
	_, _ = fmt.Fprintf(w, "Generator container here!\nUse the /gen?num=<num>&range=<range> endpoint to generator a star!")
}

func main() {
	router := mux.NewRouter()
	router.HandleFunc("/", indexHandler).Methods("GET")
	router.HandleFunc("/gen", generateHandler).Methods("GET")
	log.Fatal(http.ListenAndServe(":8123", router))
}