Commit 33865697 authored by Fredrik's avatar Fredrik
Browse files

First draft, comments need to be cleaned up but code is functional

parents
Pipeline #568 canceled with stages
package internal
import (
"fmt"
"net/http"
"encoding/json"
"strings"
"time"
)
// Version states latest version
var Version = "v1"
// Country yes haha
type Country struct{
Code string `json:"alpha2code"`
Countryname string `json:"name"`
CountryFlag string `json:"flag"`
Species []string `json:"results"`
SpeciesKey []string `json:"facets"`
}
// Species TODO
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"`
ScientificName string `json:"scientificName"`
CanonicalName string `json:"canonicalName"`
Year string `json:"year"`
}
// Diagnosis is like diogenes but not
type Diagnosis struct {
Gbif int
Restcountries int
Version string
Uptime int
}
// DoRequest does request
func DoRequest(c* http.Client, requestURL string) *http.Response{
r, e := http.NewRequest(http.MethodGet, requestURL, nil)
if e != nil {
fmt.Print("||||||||||||||||||| ERROR FORMING REQUEST |||||||||||||||||||||||||||")
panic(e)
}
resp, err := c.Do(r)
if err != nil {
fmt.Print("||||||||||||||||||| ERROR DOING HTML STUFF |||||||||||||||||||||||||||")
panic(err)
}
defer resp.Body.Close()
return resp
}
// http://localhost:8080/conservation/v1/country/no?limit=2
// HandlerCountry yeehwaw
func HandlerCountry(w http.ResponseWriter, r* http.Request){
occurrenceAPIURL := "http://api.gbif.org/v1/occurrence/search?"
countryAPIURL := "https://restcountries.eu/rest/v2/alpha/"
rawInput := strings.Split(r.URL.Path, "/")
occInput := occurrenceAPIURL + "country=" + rawInput[4] + "&limit=" + r.URL.Query().Get("limit")
countryInput := countryAPIURL + rawInput[4]
var output Country
client := http.DefaultClient
resp := DoRequest(client, occInput)
err := json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
fmt.Print("||||||||||||||||||| ERROR PARSING JSON AS OCCURRENCE |||||||||||||||||||||||||||")
panic(err)
}
resp = DoRequest(client, countryInput)
err = json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
fmt.Print("||||||||||||||||||| ERROR PARSING JSON AS COUNTRY |||||||||||||||||||||||||||")
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output)
}
// HandlerSpecies 5817566
func HandlerSpecies(w http.ResponseWriter, r* http.Request){
speciesAPIURL := "http://api.gbif.org/v1/species/"
rawInput := strings.Split(r.URL.Path, "/")
input := rawInput[4]
speciesURL := speciesAPIURL + input
var output Species
client := http.DefaultClient
resp := DoRequest(client, speciesURL)
err := json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
fmt.Print("||||||||||||||||||| ERROR PARSING JSON AS SPECIES |||||||||||||||||||||||||||")
panic(err)
}
speciesURL += "/name"
resp = DoRequest(client, speciesURL)
err = json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
fmt.Print("||||||||||||||||||| ERROR PARSING JSON AS SPECIES |||||||||||||||||||||||||||")
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output)
}
var startTime time.Time //Global variabel. Deklareres i fila du skal regne ut uptime i
// HandlerDiagnostics handler diager
func HandlerDiagnostics(w http.ResponseWriter, r* http.Request){
speciesURL := "http://api.gbif.org/v1/species/5817566"
//occurrenceURL := "http://api.gbif.org/v1/occurrence/search?country=no&limit=1"
countryURL := "https://restcountries.eu/rest/v2/alpha/no"
client := http.DefaultClient
gbifStatus := DoDiagnosticRequest(client, speciesURL)
countriesStatus := DoDiagnosticRequest(client, countryURL)
uptime := int(time.Since(startTime).Seconds()) //Regner ut uptime og caster om til int
output := Diagnosis{gbifStatus, countriesStatus, Version, uptime}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output)
/*
gbif int
restcountries int
version string
uptime int
"gbif": "<http status code for GBIF API>",
"restcountries": "<http status code for restcountries API>",
"version": "v1",
"uptime": <time in seconds from the last service restart>
*/
}
// DoDiagnosticRequest does request, but returns http status if things go wrong
func DoDiagnosticRequest(c* http.Client, requestURL string) int{
r, e := http.NewRequest(http.MethodGet, requestURL, nil)
if e != nil {
fmt.Print("||||||||||||||||||| ERROR FORMING REQUEST |||||||||||||||||||||||||||")
panic(e)
}
resp, err := c.Do(r)
if err != nil {
return -1
}
defer resp.Body.Close()
return resp.StatusCode
}
\ No newline at end of file
package main
import (
"./internal"
"fmt"
"log"
"net/http"
"os"
)
/* the three genders
/conservation/v1/country/
/conservation/v1/species/
/conservation/v1/diag/
*/
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/conservation/v1/country/", internal.HandlerCountry)
http.HandleFunc("/conservation/v1/species/", internal.HandlerSpecies)
http.HandleFunc("/conservation/v1/diag/", internal.HandlerDiagnostics)
fmt.Println("Running on port ", port )
err := http.ListenAndServe(":" + port, nil)
if err != nil {
fmt.Print("||||||||||||||| PROBLEM IN MAIN |||||||||||||||")
log.Fatal(err.Error())
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment