Commit baadf3a8 authored by Fredrik Lønnemo's avatar Fredrik Lønnemo
Browse files

Initial commit

parent aab3924c
package main package main
import ( import (
"assignemnt1" internal "assignment2"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
...@@ -15,13 +15,14 @@ func main() { ...@@ -15,13 +15,14 @@ func main() {
port = "8080" port = "8080"
} }
http.HandleFunc("/conservation/v1/country/", internal.HandlerCountry) http.HandleFunc("/", internal.HandlerHome)
http.HandleFunc("/conservation/v1/species/", internal.HandlerSpecies) http.HandleFunc("/corona/v1/country/", internal.HandlerCountry)
http.HandleFunc("/conservation/v1/diag/", internal.HandlerDiagnostics) http.HandleFunc("/corona/v1/policy/", internal.HandlerPolicy)
http.HandleFunc("/corona/v1/diag/", internal.HandlerDiagnostics)
http.HandleFunc("/corona/v1/notifications/", internal.HandlerNotifications)
fmt.Println("Running on port ", port)
fmt.Println("Running on port ", port ) err := http.ListenAndServe(":"+port, nil)
err := http.ListenAndServe(":" + port, nil)
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
......
module assignemnt1 module assignment2
go 1.13 go 1.13
package internal package internal
import ( import (
"net/http"
"encoding/json" "encoding/json"
"strings" "net/http"
"time" "time"
"fmt"
) )
// Version states current version
var Version = "v1"
// Country struct holds data for a country and its species, not done
type Country struct{
Code string `json:"alpha2code"`
Countryname string `json:"name"`
CountryFlag string `json:"flag"`
Species []InnerName `json:"results"`
SpeciesKey []InnerKey `json:"results"`
}
type InnerName struct{
Name string `json:"species"`
}
type InnerKey struct{
Key int `json:"key"`
}
// Species holds the data of a species
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 holds status codes of the services, version, and uptime
type Diagnosis struct {
Gbif int
Restcountries int
Version string
Uptime int
}
// DoRequest performs an http request and returns the response // DoRequest performs an http request and returns the response
func DoRequest(c* http.Client, requestURL string) *http.Response{ func DoRequest(c *http.Client, requestURL string) *http.Response {
r, e := http.NewRequest(http.MethodGet, requestURL, nil) r, e := http.NewRequest(http.MethodGet, requestURL, nil)
...@@ -67,88 +23,21 @@ func DoRequest(c* http.Client, requestURL string) *http.Response{ ...@@ -67,88 +23,21 @@ func DoRequest(c* http.Client, requestURL string) *http.Response{
return resp return resp
} }
// HandlerCountry handler for country and its species // Diagnosis holds status codes of the services, version, and uptime
func HandlerCountry(w http.ResponseWriter, r* http.Request){ type Diagnosis struct {
occurrenceAPIURL := "http://api.gbif.org/v1/occurrence/search?" Mmedia int
Oxford int
countryAPIURL := "https://restcountries.eu/rest/v2/alpha/" Version string
Uptime int
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)
fmt.Print(resp.Body)
err := json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
panic(err)
}
defer resp.Body.Close()
resp = DoRequest(client, countryInput)
err = json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
panic(err)
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output)
} }
// HandlerSpecies 5817566 // Version states current version
func HandlerSpecies(w http.ResponseWriter, r* http.Request){ var Version = "v1"
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 {
panic(err)
}
defer resp.Body.Close()
speciesURL += "/name"
resp = DoRequest(client, speciesURL)
err = json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
panic(err)
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output)
}
var startTime time.Time var startTime time.Time
// HandlerDiagnostics handler diager // HandlerDiagnostics handler diager
func HandlerDiagnostics(w http.ResponseWriter, r* http.Request){ func HandlerDiagnostics(w http.ResponseWriter, r *http.Request) {
speciesURL := "http://api.gbif.org/v1/species/5817566" speciesURL := "http://api.gbif.org/v1/species/5817566"
countryURL := "https://restcountries.eu/rest/v2/alpha/no" countryURL := "https://restcountries.eu/rest/v2/alpha/no"
...@@ -162,12 +51,12 @@ func HandlerDiagnostics(w http.ResponseWriter, r* http.Request){ ...@@ -162,12 +51,12 @@ func HandlerDiagnostics(w http.ResponseWriter, r* http.Request){
output := Diagnosis{gbifStatus, countriesStatus, Version, uptime} output := Diagnosis{gbifStatus, countriesStatus, Version, uptime}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output) json.NewEncoder(w).Encode(output)
} }
// DoDiagnosticRequest does request, but returns http status, even if things go wrong // DoDiagnosticRequest does request, but returns http status, even if things go wrong
func DoDiagnosticRequest(c* http.Client, requestURL string) int{ func DoDiagnosticRequest(c *http.Client, requestURL string) int {
r, e := http.NewRequest(http.MethodGet, requestURL, nil) r, e := http.NewRequest(http.MethodGet, requestURL, nil)
...@@ -183,4 +72,12 @@ func DoDiagnosticRequest(c* http.Client, requestURL string) int{ ...@@ -183,4 +72,12 @@ func DoDiagnosticRequest(c* http.Client, requestURL string) int{
defer resp.Body.Close() defer resp.Body.Close()
return resp.StatusCode return resp.StatusCode
} }
\ No newline at end of file
func HandlerHome(w http.ResponseWriter, r *http.Request) {
output := "welcome to assignment 2"
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output)
}
package internal
import (
//"encoding/json"
"net/http"
)
// Country struct holds data for a country
type Country struct {
// todo
}
// *Iunno if we need a policy struct
type Policy struct {
// todo
}
func HandlerCountry(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not implemented", 501)
}
func HandlerPolicy(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not implemented", 501)
}
package internal
import (
//"encoding/json"
"net/http"
)
func HandlerNotifications(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not implemented", 501)
}
if you're reading this i forgot to update the readme
\ No newline at end of file
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