diff options
-rw-r--r-- | main.go | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/main.go b/main.go index 6d9ac30..e9e6558 100644 --- a/main.go +++ b/main.go @@ -20,10 +20,12 @@ type point struct { // 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 == "" { - log.Println("no nfwurl given! set one using the nfwurl environment variable!") - return -1, nil + return 0, fmt.Errorf("No nfwurl given!") } // build the request string @@ -31,21 +33,19 @@ func netNFW(x float64, y float64, z float64) (float64, error) { // make the request resp, err := http.Get(randMinRequestURL) + defer resp.Body.Close() if err != nil { panic(err) } - // close the request body when finished - defer resp.Body.Close() - // 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) @@ -112,7 +112,7 @@ func gen(galaxyRange float64) point { return point{0, 0, 0} } -func generate(w http.ResponseWriter, r *http.Request) { +func generateHandler(w http.ResponseWriter, r *http.Request) { params := r.URL.Query() amount, _ := strconv.ParseInt(params.Get("num"), 10, 64) @@ -137,6 +137,6 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { func main() { router := mux.NewRouter() router.HandleFunc("/", indexHandler).Methods("GET") - router.HandleFunc("/gen", generate).Methods("GET") + router.HandleFunc("/gen", generateHandler).Methods("GET") log.Fatal(http.ListenAndServe(":8123", router)) } |