//just a small struct used to make it easier to get year
typespecisYearJsonstruct{
Keyfloat64`json:"key"`
}
//struct for json respnce in country method
typecountryJSONstruct{
Codestring`json:"code"`
Countrynamestring`json:"countryname"`
...
...
@@ -36,6 +38,10 @@ type countryJSON struct {
}
//due to stupid implementation by me i had to make an identical struct with an float responce in year, hey if it works it works
// the initial json was made when my understanding of json was poor, something that imporved over time, but i am afraid that trying to improve the json
//to the other type might break it
typespeciesJSON2struct{
Keyfloat64`json:"key"`
Kingdomstring`json:"kingdom"`
...
...
@@ -60,16 +66,20 @@ type speciesJSON struct {
Yearstring`json:"year"`
}
//json struct for diagnose
typediagJSONstruct{
Gbifint`json:"gbif"`
Rescountriesint`json:"rescountries"`
Rescountriesint`json:"restcountries"`
Versionstring`json:"version"`
Uptimefloat64`json:"uptime"`
}
//global variables
var(
StartTime=time.Time{}
StartTime=time.Time{}//for uptime
//filter for url methods
countryFilter="/conservation/v1/country/"
specisFilter="/conservation/v1/species/"
diagFilter="/conservation/v1/diag/"
...
...
@@ -79,40 +89,27 @@ var(
funcmain(){
/*fiks limit
finn ut om vi trenger å lage en json //ja, desverre
lag riktig json for country/specis/diag
inkluder år i species
finn ut hvordan man finner diag informationen //ping something
inkluder specis i country
remove dupes
//optional
add error code handinling
add custom 404
add rate limiting
startTime := time.Now()
*/
// add switch here
//starts the uptime counter
StartTime=time.Now()
//to handle random / and undefined behavior
http.HandleFunc("/",undefined)
//handels the functions based on user input, the 2. parameter is the global string used for filtering
http.HandleFunc(countryFilter,country)
http.HandleFunc(specisFilter,species)
http.HandleFunc(diagFilter,diag)
//tries to get the port from the port number from the env file
port:=os.Getenv("PORT")
//if it is unable to find the port variable it forces one
keyTest:=fmt.Sprintf("%f",spectest.ArryJson[i].Key)//gets the key
ifstrings.Contains(duplicateCheck,keyTest){//check if key already exists, go out of loop if it does
continue
}
duplicateCheck+=keyTest
duplicateCheck+=keyTest+","
key:=spectest.ArryJson[i].Key
...
...
@@ -257,15 +274,7 @@ func country(w http.ResponseWriter, r *http.Request){
forindex,element:=rangecountryJsonArray{
fmt.Println(index,"=>",element)
}
iferr!=nil{
print("error\n")
log.Fatalln(err)
}
deferresp.Body.Close()
body,err:=ioutil.ReadAll(resp.Body)
...
...
@@ -276,27 +285,21 @@ func country(w http.ResponseWriter, r *http.Request){
}
varinfomap[string]interface{}
varinfomap[string]interface{}//as mentioned in the start of the document this is my bad implementation of json, but since it works im afraid to fix it properly to the other
//implementation.
json.Unmarshal([]byte(body),&info)
//initalises temp variables and set their default values incase something goes wrong
//todo make this work properly
/* tempCode := "error, unable to locate code"
tempCountryname := "error, unable to locate countryname"
tempCountryFlag := "error, unable to locate countryflag"
tempSpecies := "error, unable to locate specis"
tempSpeciesKey := "error, unable to locate specis key"
*/
tempCode:=info["alpha2Code"]
tempCountryname:=info["name"]
tempCountryFlag:=info["flag"]
//tempSpecies := "work in progress " //todo
//tempSpeciesKey := "work in progress " //todo
//this is so i can collapse this test //todo fix species and key test
//this is so i can collapse this test /
{
iftempCode==nil{
tempCode="NA"
...
...
@@ -321,149 +324,191 @@ func country(w http.ResponseWriter, r *http.Request){
resp,err:=http.Get("http://api.gbif.org/v1/species/"+filtred)//runs api call with that paramter
ifresp.StatusCode==404{
io.WriteString(w,"404")//if input is invalid we do an 404 check and writes that out on the page if that is the case
iferr!=nil{
print("error\n")
log.Fatalln(err)
}else{
}
deferresp.Body.Close()
body,err:=ioutil.ReadAll(resp.Body)
iferr!=nil{
print("error\n")
log.Fatalln(err)
iferr!=nil{
print("error\n")
log.Fatalln(err)
}
}
deferresp.Body.Close()
body,err:=ioutil.ReadAll(resp.Body)
varinfomap[string]interface{}
json.Unmarshal([]byte(body),&info)
iferr!=nil{
print("error\n")
log.Fatalln(err)
}
varinfomap[string]interface{}//this is an shit way of doing json, it works for easier structures but not with nested, which is the reasons i go away from it later on
json.Unmarshal([]byte(body),&info)
tempKey:=info["key"]
tempNameKey:=info["nameKey"]
tempKingdom:=info["kingdom"]
tempPhylum:=info["phylum"]
tempOrder:=info["order"]
tempFamily:=info["family"]
tempGenus:=info["genus"]
tempScientificName:=info["scientificName"]
tempCanonicalName:=info["canonicalName"]
varnamekeyIntint=int(tempNameKey.(float64))
//this is so i can collapse this test
{
iftempKey==nil{
tempKey="NA"
}
iftempKingdom==nil{
tempKingdom="NA"
}
iftempPhylum==nil{
tempPhylum="NA"
}
iftempFamily==nil{
tempFamily="NA"
}
iftempGenus==nil{
tempGenus="NA"
}
iftempScientificName==nil{
tempScientificName="NA"
}
iftempCanonicalName==nil{
tempCanonicalName="NA"
}
ifnamekeyInt==0{
namekeyInt=0
}
}
tempKey:=info["key"]
tempNameKey:=info["nameKey"]
tempKingdom:=info["kingdom"]
tempPhylum:=info["phylum"]
tempOrder:=info["order"]
tempFamily:=info["family"]
tempGenus:=info["genus"]
tempScientificName:=info["scientificName"]
tempCanonicalName:=info["canonicalName"]
respYear,err:=http.Get("http://api.gbif.org/v1/species/"+strconv.Itoa(namekeyInt)+"/name")//uses the namekey from the species call to find year in /name
iferr!=nil{
print("error\n")
log.Fatalln(err)
//this is so i can collapse this test //todo fix year test
{
iftempKey==nil{
tempKey="NA"
}
iftempKingdom==nil{
tempKingdom="NA"
}
iftempPhylum==nil{
tempPhylum="NA"
}
iftempFamily==nil{
tempFamily="NA"
}
iftempGenus==nil{
tempGenus="NA"
}
iftempScientificName==nil{
tempScientificName="NA"
}
iftempCanonicalName==nil{
tempCanonicalName="NA"
}
deferrespYear.Body.Close()
bodyYear,err:=ioutil.ReadAll(respYear.Body)
iferr!=nil{
print("error\n")
log.Fatalln(err)
}
varinfoYearmap[string]interface{}
json.Unmarshal([]byte(bodyYear),&infoYear)//again this poor implementation of json, but it works
gbifCode:=0//variable to hold responce code from gbif api
rescountriesCode:=0//variable to hold responce code from restcountries api
//todo add year check
version:=runtime.Version()//get current go version
serverRuntime:=time.Since(StartTime).Seconds()//global variable introdused, it starts counting when main is ran, and calculates time since that happend
serverRuntime=math.Round(serverRuntime*100)/100//makes it into secounds with 2 decimales, change the rounding to make it into pure secounds. i prefered it with 2 decimales