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

Initial commit

parent aab3924c
package main
import (
"assignemnt1"
internal "assignment2"
"fmt"
"log"
"net/http"
......@@ -15,13 +15,14 @@ func main() {
port = "8080"
}
http.HandleFunc("/conservation/v1/country/", internal.HandlerCountry)
http.HandleFunc("/conservation/v1/species/", internal.HandlerSpecies)
http.HandleFunc("/conservation/v1/diag/", internal.HandlerDiagnostics)
http.HandleFunc("/", internal.HandlerHome)
http.HandleFunc("/corona/v1/country/", internal.HandlerCountry)
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 )
err := http.ListenAndServe(":" + port, nil)
fmt.Println("Running on port ", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal(err.Error())
}
......
module assignemnt1
module assignment2
go 1.13
package internal
import (
"net/http"
"encoding/json"
"strings"
"time"
"fmt"
"net/http"
"time"
)
// 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
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)
......@@ -67,88 +23,21 @@ func DoRequest(c* http.Client, requestURL string) *http.Response{
return resp
}
// HandlerCountry handler for country and its species
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)
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)
// Diagnosis holds status codes of the services, version, and uptime
type Diagnosis struct {
Mmedia int
Oxford int
Version string
Uptime int
}
// 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 {
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)
}
// Version states current version
var Version = "v1"
var startTime time.Time
// 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"
countryURL := "https://restcountries.eu/rest/v2/alpha/no"
......@@ -162,12 +51,12 @@ func HandlerDiagnostics(w http.ResponseWriter, r* http.Request){
output := Diagnosis{gbifStatus, countriesStatus, Version, uptime}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output)
}
// 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)
......@@ -183,4 +72,12 @@ func DoDiagnosticRequest(c* http.Client, requestURL string) int{
defer resp.Body.Close()
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