Skip to content
Snippets Groups Projects
Commit 77b8239f authored by andmag's avatar andmag
Browse files

Finalizing comments

parent ee17aec5
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,6 @@ package assignment1 ...@@ -2,7 +2,6 @@ package assignment1
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"strings" "strings"
) )
...@@ -16,7 +15,7 @@ func countryRequest(url string, c *http.Client, country *Country) { ...@@ -16,7 +15,7 @@ func countryRequest(url string, c *http.Client, country *Country) {
// decodes the response into the struct // decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&country) err := json.NewDecoder(resp.Body).Decode(&country)
if err != nil { if err != nil {
fmt.Println("Error", err.Error()) panic(err)
} }
// closes the body // closes the body
...@@ -35,7 +34,7 @@ func speciesInCountryRequest(url string, c *http.Client, country *Country) { ...@@ -35,7 +34,7 @@ func speciesInCountryRequest(url string, c *http.Client, country *Country) {
// decodes the response into the array of results // decodes the response into the array of results
err := json.NewDecoder(resp.Body).Decode(nameAndKey) err := json.NewDecoder(resp.Body).Decode(nameAndKey)
if err != nil { if err != nil {
fmt.Println("Error", err.Error()) panic(err)
} }
// closes the body // closes the body
...@@ -67,6 +66,7 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { ...@@ -67,6 +66,7 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
urlQuery := r.URL.RawQuery urlQuery := r.URL.RawQuery
parts := strings.Split(r.URL.Path, "/") parts := strings.Split(r.URL.Path, "/")
// checks
if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "country" || parts[4] == "" { if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "country" || parts[4] == "" {
status := http.StatusBadRequest status := http.StatusBadRequest
http.Error(w, "Expecting format /conservation/v1/country/'AlphaCode'", status) http.Error(w, "Expecting format /conservation/v1/country/'AlphaCode'", status)
...@@ -77,7 +77,7 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { ...@@ -77,7 +77,7 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
APIURL += parts[4] APIURL += parts[4]
APIURL2 := "http://api.gbif.org/v1/occurrence/search?country=" + parts[4] + "&" + urlQuery APIURL2 := "http://api.gbif.org/v1/occurrence/search?country=" + parts[4] + "&" + urlQuery
//empty country struct // empty country struct
country := &Country{} country := &Country{}
// makes a client // makes a client
...@@ -90,6 +90,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { ...@@ -90,6 +90,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
// encodes everything to the browser // encodes everything to the browser
err := json.NewEncoder(w).Encode(country) err := json.NewEncoder(w).Encode(country)
if err != nil { if err != nil {
fmt.Println("Error", err.Error()) panic(err)
} }
} }
...@@ -2,7 +2,6 @@ package assignment1 ...@@ -2,7 +2,6 @@ package assignment1
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
) )
...@@ -30,6 +29,6 @@ func HandlerDiag(w http.ResponseWriter, r *http.Request) { ...@@ -30,6 +29,6 @@ func HandlerDiag(w http.ResponseWriter, r *http.Request) {
// encodes everything to the browser // encodes everything to the browser
err := json.NewEncoder(w).Encode(diagnostics) err := json.NewEncoder(w).Encode(diagnostics)
if err != nil { if err != nil {
fmt.Println("Error", err.Error()) panic(err)
} }
} }
...@@ -11,17 +11,17 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) { ...@@ -11,17 +11,17 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Invalid request", http.StatusBadRequest) 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 { func doRequest(url string, c *http.Client) *http.Response {
req, err := http.NewRequest(http.MethodGet, url, nil) req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil { if err != nil {
fmt.Println("Error", err) panic(err)
} }
resp, err := c.Do(req) resp, err := c.Do(req)
if err != nil { if err != nil {
fmt.Println("Error", err) panic(err)
} }
return resp return resp
......
...@@ -2,7 +2,6 @@ package assignment1 ...@@ -2,7 +2,6 @@ package assignment1
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"strings" "strings"
) )
...@@ -16,7 +15,7 @@ func speciesRequest(url string, c *http.Client, species *Species) { ...@@ -16,7 +15,7 @@ func speciesRequest(url string, c *http.Client, species *Species) {
// decodes the response into the struct // decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&species) err := json.NewDecoder(resp.Body).Decode(&species)
if err != nil { if err != nil {
fmt.Println("Error", err.Error()) panic(err)
} }
// closes the body // closes the body
...@@ -35,7 +34,7 @@ func yearRequest(url string, c *http.Client, species *Species) { ...@@ -35,7 +34,7 @@ func yearRequest(url string, c *http.Client, species *Species) {
// decodes the response into the struct // decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&year) err := json.NewDecoder(resp.Body).Decode(&year)
if err != nil { if err != nil {
fmt.Println("Error", err.Error()) panic(err)
} }
// puts the year from the year struct into the species struct // puts the year from the year struct into the species struct
...@@ -79,6 +78,6 @@ func HandlerSpecies(w http.ResponseWriter, r *http.Request) { ...@@ -79,6 +78,6 @@ func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
// encodes species which now contains year as well // encodes species which now contains year as well
err := json.NewEncoder(w).Encode(species) err := json.NewEncoder(w).Encode(species)
if err != nil { if err != nil {
fmt.Println("Error", err.Error()) panic(err)
} }
} }
...@@ -38,7 +38,7 @@ type Year struct { ...@@ -38,7 +38,7 @@ type Year struct {
Year string `json:"bracketYear"` Year string `json:"bracketYear"`
} }
// Diag struct: // Diag struct: struct for diagnostics
type Diag struct { type Diag struct {
Gbif int Gbif int
Restcountries int Restcountries int
......
...@@ -4,7 +4,8 @@ import ( ...@@ -4,7 +4,8 @@ import (
"time" "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 // Uptime function: returns time since start time in seconds
func Uptime() float64 { func Uptime() float64 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment