Skip to content
Snippets Groups Projects
Commit 1613e3ea authored by andmag's avatar andmag
Browse files

Optimalization and commenting

parent c4b22699
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,10 @@ import ( ...@@ -10,7 +10,10 @@ import (
func main() { func main() {
// start-time
assignment1.Init() assignment1.Init()
// opens a port
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port == "" { if port == "" {
port = "8080" port = "8080"
......
...@@ -7,91 +7,93 @@ import ( ...@@ -7,91 +7,93 @@ import (
"strings" "strings"
) )
func replyCountry(w http.ResponseWriter, r *http.Request, url string, url2 string) { // sends a request to the api and decodes the response into the country struct
func countryRequest(url string, c *http.Client, country *Country) {
http.Header.Add(w.Header(), "content-type", "application/json") // sends a request for country and gets the response
resp := doRequest(url, c)
req, err := http.NewRequest(http.MethodGet, url, nil) // decodes the response into the struct
if err != nil { err := json.NewDecoder(resp.Body).Decode(&country)
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil { if err != nil {
fmt.Println("Error", err) fmt.Println("Error", err.Error())
} }
// closes the body
defer resp.Body.Close() defer resp.Body.Close()
country := &Country{}
json.NewDecoder(resp.Body).Decode(country)
replySpeciesCountry(w, r, url2, country)
json.NewEncoder(w).Encode(country)
} }
func replySpeciesCountry(w http.ResponseWriter, r *http.Request, url string, country *Country) { // sends a request to the api and decodes the respone into the structs
func speciesInCountryRequest(url string, c *http.Client, country *Country) {
req, err := http.NewRequest(http.MethodGet, url, nil) // sends a request for species in a country and gets the response
if err != nil { resp := doRequest(url, c)
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close()
// array of results structs
type Result Results type Result Results
// empty result struct
var nameAndKey = &Result{} var nameAndKey = &Result{}
err2 := json.NewDecoder(resp.Body).Decode(nameAndKey) // decodes the response into the array of results
if err2 != nil { err := json.NewDecoder(resp.Body).Decode(nameAndKey)
fmt.Println("Error", err2.Error()) if err != nil {
fmt.Println("Error", err.Error())
} }
// closes the body
defer resp.Body.Close()
// slice of species and speciesKey
var spec []string var spec []string
var specKey []int var specKey []int
for k, v := range nameAndKey.Result { // appends species and speciesKey to a list so we get two lists, one for species and one for speciesKey
fmt.Println(k) for _, v := range nameAndKey.Result {
spec = append(spec, v.Species) spec = append(spec, v.Species)
specKey = append(specKey, v.SpeciesKey) specKey = append(specKey, v.SpeciesKey)
} }
// puts the lists in the country struct
country.Species = spec country.Species = spec
country.SpeciesKey = specKey country.SpeciesKey = specKey
} }
// HandlerCountry dskjfhskjfhk // HandlerCountry function: handles the country endpoint
func HandlerCountry(w http.ResponseWriter, r *http.Request) { func HandlerCountry(w http.ResponseWriter, r *http.Request) {
APIURL := "https://restcountries.eu/rest/v2/alpha/" http.Header.Add(w.Header(), "content-type", "application/json")
//SJEKK LENGDE 4 APIURL := "https://restcountries.eu/rest/v2/alpha/"
// gets the query we want and splits the string into parts
urlQuery := r.URL.RawQuery
parts := strings.Split(r.URL.Path, "/") parts := strings.Split(r.URL.Path, "/")
if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "country" || parts[4] == "" { /*
status := http.StatusBadRequest if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "country" || parts[4] == "" {
http.Error(w, "Expecting format /conservation/v1/country/'AlphaCode'", status) status := http.StatusBadRequest
return http.Error(w, "Expecting format /conservation/v1/country/'AlphaCode'", status)
} return
}
*/
// appends the country-code to the country api and appends country-code and limit to the species api
APIURL += parts[4] APIURL += parts[4]
APIURL2 := "http://api.gbif.org/v1/occurrence/search?country=" APIURL2 := "http://api.gbif.org/v1/occurrence/search?country=" + parts[4] + "&" + urlQuery
APIURL2 += parts[4]
APIURL2 += "?limit=" //empty country struct
country := &Country{}
replyCountry(w, r, APIURL, APIURL2) // makes a client
client := http.DefaultClient
// makes a request for country and species in a country and decodes everything
countryRequest(APIURL, client, country)
speciesInCountryRequest(APIURL2, client, country)
// encodes everything to the browser
err := json.NewEncoder(w).Encode(country)
if err != nil {
fmt.Println("Error", err.Error())
}
} }
...@@ -6,12 +6,7 @@ import ( ...@@ -6,12 +6,7 @@ import (
"net/http" "net/http"
) )
// HandlerNil kjhfkerjhgk // gets the status code from the url/api
func HandlerNil(w http.ResponseWriter, r *http.Request) {
fmt.Println("Default Handler: Invalid request received.")
http.Error(w, "Invalid request", http.StatusBadRequest)
}
func getStatusCode(URL string) int { func getStatusCode(URL string) int {
resp, err := http.Get(URL) resp, err := http.Get(URL)
if err != nil { if err != nil {
...@@ -20,15 +15,21 @@ func getStatusCode(URL string) int { ...@@ -20,15 +15,21 @@ func getStatusCode(URL string) int {
return resp.StatusCode return resp.StatusCode
} }
// HandlerDiag dsjfbgdfjgb // HandlerDiag function: handles the diag endpoint
func HandlerDiag(w http.ResponseWriter, r *http.Request) { func HandlerDiag(w http.ResponseWriter, r *http.Request) {
http.Header.Add(w.Header(), "content-type", "application/json") http.Header.Add(w.Header(), "content-type", "application/json")
diagnostics := &Diag{} diagnostics := &Diag{} // empty diag struct
diagnostics.Uptime = Uptime() diagnostics.Uptime = Uptime() // uptime in seconds
diagnostics.Version = "v1" diagnostics.Version = "v1" // version
// gets statuscode from the apis
diagnostics.Restcountries = getStatusCode("https://restcountries.eu/") diagnostics.Restcountries = getStatusCode("https://restcountries.eu/")
diagnostics.Gbif = getStatusCode("http://api.gbif.org/v1/") diagnostics.Gbif = getStatusCode("http://api.gbif.org/v1/")
json.NewEncoder(w).Encode(diagnostics) // encodes everything to the browser
err := json.NewEncoder(w).Encode(diagnostics)
if err != nil {
fmt.Println("Error", err.Error())
}
} }
func.go 0 → 100644
package assignment1
import (
"fmt"
"net/http"
)
// HandlerNil function: if the user doesn't write anything valid
func HandlerNil(w http.ResponseWriter, r *http.Request) {
fmt.Println("Default Handler: Invalid request received.")
http.Error(w, "Invalid request", http.StatusBadRequest)
}
// handles all the requests 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)
}
resp, err := c.Do(req)
if err != nil {
fmt.Println("Error", err)
}
return resp
}
...@@ -7,55 +7,78 @@ import ( ...@@ -7,55 +7,78 @@ import (
"strings" "strings"
) )
// HandlerSpecies kdsjfhkdjghlfdkhn // sends a request to the api and decodes the response into the species struct
func speciesRequest(url string, c *http.Client, species *Species) {
// sends a request to the api and gets a response
resp := doRequest(url, c)
// decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&species)
if err != nil {
fmt.Println("Error", err.Error())
}
// closes the body
defer resp.Body.Close()
}
// sends a request to the api and decodes the response into the year struct
func yearRequest(url string, c *http.Client, species *Species) {
// sends a request to the api and gets a response
resp := doRequest(url, c)
// empty year struct
year := &Year{}
// decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&year)
if err != nil {
fmt.Println("Error", err.Error())
}
// puts the year from the year struct into the species struct
species.Year = year.Year
// closes the body
defer resp.Body.Close()
}
// HandlerSpecies function: handles the species endpoint
func HandlerSpecies(w http.ResponseWriter, r *http.Request) { func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
http.Header.Add(w.Header(), "content-type", "application/json") http.Header.Add(w.Header(), "content-type", "application/json")
// empty struct that will eventually contain the species values
species := &Species{} species := &Species{}
APIURL := "http://api.gbif.org/v1/species/" APIURL := "http://api.gbif.org/v1/species/"
// splits the url so we can append user input to the apiurl
parts := strings.Split(r.URL.Path, "/") parts := strings.Split(r.URL.Path, "/")
// checks if it is a valid path
if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "species" || parts[4] == "" { if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "species" || parts[4] == "" {
status := http.StatusBadRequest status := http.StatusBadRequest
http.Error(w, "Expecting format /conservation/v1/species/'speciesNumber'", status) http.Error(w, "Expecting format /conservation/v1/species/'speciesNumber'", status)
return return
} }
// Appends userinput to the apiurl
APIURL += parts[4] APIURL += parts[4]
APIURL2 := APIURL + "/name" APIURL2 := APIURL + "/name"
s := "species" // makes a client
y := "year"
replySpecies(w, r, APIURL, s, species)
replySpecies(w, r, APIURL2, y, species)
json.NewEncoder(w).Encode(species)
}
func replySpecies(w http.ResponseWriter, r *http.Request, url string, str string, species *Species) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close() // sends requests to the apis, and decodes everything
speciesRequest(APIURL, client, species)
yearRequest(APIURL2, client, species)
switch str { // encodes species which now contains year as well
case "species": err := json.NewEncoder(w).Encode(species)
json.NewDecoder(resp.Body).Decode(&species) if err != nil {
case "year": fmt.Println("Error", err.Error())
year := &Year{}
json.NewDecoder(resp.Body).Decode(&year)
species.Year = year.Year
} }
} }
package assignment1 package assignment1
// Country dslkjfslkfjl // Country struct: json values for a country and its species
type Country struct { type Country struct {
Code string `json:"alpha2Code"` Code string `json:"alpha2Code"`
Countryname string `json:"name"` Countryname string `json:"name"`
...@@ -9,17 +9,18 @@ type Country struct { ...@@ -9,17 +9,18 @@ type Country struct {
SpeciesKey []int `json:"speciesKey"` SpeciesKey []int `json:"speciesKey"`
} }
// Name jdhgkjdng // Both struct: json values for species and speciesKey
type Both struct { type Both struct {
Species string `json:"species"` Species string `json:"species"`
SpeciesKey int `json:"speciesKey"` SpeciesKey int `json:"speciesKey"`
} }
// Results struct: json value for result and contains both species and speciesKey
type Results struct { type Results struct {
Result []Both `json:"results"` Result []Both `json:"results"`
} }
// Species comment endpoint // Species struct: json values for one specific species
type Species struct { type Species struct {
Key int `json:"key"` Key int `json:"key"`
Kingdom string `json:"kingdom"` Kingdom string `json:"kingdom"`
...@@ -32,12 +33,12 @@ type Species struct { ...@@ -32,12 +33,12 @@ type Species struct {
Year string `json:"year"` Year string `json:"year"`
} }
// Year lkerjgflkjtgj // Year struct: json value for a species discovery year
type Year struct { type Year struct {
Year string `json:"bracketYear"` Year string `json:"bracketYear"`
} }
// Diag comment endpoint // Diag struct:
type Diag struct { type Diag struct {
Gbif int Gbif int
Restcountries int Restcountries int
......
...@@ -4,12 +4,14 @@ import ( ...@@ -4,12 +4,14 @@ import (
"time" "time"
) )
var startTime time.Time var startTime time.Time // to measure the time since the last service restart
// Uptime function: returns time since start time in seconds
func Uptime() float64 { func Uptime() float64 {
return time.Since(startTime).Seconds() return time.Since(startTime).Seconds()
} }
// Init function: begins the time
func Init() { func Init() {
startTime = time.Now() startTime = time.Now()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment