diff --git a/assignment1 b/assignment1 index df27078391d46be15a3a89340b6fccc8dee0921f..63e8da778e889c6a6bc09365b18ea140fa36efa8 100755 Binary files a/assignment1 and b/assignment1 differ diff --git a/main.go b/main.go index fcf85851f7d3680760085f0e6d1cc22a3a1ffdf2..fba2735cc255f94ff98f9a709c5c459c2706b95f 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "strings" + "time" ) //https://golang.org/doc/articles/wiki/#tmp_4 //Server that prints hello @@ -30,14 +31,11 @@ func urlSplitForCountry(a string)(string, string){ return CountryCode,Limit } -func viewHandler(w http.ResponseWriter, r *http.Request) { - - //Fprintf "F" streams hello into w - //r.URL.Path means request URL Path and send to w for print - //fmt.Fprintf(w, "Hello johannes %q\n", html.EscapeString(r.URL.Path)) +func defaultHandler(w http.ResponseWriter, r *http.Request){ + //code to be added +} - //To avoid having url written on respons page - //fmt.Fprintln(w,"Hello, i'm now using Fprintln instead") +func viewHandler(w http.ResponseWriter, r *http.Request) { //Code to do // should you test for client error or server error 400 / 500 @@ -66,9 +64,22 @@ Species := getSpecies(speciesKey) Year:= getYearForSpecie(speciesKey) // write to web -fmt.Fprintf(w,"Species: %s\n Year: %s\n ", Species, Year) +fmt.Fprintf(w," Species: %v\n Year: %s\n ", Species, Year) + +} + +func diagnosticHandler(w http.ResponseWriter, r *http.Request){ + // /conservation/v1/diag/ + StatGBIF, StatRestC, Version, Time := getDiagnostic() + fmt.Fprintf(w," GBIF API Status: %d\n Rest Countries API Status: %d\n", StatGBIF, StatRestC) + + fmt.Fprintf(w," Version: %s\n UpTime: %s\n", Version, Time) } +// Global var type time +var Start time.Time + +// Main function func main() { port := os.Getenv("PORT") port = "8080" @@ -78,14 +89,22 @@ func main() { log.Fatal("$PORT must be set") } - + Start = time.Now() // we initialize http using viewHandler to handle any req // under the path / remember to end with trailing / + + // Add code to this function + http.HandleFunc("/conservation/v1/", defaultHandler) + http.HandleFunc("/conservation/v1/country/name/", viewHandler) http.HandleFunc("/conservation/v1/species/", speciesHandler) + http.HandleFunc("/conservation/v1/diag/", diagnosticHandler) + + + //LiAnSe listens on TCP net addr, calls serve with handler (nil) // -to handle req on incoming connections, if accept it keeps TCP alive log.Fatal(http.ListenAndServe(":"+port, nil)) diff --git a/webapp.go b/webapp.go index 76f96e7bf356e4839233c3b35b770e94cf51ffdf..f16398c3f722701c92ac1fbb8f051e1d9a72732e 100644 --- a/webapp.go +++ b/webapp.go @@ -4,15 +4,21 @@ import ( "encoding/json" "fmt" "net/http" + "time" ) -//This works see below This https://restcountries.eu/rest/v2/name/ does not const apiRoot = "https://restcountries.eu/rest/v2/alpha/" const apiRootSpeciesInCountry = "http://api.gbif.org/v1/occurrence/search?country=" +const apiRootGBIF ="http://api.gbif.org/" + +const apiRootRestCountr = "https://restcountries.eu/" + const limitSyntax = "&limit=" +const version = "Version 1" + //Root for species by country request const apiRootSpecies = "http://api.gbif.org/v1/species/" @@ -54,6 +60,42 @@ type SpeciesYear struct{ Year string `json:"bracketYear"` } +func getDiagnostic() (int,int,string, time.Duration) { + + /* + "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> + + */ + //res := Response{} + urlGBIF := apiRootGBIF + + respDiag, err := http.Get(urlGBIF) + if err !=nil { + fmt.Println("Error:we have a problem", err) + return 0,0, "",0 + } + defer respDiag.Body.Close() + //json.NewDecoder(respDiag.Body).Decode(ci) + + urlRestCountr := apiRootRestCountr + respCountr, err := http.Get(urlRestCountr) + if err !=nil { + fmt.Println("Error:we have a problem", err) + return 0, 0,"",0 + } + defer respCountr.Body.Close() + + t := time.Now() + elapsed := t.Sub(Start) + + + + return respDiag.StatusCode, respCountr.StatusCode, version, elapsed +} + //Should return the name Norway func getCountryName(countryName string) (string, string, string) {