diff --git a/assignment1 b/assignment1 index 63e8da778e889c6a6bc09365b18ea140fa36efa8..e550227f1c740d4f844b53f6bbf9d8c5be16abb7 100755 Binary files a/assignment1 and b/assignment1 differ diff --git a/main.go b/main.go index 643cac9a5320e7904df5b2c59036b643d5360db5..dcae17c015bd47dbca388575638c49010f2b32b4 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "html" "log" @@ -14,22 +15,36 @@ import ( // URL given to r, page data loaded. // formatted HTML is then sent to the page via W + + func urlSplitForCountry(a string)(string, string){ +const lengthUrl = 5 - // 0/1=conserv/2=v1/3=country/4=name/5=no&limit=10 + // 0/1=conserv/2=v1/3=country/4=no&limit=10 partsOne := strings.Split(a, "/") - // 0=no & 1=limit=no - partsTwo := strings.Split(partsOne[5], "&") + + // 0=no & 1=limit=10 + partsTwo := strings.Split(partsOne[4], "&") + // 0=&limit 1=10 - partsThree := strings.Split(partsOne[5],"=") + partsThree := strings.Split(partsOne[4], "=") + + + fmt.Printf(" PartsOne: %d\n PartsOne[4]: %d\n", len(partsOne), len(partsOne[4])) + if len(partsOne) == lengthUrl { - CountryCode := partsTwo[0] - fmt.Printf("CountryCode is: %s\n",CountryCode) + CountryCode := partsTwo[0] + fmt.Printf("CountryCode is: %s\n", CountryCode) - Limit := partsThree[1] - fmt.Printf("Limit is: %s\n",Limit) + Limit := partsThree[1] + fmt.Printf("Limit is: %s\n", Limit) + + return CountryCode, Limit + + } else {fmt.Printf("Remember to use /conservation/v1/country/<value>&limit=<value> ") + return "","" + } - return CountryCode,Limit } func defaultHandler(w http.ResponseWriter, r *http.Request){ @@ -45,13 +60,15 @@ func viewHandler(w http.ResponseWriter, r *http.Request) { CountryCode, Limit := urlSplitForCountry(r.URL.Path) // Get country - Country, Ccode, Flag := getCountryName(CountryCode) + CountryInfo:= getCountryName(CountryCode) - fmt.Fprintf(w,"Country name is %s\n Country code is: %s\n Flag is: %s\n ", Country, Ccode, Flag) + //fmt.Fprintf(w,"Country name is %s\n Country code is: %s\n Flag is: %s\n ", Country, Ccode, Flag) // get species from a given country Species := getSpeciesByCountry(CountryCode,Limit) - fmt.Fprintf(w,"Species %v\n",Species) + //fmt.Fprintf(w,"Species %v\n",Species.Collection) + json.NewEncoder(w).Encode(CountryInfo) + json.NewEncoder(w).Encode(Species) } func speciesHandler(w http.ResponseWriter, r *http.Request){ @@ -66,16 +83,16 @@ Species := getSpecies(speciesKey) Year:= getYearForSpecie(speciesKey) // write to web -fmt.Fprintf(w," Species: %v\n Year: %s\n ", Species, Year) - +//fmt.Fprintf(w," Species: %v\n Year: %s\n ", Species, Year) + json.NewEncoder(w).Encode(Species) + json.NewEncoder(w).Encode(Year) } func diagnosticHandler(w http.ResponseWriter, r *http.Request){ // /conservation/v1/diag/ - StatGBIF, StatRestC, Version, Time := getDiagnostic() - fmt.Fprintf(w," GBIF API Status: %d\n Rest Countries API Status: %d\n", StatGBIF, StatRestC) + Diagnostic := getDiagnostic() - fmt.Fprintf(w," Version: %s\n UpTime: %s\n", Version, Time) + json.NewEncoder(w).Encode(Diagnostic) } // Global var type time @@ -85,6 +102,9 @@ var Start time.Time func main() { port := os.Getenv("PORT") + // Remove this when deploy + port ="8080" + if port == "" { //log.fatal formatting output and exits log.Fatal("$PORT must be set") @@ -98,7 +118,7 @@ func main() { // Add code to this function http.HandleFunc("/", defaultHandler) - http.HandleFunc("/conservation/v1/country/name/", viewHandler) + http.HandleFunc("/conservation/v1/country/", viewHandler) http.HandleFunc("/conservation/v1/species/", speciesHandler) diff --git a/webapp.go b/webapp.go index f16398c3f722701c92ac1fbb8f051e1d9a72732e..019c3a57176d486e354ab946a284a0a36c927846 100644 --- a/webapp.go +++ b/webapp.go @@ -57,56 +57,58 @@ type SpeciesKeyBody struct{ type SpeciesYear struct{ //Hint: For the year, check the /species/{key}/name path for a given key. - Year string `json:"bracketYear"` + Year int `json:"bracketYear"` } -func getDiagnostic() (int,int,string, time.Duration) { +type Diagnostics struct{ +Gbif int +Restcountries int +Version string +Uptime time.Duration +} - /* - "gbif": "<http status code for GBIF API>", - "restcountries": "<http status code for restcountries API>", - "version": "v1", - "uptime": <time in seconds from the last service restart> +func getDiagnostic() Diagnostics { - */ - //res := Response{} urlGBIF := apiRootGBIF respDiag, err := http.Get(urlGBIF) + fmt.Printf("Dette er vår respDiag%s\n",respDiag) if err !=nil { fmt.Println("Error:we have a problem", err) - return 0,0, "",0 + return Diagnostics{} } defer respDiag.Body.Close() - //json.NewDecoder(respDiag.Body).Decode(ci) + + //json.NewDecoder(respDiag.Body).Decode(diag.Gbif) urlRestCountr := apiRootRestCountr respCountr, err := http.Get(urlRestCountr) if err !=nil { fmt.Println("Error:we have a problem", err) - return 0, 0,"",0 + return Diagnostics{} } defer respCountr.Body.Close() + //json.NewDecoder(respCountr.Body).Decode(diag.Restcountries) t := time.Now() elapsed := t.Sub(Start) + diag := Diagnostics{respDiag.StatusCode, respCountr.StatusCode, version, elapsed} - - return respDiag.StatusCode, respCountr.StatusCode, version, elapsed + return diag } //Should return the name Norway -func getCountryName(countryName string) (string, string, string) { +func getCountryName(countryCode string) CntrInfo { // Instance of struct - ci := &CntrInfo{} + ci := CntrInfo{} //Change hardcoded to func parameters "We pass them" //countryCode := "NO" //Implement passed parameters - url := apiRoot + countryName + url := apiRoot + countryCode fmt.Println("Dette er vår url",url) //resp is json, so we need to parse it @@ -117,7 +119,7 @@ func getCountryName(countryName string) (string, string, string) { if err !=nil { fmt.Println("Error:we have a problem", err) - return "", "","" + return ci } //Closes when surrounded functions are finished @@ -126,10 +128,10 @@ func getCountryName(countryName string) (string, string, string) { //resp.Body JSON //NewDecoder returns a new decoder that reads from r - json.NewDecoder(resp.Body).Decode(ci) + json.NewDecoder(resp.Body).Decode(&ci) fmt.Println("after json decoder",ci) - return ci.Name, ci.CountryCode, ci.Flag + return ci } // Should return a struct with species and key that has occurrence in a pre defined country @@ -181,8 +183,9 @@ func getSpecies (SpeciesKey string) SpeciesKeyBody { } func getYearForSpecie(SpeciesKey string) SpeciesYear { - const Name = "name" + const Name = "/name" sy:= SpeciesYear{} + // http://api.gbif.org/v1/species/5231190/name url:= apiRootSpecies + SpeciesKey + Name respYear, err:=http.Get(url) if err !=nil {