diff --git a/cmd/main.go b/cmd/main.go index 093cc4a57e7386468e1e5ec1598144f74ff50459..7bf604648e2fa815d000db28400d0c47d05b0ac7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,6 +19,7 @@ func main() { http.HandleFunc("/conservation/v1/country/", assignment1.HandlerCountry) http.HandleFunc("/conservation/v1/species/", assignment1.HandlerSpecies) http.HandleFunc("/conservation/v1/diag/", assignment1.HandlerDiag) + //http.HandleFunc("/conservation/v1/species2/", assignment1.HandlerOnlySpecies) fmt.Println("Listening on port " + port) log.Fatal(http.ListenAndServe(":"+port, nil)) diff --git a/country.go b/country.go index 9638dd7d1027213d6d833b7f4d05393151731c20..a33ed32a7ab90906065afc4f9f7cfc37fe973cb5 100644 --- a/country.go +++ b/country.go @@ -38,7 +38,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { //SJEKK LENGDE 4 parts := strings.Split(r.URL.Path, "/") - 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) @@ -50,3 +49,37 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { replyCountry(w, r, APIURL) } + +/* +// HandlerOnlySpecies skjdfhkshfkl +func HandlerOnlySpecies(w http.ResponseWriter, r *http.Request) { + + APIURL := "http://api.gbif.org/v1/occurrence/search?country=" + + parts := strings.Split(r.URL.Path, "/") + + APIURL += parts[4] + + http.Header.Add(w.Header(), "content-type", "application/json") + + req, err := http.NewRequest(http.MethodGet, APIURL, nil) + if err != nil { + fmt.Println("Error", err) + } + + client := http.DefaultClient + resp, err := client.Do(req) + if err != nil { + fmt.Println("Error", err) + } + + defer resp.Body.Close() + + var species []SpeciesByCountry + //species := []SpeciesByCountry{} + //mySpeciesList := make([]SBCList, 0) + json.NewDecoder(resp.Body).Decode(&species) + json.NewEncoder(w).Encode(species) + +} +*/ diff --git a/diagnostics.go b/diagnostics.go new file mode 100644 index 0000000000000000000000000000000000000000..89b5bed6880002ec30ee701c20dbdc8380cedaa9 --- /dev/null +++ b/diagnostics.go @@ -0,0 +1,33 @@ +package assignment1 + +import ( + "encoding/json" + "fmt" + "net/http" +) + +// HandlerNil kjhfkerjhgk +func HandlerNil(w http.ResponseWriter, r *http.Request) { + fmt.Println("Default Handler: Invalid request received.") + http.Error(w, "Invalid request", http.StatusBadRequest) +} + +// HandlerDiag dsjfbgdfjgb +func HandlerDiag(w http.ResponseWriter, r *http.Request) { + http.Header.Add(w.Header(), "content-type", "application/json") + + diagnostics := &Diag{} + diagnostics.Version = "v1" + diagnostics.Restcountries = getStatusCode("https://restcountries.eu/") + diagnostics.Gbif = getStatusCode("http://api.gbif.org/v1/") + + json.NewEncoder(w).Encode(diagnostics) +} + +func getStatusCode(URL string) int { + resp, err := http.Get(URL) + if err != nil { + panic(err) + } + return resp.StatusCode +} diff --git a/func.go b/func.go deleted file mode 100644 index 6eb5f44e84197e1a2b1485c37db4df88906622ff..0000000000000000000000000000000000000000 --- a/func.go +++ /dev/null @@ -1,152 +0,0 @@ -package assignment1 - -import ( - "fmt" - "net/http" -) - -// HandlerNil kjhfkerjhgk -func HandlerNil(w http.ResponseWriter, r *http.Request) { - fmt.Println("Default Handler: Invalid request received.") - http.Error(w, "Invalid request", http.StatusBadRequest) -} - -/* -func replyRequest(w http.ResponseWriter, r *http.Request, url string, url2 string, c string) { - - http.Header.Add(w.Header(), "content-type", "application/json") - - req, err := http.NewRequest(http.MethodGet, url, nil) - if err != nil { - fmt.Println("Error", err) - } - - client := http.DefaultClient - resp, err := client.Do(req) - if err != nil { - fmt.Println("Error", err) - } - - defer resp.Body.Close() - - switch c { - case "country": - country := &Country{} - json.NewDecoder(resp.Body).Decode(country) - json.NewEncoder(w).Encode(country) - - - - case "species": - species := &Species{} - year := &Year{} - - species.Year = year.Year - - json.NewDecoder(resp.Body).Decode(species) - //both := &Both{species.Key, species.Kingdom, species.Phylum, species.Order, species.Family, species.Genus, species.ScientificName, species.CanonicalName, year.Year} - both := dealWithYear(species, url2) - - json.NewEncoder(w).Encode(year) - } - -} -*/ - -/* - -// HandlerCountry dskjfhskjfhk -func HandlerCountry(w http.ResponseWriter, r *http.Request) { - - APIURL := "https://restcountries.eu/rest/v2/alpha/" - - //SJEKK LENGDE 4 - - parts := strings.Split(r.URL.Path, "/") - - 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) - return - } - - APIURL += parts[4] - - c := "country" - - replyRequest(w, r, APIURL, "", c) - -} -*/ - -/* - -func replySpecies(w http.ResponseWriter, r *http.Request, url string, url2 string) { - - species := &Species{} - - http.Header.Add(w.Header(), "content-type", "application/json") - - req, err := http.NewRequest(http.MethodGet, url, nil) - if err != nil { - fmt.Println("Error", err) - } - - client := http.DefaultClient - resp, err := client.Do(req) - if err != nil { - fmt.Println("Error", err) - } - - defer resp.Body.Close() - - json.NewDecoder(resp.Body).Decode(species) - - replyWithYear(species, url2) - - json.NewEncoder(w).Encode(species) -} - -func replyWithYear(species *Species, url string) { - - req, err := http.NewRequest(http.MethodGet, url, nil) - if err != nil { - fmt.Println("Error", err) - } - - client := http.DefaultClient - resp, err := client.Do(req) - if err != nil { - fmt.Println("Error", err) - } - - defer resp.Body.Close() - - year := &Year{} - - json.NewDecoder(resp.Body).Decode(&year) - - species.Year = year.Year - -} - -// HandlerSpecies dfkjekjgh -func HandlerSpecies(w http.ResponseWriter, r *http.Request) { - APIURL := "http://api.gbif.org/v1/species/" - - //SJEKK LENGDE 4 - - parts := strings.Split(r.URL.Path, "/") - APIURL += parts[4] - - APIURL2 := APIURL + "/name" - - replySpecies(w, r, APIURL, APIURL2) - -} -*/ - -// HandlerDiag dsjfbgdfjgb -func HandlerDiag(w http.ResponseWriter, r *http.Request) { - -} diff --git a/species.go b/species.go index c1189c0fda61b97ec0c8c3bad631d7d9c25faa5e..18102e3e44e615accd50a228190b9018fd33896f 100644 --- a/species.go +++ b/species.go @@ -7,33 +7,35 @@ import ( "strings" ) -func replySpecies(w http.ResponseWriter, r *http.Request, url string, url2 string) { - - species := &Species{} +// HandlerSpecies kdsjfhkdjghlfdkhn +func HandlerSpecies(w http.ResponseWriter, r *http.Request) { http.Header.Add(w.Header(), "content-type", "application/json") - req, err := http.NewRequest(http.MethodGet, url, nil) - if err != nil { - fmt.Println("Error", err) - } + species := &Species{} - client := http.DefaultClient - resp, err := client.Do(req) - if err != nil { - fmt.Println("Error", err) + APIURL := "http://api.gbif.org/v1/species/" + + parts := strings.Split(r.URL.Path, "/") + if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "species" || parts[4] == "" { + status := http.StatusBadRequest + http.Error(w, "Expecting format /conservation/v1/species/'speciesNumber'", status) + return } - defer resp.Body.Close() + APIURL += parts[4] + APIURL2 := APIURL + "/name" - json.NewDecoder(resp.Body).Decode(species) + s := "species" + y := "year" - replyWithYear(species, url2) + replySpecies(w, r, APIURL, s, species) + replySpecies(w, r, APIURL2, y, species) json.NewEncoder(w).Encode(species) } -func replyWithYear(species *Species, url string) { +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 { @@ -48,25 +50,12 @@ func replyWithYear(species *Species, url string) { defer resp.Body.Close() - year := &Year{} - - json.NewDecoder(resp.Body).Decode(&year) - - species.Year = year.Year - -} - -// HandlerSpecies dfkjekjgh -func HandlerSpecies(w http.ResponseWriter, r *http.Request) { - APIURL := "http://api.gbif.org/v1/species/" - - //SJEKK LENGDE 4 - - parts := strings.Split(r.URL.Path, "/") - APIURL += parts[4] - - APIURL2 := APIURL + "/name" - - replySpecies(w, r, APIURL, APIURL2) - + switch str { + case "species": + json.NewDecoder(resp.Body).Decode(&species) + case "year": + year := &Year{} + json.NewDecoder(resp.Body).Decode(&year) + species.Year = year.Year + } } diff --git a/struct.go b/struct.go index 2000a1fb8b1f02edfbe6a7c61eb561393ccff4e3..d1653bc49e31b1143e5578a014420cde64e8b444 100644 --- a/struct.go +++ b/struct.go @@ -9,6 +9,13 @@ type Country struct { //Speciescategorycount string `json:"` } +/* +type SpeciesByCountry struct { + Species string `json:"species"` + SpeciesKey int `json:"speciesKey"` +} +*/ + // Species comment endpoint type Species struct { Key int `json:"key"` @@ -29,8 +36,8 @@ type Year struct { // Diag comment endpoint type Diag struct { - Gbif string `json:""` - Restcountries string `json:""` - Version string `json:""` - uptime string `json:""` + Gbif int + Restcountries int + Version string + uptime int }