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

divided into different files

parent 815d8053
No related branches found
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 ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"os"
"strings" "strings"
) )
// Coutry comment endpoint // HandlerNil kjhfkerjhgk
type Country struct { func HandlerNil(w http.ResponseWriter, r *http.Request) {
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.") fmt.Println("Default Handler: Invalid request received.")
http.Error(w, "Invalid request", http.StatusBadRequest) http.Error(w, "Invalid request", http.StatusBadRequest)
} }
func handlerCountry(w http.ResponseWriter, r *http.Request) { func replyRequest(w http.ResponseWriter, r *http.Request, url string, c string) {
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") 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 { if err != nil {
fmt.Println("Error", err) fmt.Println("Error", err)
} }
...@@ -46,24 +29,57 @@ func handlerCountry(w http.ResponseWriter, r *http.Request) { ...@@ -46,24 +29,57 @@ func handlerCountry(w http.ResponseWriter, r *http.Request) {
} }
defer resp.Body.Close() defer resp.Body.Close()
country := &Country{}
switch c {
case "country":
country := &Country{}
json.NewDecoder(resp.Body).Decode(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) {
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)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
} }
http.HandleFunc("/", handlerNil) // HandlerSpecies dfkjekjgh
http.HandleFunc("/conservation/v1/country/", handlerCountry) func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
fmt.Println("Listening on port " + port) APIURL := "http://api.gbif.org/v1/species/"
log.Fatal(http.ListenAndServe(":"+port, nil))
//SJEKK LENGDE 4
parts := strings.Split(r.URL.Path, "/")
APIURL += parts[4]
c := "species"
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