Commit b46f4dcd authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

status code 502 when appropriate

parent e8edf67a
......@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
)
......@@ -16,13 +17,16 @@ func Chandler(w http.ResponseWriter, r *http.Request) {
}
country, code := pkg.GetCountryByCode(vars["country_identifier"], limit)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
c, _ := json.Marshal(country)
if code == 200 {
_, _ = fmt.Fprintln(w, string(c))
w.Header().Set("Content-Type", "application/json")
_, err := fmt.Fprintln(w, string(c))
if err != nil {
log.Fatal(err)
}
} else {
_, _ = fmt.Fprintln(w, code)
w.WriteHeader(http.StatusBadGateway)
_, _ = fmt.Fprintln(w, http.StatusBadGateway)
}
}
\ No newline at end of file
......@@ -12,7 +12,6 @@ func Dhandler(w http.ResponseWriter, r *http.Request) {
pkg.GetDiag(&d)
w.Header().Set("Content-Type", "application/json")
marsh, _ := json.Marshal(d)
_, _ = fmt.Fprintln(w, string(marsh))
}
......
......@@ -10,16 +10,15 @@ import (
func Shandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
specie, code := pkg.GetSpecies(vars["speciesKey"])
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
s, _ := json.Marshal(specie)
if code == 200 {
w.Header().Set("Content-Type", "application/json")
_, _ = fmt.Fprintln(w, string(s))
} else {
_, _ = fmt.Fprintln(w, code)
w.WriteHeader(http.StatusBadGateway)
_, _ = fmt.Fprintln(w, http.StatusBadGateway)
}
}
......
......@@ -2,7 +2,7 @@ package pkg
import (
"encoding/json"
"fmt"
"log"
)
const countryApi = "https://restcountries.eu/rest/v2/alpha/"
......@@ -40,7 +40,7 @@ func GetCountryByCode(code, limit string) (Country, int) {
}
err := json.Unmarshal([]byte(cBody), &country)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
// gets species in that country
......@@ -50,7 +50,7 @@ func GetCountryByCode(code, limit string) (Country, int) {
}
err = json.Unmarshal([]byte(rBody), &species)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
// adds species-keys & specie-names to the country struct
......
package pkg
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func getString(url string) (string, int) {
resp, err:= http.Get(url)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
defer resp.Body.Close()
......@@ -17,7 +17,7 @@ func getString(url string) (string, int) {
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
bodyString = string(bodyBytes)
}
......
......@@ -2,7 +2,7 @@ package pkg
import (
"encoding/json"
"fmt"
"log"
)
const speciesApi = "http://api.gbif.org/v1/species/"
......@@ -29,7 +29,7 @@ func GetSpecies(key string) (Specie, int) {
}
err := json.Unmarshal([]byte(sBody), &specie)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
url = url + "/name"
......@@ -39,7 +39,7 @@ func GetSpecies(key string) (Specie, int) {
}
err = json.Unmarshal([]byte(sBody), &specie)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
return specie, code
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment