diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000000000000000000000000000000000000..ee235e3066557fc1000b6860cd97286ddf6c3da2 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "assignment1" + "fmt" + "log" + "net/http" + "os" +) + +func main() { + + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + + http.HandleFunc("/", assignment1.HandlerNil) + http.HandleFunc("/conservation/v1/country/", assignment1.HandlerCountry) + http.HandleFunc("/conservation/v1/species/", assignment1.HandlerSpecies) + //http.HandleFunc("/conservation/v1/diag/", assignment1.HandlerDiag) + fmt.Println("Listening on port " + port) + log.Fatal(http.ListenAndServe(":"+port, nil)) + +} diff --git a/func.go b/func.go new file mode 100644 index 0000000000000000000000000000000000000000..d22b547548723222be6e0d39fae4ed19d1a8f981 --- /dev/null +++ b/func.go @@ -0,0 +1,85 @@ +package assignment1 + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" +) + +// 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, 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{} + json.NewDecoder(resp.Body).Decode(species) + json.NewEncoder(w).Encode(species) + } + +} + +// 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) + +} + +// 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] + + c := "species" + + replyRequest(w, r, APIURL, c) + +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..217b00b9d7d412528bc7e0bc9fd6ffa9e8918029 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module assignment1 + +go 1.13 diff --git a/main.go b/main.go deleted file mode 100644 index 2626d0f84b1dbf1e8ca235081346148c55ac3b74..0000000000000000000000000000000000000000 --- a/main.go +++ /dev/null @@ -1,69 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "log" - "net/http" - "os" - "strings" -) - -// Coutry comment endpoint -type Country struct { - Code string `json:"alpha2Code"` - Countryname string `json:"name"` - Countryflag string `json:"flag"` - //Categorycodes string `json:"category"` - //Speciescategorycount string `json:"` -} - -func handlerNil(w http.ResponseWriter, r *http.Request) { - fmt.Println("Default Handler: Invalid request received.") - http.Error(w, "Invalid request", http.StatusBadRequest) -} - -func handlerCountry(w http.ResponseWriter, r *http.Request) { - - APIURL := "https://restcountries.eu/rest/v2/alpha/" - - //SJEKK LENGDE 4 - - 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() - country := &Country{} - - json.NewDecoder(resp.Body).Decode(country) - - json.NewEncoder(w).Encode(country) - -} - -func main() { - - port := os.Getenv("PORT") - if port == "" { - port = "8080" - } - - http.HandleFunc("/", handlerNil) - http.HandleFunc("/conservation/v1/country/", handlerCountry) - fmt.Println("Listening on port " + port) - log.Fatal(http.ListenAndServe(":"+port, nil)) - -} diff --git a/struct.go b/struct.go new file mode 100644 index 0000000000000000000000000000000000000000..dc6d68394499115bd526f6cc12468d5317b100e6 --- /dev/null +++ b/struct.go @@ -0,0 +1,32 @@ +package assignment1 + +// Country dslkjfslkfjl +type Country struct { + Code string `json:"alpha2Code"` + Countryname string `json:"name"` + Countryflag string `json:"flag"` + //Categorycodes string `json:"category"` + //Speciescategorycount string `json:"` +} + +// Species comment endpoint +type Species struct { + Key int `json:"key"` + Kingdom string `json:"kingdom"` + Phylum string `json:"phylum"` + Order string `json:"order"` + Family string `json:"family"` + Genus string `json:"genus"` + //SpeciesName string `json:"genericName"` + ScientificName string `json:"scientificName"` + CanonicalName string `json:"canonicalName"` + //Extinct bool `json:"extinct"` +} + +// Diag comment endpoint +type Diag struct { + Gbif string `json:""` + Restcountries string `json:""` + Version string `json:""` + uptime string `json:""` +}