diff --git a/country.go b/country.go index 19ce497556f21ceeb8943230604a017e0534d5a3..05fa12613af163f4693ce3c1ebd23697019c0329 100644 --- a/country.go +++ b/country.go @@ -2,7 +2,6 @@ package assignment1 import ( "encoding/json" - "fmt" "net/http" "strings" ) @@ -16,7 +15,7 @@ func countryRequest(url string, c *http.Client, country *Country) { // decodes the response into the struct err := json.NewDecoder(resp.Body).Decode(&country) if err != nil { - fmt.Println("Error", err.Error()) + panic(err) } // closes the body @@ -35,7 +34,7 @@ func speciesInCountryRequest(url string, c *http.Client, country *Country) { // decodes the response into the array of results err := json.NewDecoder(resp.Body).Decode(nameAndKey) if err != nil { - fmt.Println("Error", err.Error()) + panic(err) } // closes the body @@ -67,6 +66,7 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { urlQuery := r.URL.RawQuery parts := strings.Split(r.URL.Path, "/") + // checks if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "country" || parts[4] == "" { status := http.StatusBadRequest http.Error(w, "Expecting format /conservation/v1/country/'AlphaCode'", status) @@ -77,7 +77,7 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { APIURL += parts[4] APIURL2 := "http://api.gbif.org/v1/occurrence/search?country=" + parts[4] + "&" + urlQuery - //empty country struct + // empty country struct country := &Country{} // makes a client @@ -90,6 +90,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { // encodes everything to the browser err := json.NewEncoder(w).Encode(country) if err != nil { - fmt.Println("Error", err.Error()) + panic(err) } } diff --git a/diagnostics.go b/diagnostics.go index 33cb93d05140e37c2fbf1dcc58d99d67d49f762b..4e120a522010a7fdb731c4f93ce4823512091add 100644 --- a/diagnostics.go +++ b/diagnostics.go @@ -2,7 +2,6 @@ package assignment1 import ( "encoding/json" - "fmt" "net/http" ) @@ -30,6 +29,6 @@ func HandlerDiag(w http.ResponseWriter, r *http.Request) { // encodes everything to the browser err := json.NewEncoder(w).Encode(diagnostics) if err != nil { - fmt.Println("Error", err.Error()) + panic(err) } } diff --git a/func.go b/func.go index 336a1cc656f455cc973c28f17d82720996e49873..af66e733cc13034ce1f700fa93a73f3674abe901 100644 --- a/func.go +++ b/func.go @@ -11,17 +11,17 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) { http.Error(w, "Invalid request", http.StatusBadRequest) } -// handles all the requests and returns the response +// handles the requests from the apis and returns the response func doRequest(url string, c *http.Client) *http.Response { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { - fmt.Println("Error", err) + panic(err) } resp, err := c.Do(req) if err != nil { - fmt.Println("Error", err) + panic(err) } return resp diff --git a/species.go b/species.go index ae96f4d6edb53fdc1f9960e8ab7b7ce869733659..52c1d7aba8dff767f1ccf90166bbcb5a39c8df39 100644 --- a/species.go +++ b/species.go @@ -2,7 +2,6 @@ package assignment1 import ( "encoding/json" - "fmt" "net/http" "strings" ) @@ -16,7 +15,7 @@ func speciesRequest(url string, c *http.Client, species *Species) { // decodes the response into the struct err := json.NewDecoder(resp.Body).Decode(&species) if err != nil { - fmt.Println("Error", err.Error()) + panic(err) } // closes the body @@ -35,7 +34,7 @@ func yearRequest(url string, c *http.Client, species *Species) { // decodes the response into the struct err := json.NewDecoder(resp.Body).Decode(&year) if err != nil { - fmt.Println("Error", err.Error()) + panic(err) } // puts the year from the year struct into the species struct @@ -79,6 +78,6 @@ func HandlerSpecies(w http.ResponseWriter, r *http.Request) { // encodes species which now contains year as well err := json.NewEncoder(w).Encode(species) if err != nil { - fmt.Println("Error", err.Error()) + panic(err) } } diff --git a/struct.go b/struct.go index f5c00cbc99ea871d9f6330b9b0e906a8ab1d0d2d..aa35085d16bd191cdb89278c2b980e5e0c43ab9f 100644 --- a/struct.go +++ b/struct.go @@ -38,7 +38,7 @@ type Year struct { Year string `json:"bracketYear"` } -// Diag struct: +// Diag struct: struct for diagnostics type Diag struct { Gbif int Restcountries int diff --git a/uptime.go b/uptime.go index 99d49819f97ed80fecd3475b76b25b5de2422c3f..e1aaf5272b561028155a170a459488bb2b989387 100644 --- a/uptime.go +++ b/uptime.go @@ -4,7 +4,8 @@ import ( "time" ) -var startTime time.Time // to measure the time since the last service restart +// to measure the time since the last service restart +var startTime time.Time // Uptime function: returns time since start time in seconds func Uptime() float64 {