Skip to content
Snippets Groups Projects
Commit 5be4f0af authored by andmag's avatar andmag
Browse files

diag added, optimalization

parent 1c48f086
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ func main() { ...@@ -19,6 +19,7 @@ func main() {
http.HandleFunc("/conservation/v1/country/", assignment1.HandlerCountry) http.HandleFunc("/conservation/v1/country/", assignment1.HandlerCountry)
http.HandleFunc("/conservation/v1/species/", assignment1.HandlerSpecies) http.HandleFunc("/conservation/v1/species/", assignment1.HandlerSpecies)
http.HandleFunc("/conservation/v1/diag/", assignment1.HandlerDiag) http.HandleFunc("/conservation/v1/diag/", assignment1.HandlerDiag)
//http.HandleFunc("/conservation/v1/species2/", assignment1.HandlerOnlySpecies)
fmt.Println("Listening on port " + port) fmt.Println("Listening on port " + port)
log.Fatal(http.ListenAndServe(":"+port, nil)) log.Fatal(http.ListenAndServe(":"+port, nil))
......
...@@ -38,7 +38,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { ...@@ -38,7 +38,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
//SJEKK LENGDE 4 //SJEKK LENGDE 4
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] == "" { 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)
...@@ -50,3 +49,37 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { ...@@ -50,3 +49,37 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
replyCountry(w, r, APIURL) 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)
}
*/
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
}
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) {
}
...@@ -7,33 +7,35 @@ import ( ...@@ -7,33 +7,35 @@ import (
"strings" "strings"
) )
func replySpecies(w http.ResponseWriter, r *http.Request, url string, url2 string) { // HandlerSpecies kdsjfhkdjghlfdkhn
func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
species := &Species{}
http.Header.Add(w.Header(), "content-type", "application/json") http.Header.Add(w.Header(), "content-type", "application/json")
req, err := http.NewRequest(http.MethodGet, url, nil) species := &Species{}
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient APIURL := "http://api.gbif.org/v1/species/"
resp, err := client.Do(req)
if err != nil { parts := strings.Split(r.URL.Path, "/")
fmt.Println("Error", err) 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) 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) req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil { if err != nil {
...@@ -48,25 +50,12 @@ func replyWithYear(species *Species, url string) { ...@@ -48,25 +50,12 @@ func replyWithYear(species *Species, url string) {
defer resp.Body.Close() defer resp.Body.Close()
year := &Year{} switch str {
case "species":
json.NewDecoder(resp.Body).Decode(&year) json.NewDecoder(resp.Body).Decode(&species)
case "year":
species.Year = year.Year 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)
} }
...@@ -9,6 +9,13 @@ type Country struct { ...@@ -9,6 +9,13 @@ type Country struct {
//Speciescategorycount string `json:"` //Speciescategorycount string `json:"`
} }
/*
type SpeciesByCountry struct {
Species string `json:"species"`
SpeciesKey int `json:"speciesKey"`
}
*/
// Species comment endpoint // Species comment endpoint
type Species struct { type Species struct {
Key int `json:"key"` Key int `json:"key"`
...@@ -29,8 +36,8 @@ type Year struct { ...@@ -29,8 +36,8 @@ type Year struct {
// Diag comment endpoint // Diag comment endpoint
type Diag struct { type Diag struct {
Gbif string `json:""` Gbif int
Restcountries string `json:""` Restcountries int
Version string `json:""` Version string
uptime string `json:""` uptime int
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment