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

divided into different files

parent 815d8053
Branches
No related tags found
No related merge requests found
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))
}
package main
package assignment1
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) {
// 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 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]
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, APIURL, nil)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
......@@ -46,24 +29,57 @@ func handlerCountry(w http.ResponseWriter, r *http.Request) {
}
defer resp.Body.Close()
country := &Country{}
json.NewDecoder(resp.Body).Decode(country)
switch c {
case "country":
country := &Country{}
json.NewDecoder(resp.Body).Decode(country)
json.NewEncoder(w).Encode(country)
json.NewEncoder(w).Encode(country)
case "species":
species := &Species{}
json.NewDecoder(resp.Body).Decode(species)
json.NewEncoder(w).Encode(species)
}
}
func main() {
// HandlerCountry dskjfhskjfhk
func HandlerCountry(w http.ResponseWriter, r *http.Request) {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
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"
http.HandleFunc("/", handlerNil)
http.HandleFunc("/conservation/v1/country/", handlerCountry)
fmt.Println("Listening on port " + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
replyRequest(w, r, APIURL, c)
}
go.mod 0 → 100644
module assignment1
go 1.13
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:""`
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment