Skip to content
Snippets Groups Projects
Commit 35d288ad authored by Vladimir Haug's avatar Vladimir Haug
Browse files

Delete v1

parent 80c4d29e
No related branches found
No related tags found
No related merge requests found
package main
import (
"fmt"
"html"
"log"
"net/http"
"os"
"io/ioutil"
"strings"
"encoding/json"
// "time"
)
const COUNTRIES = "https://restcountries.eu/rest/v2/alpha/"
const SPECIES = "http://api.gbif.org/v1/species/"
const OCCURENCE = "http://api.gbif.org/v1/occurrence/search?publishingCountry="
type Country struct {
Code string `json:"alpha2Code"`
CountryName string `json:"name"`
CountryFlag string `json:"flag"`
Species string `json:"species"`
SpeciesKey string `json:"speciesKey"`
}
type Species struct {
Key int `json:"key"`
Kingdom string `json:"kingdom"`
Phylum string `json:"phylum"`
Family string `json:"family"`
Genus string `json:"genus"`
ScientificName string `json:"scientificName"`
CanonicalName string `json:"canonicalName"`
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}
func countryHandler(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/conservation/v1/country/")
var1 := parts[1]
url := COUNTRIES + var1
response, err := http.Get(url)
body, readErr := ioutil.ReadAll(response.Body)
if response.StatusCode >= 400 && response.StatusCode <= 499 {
fmt.Fprintf(w, "Country code not found")
}
if readErr != nil {
log.Fatal(readErr)
}
Country1 := Country{}
jsonErr := json.Unmarshal(body, &Country1)
if jsonErr != nil {
log.Fatal(jsonErr)
}
fmt.Fprintf(w, Country1.CountryName + "\n" + Country1.Code + "\n" + Country1.CountryFlag + "\n" + Country1.Species + "\n")
if err != nil {
fmt.Fprintf(w, "Dårlig")
} else {
cData, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(cData))
}
}
func speciesHandler(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/conservation/v1/species/")
// fmt.Fprintf(w, "Se om det funker", parts)
var1 := parts[1]
url := SPECIES + var1
response, err := http.Get(url)
body, readErr := ioutil.ReadAll(response.Body)
if response.StatusCode >= 400 && response.StatusCode <= 499 {
fmt.Fprintf(w, "Species code not found")
}
if readErr != nil {
log.Fatal(readErr)
}
Species1 := Species{}
jsonErr := json.Unmarshal(body, &Species1)
if jsonErr != nil {
log.Fatal(jsonErr)
}
fmt.Fprintf(w, "%d" + "\n" + Species1.Kingdom + "\n" + Species1.Phylum + "\n" + Species1.Family + "\n" + Species1.Genus + "\n" + Species1.ScientificName + "\n" + Species1.CanonicalName, Species1.Key)
if err != nil {
fmt.Println("Dårlig")
} else {
cData, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(cData))
}
}
func diagHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Her blir det tull", html.EscapeString(r.URL.Path))
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
http.HandleFunc("/", helloHandler)
http.HandleFunc("/conservation/v1/country/", countryHandler)
http.HandleFunc("/conservation/v1/species/", speciesHandler)
http.HandleFunc("/conservation/v1/diag/", diagHandler)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment