From 77b8239f13a864eed7e684bb8cb9ac3d8a2007c9 Mon Sep 17 00:00:00 2001 From: andmag <andmag@stud.ntnu.no> Date: Thu, 17 Oct 2019 09:13:40 +0200 Subject: [PATCH] Finalizing comments --- country.go | 10 +++++----- diagnostics.go | 3 +-- func.go | 6 +++--- species.go | 7 +++---- struct.go | 2 +- uptime.go | 3 ++- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/country.go b/country.go index 19ce497..05fa126 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 33cb93d..4e120a5 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 336a1cc..af66e73 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 ae96f4d..52c1d7a 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 f5c00cb..aa35085 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 99d4981..e1aaf52 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 { -- GitLab