Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
ASSIGNMENT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Herman Andersen Dyrkorn
ASSIGNMENT
Commits
0350ff2a
Commit
0350ff2a
authored
5 years ago
by
Herman Andersen Dyrkorn
Browse files
Options
Downloads
Patches
Plain Diff
commenting an chaging some errors
parent
eb03b69d
No related branches found
No related tags found
No related merge requests found
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
cmd/main.go
+2
-1
2 additions, 1 deletion
cmd/main.go
country.go
+19
-8
19 additions, 8 deletions
country.go
diagnostics.go
+8
-1
8 additions, 1 deletion
diagnostics.go
functions.go
+6
-4
6 additions, 4 deletions
functions.go
species.go
+13
-5
13 additions, 5 deletions
species.go
timer.go
+3
-0
3 additions, 0 deletions
timer.go
with
51 additions
and
19 deletions
cmd/main.go
+
2
−
1
View file @
0350ff2a
...
...
@@ -9,8 +9,9 @@ import (
)
func
main
()
{
//start
clock for diagnostics
//start
timer
assignment1
.
InitTime
()
//trying to get portnumber
port
:=
os
.
Getenv
(
"PORT"
)
if
port
==
""
{
port
=
"8080"
...
...
This diff is collapsed.
Click to expand it.
country.go
+
19
−
8
View file @
0350ff2a
...
...
@@ -2,7 +2,6 @@ package assignment1
import
(
"encoding/json"
"fmt"
"net/http"
"strings"
)
...
...
@@ -12,25 +11,34 @@ func CountryHandler(w http.ResponseWriter, r *http.Request) {
http
.
Header
.
Add
(
w
.
Header
(),
"content-type"
,
"application/json"
)
APIURL
:=
"https://restcountries.eu/rest/v2/alpha/"
//gets the query from the url the user writes
urlQuery
:=
r
.
URL
.
RawQuery
//splitting the url path that the user writes
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
//checking if the path is right
if
len
(
parts
)
!=
5
||
parts
[
1
]
!=
"conservation"
||
parts
[
2
]
!=
"v1"
||
parts
[
3
]
!=
"country"
||
parts
[
4
]
==
""
{
status
:=
http
.
StatusBadRequest
http
.
Error
(
w
,
"Expecting format /conservation/v1/country/AlphaCode?limit='integer'"
,
status
)
return
}
APIURL
+=
parts
[
4
]
//setting up a client and a empty country strct
client
:=
http
.
DefaultClient
APIURL
+=
parts
[
4
]
country
:=
&
Country
{}
//do a request on the APIURL and decode the response to the countrystruct
resp
:=
GetTheRequest
(
APIURL
,
client
)
country
:=
&
Country
{}
DecodeCountry
(
country
,
resp
,
w
)
//creating a new URL to get species and species keys
NEWURL
:=
"http://api.gbif.org/v1/occurrence/search?country="
+
country
.
Alpha2Code
+
"&"
+
urlQuery
//do a request on the new url and decodes the response into the countrystruct
resp2
:=
GetTheRequest
(
NEWURL
,
client
)
DecodeKeyAndSpecies
(
country
,
resp2
)
//encode countrystruct back to the user
json
.
NewEncoder
(
w
)
.
Encode
(
country
)
}
...
...
@@ -38,8 +46,9 @@ func CountryHandler(w http.ResponseWriter, r *http.Request) {
//DecodeCountry function
func
DecodeCountry
(
country
*
Country
,
resp
*
http
.
Response
,
w
http
.
ResponseWriter
)
{
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
country
)
if
country
.
Alpha2Code
==
""
{
//checking if there is any errors from the decoding
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
country
)
if
err
!=
nil
{
status
:=
http
.
StatusBadRequest
http
.
Error
(
w
,
"Expected a country, got nothing"
,
status
)
return
...
...
@@ -49,19 +58,21 @@ func DecodeCountry(country *Country, resp *http.Response, w http.ResponseWriter)
//DecodeKeyAndSpecies function
func
DecodeKeyAndSpecies
(
country
*
Country
,
resp
*
http
.
Response
)
{
var
speciesKeyAndSpecies
=
&
Results
{}
//creating a empty resultstruct and decodes it
speciesKeyAndSpecies
:=
&
Results
{}
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
speciesKeyAndSpecies
)
var
species
[]
string
var
spesiesKey
[]
int
for
k
,
v
:=
range
speciesKeyAndSpecies
.
Result
{
f
mt
.
Println
(
k
)
//loop that puts all the decoded values into two arrays that contains keys and speciesname
f
or
_
,
v
:=
range
speciesKeyAndSpecies
.
Result
{
species
=
append
(
species
,
v
.
Specie
)
spesiesKey
=
append
(
spesiesKey
,
v
.
Key
)
}
//copy keys and speciesname into country
country
.
SpeciesKey
=
spesiesKey
country
.
Species
=
species
...
...
This diff is collapsed.
Click to expand it.
diagnostics.go
+
8
−
1
View file @
0350ff2a
...
...
@@ -10,27 +10,34 @@ import (
func
DiagHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
http
.
Header
.
Add
(
w
.
Header
(),
"content-type"
,
"application/json"
)
//splitting the path into smaller parts
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
//checks that the path is right
if
len
(
parts
)
!=
5
||
parts
[
1
]
!=
"conservation"
||
parts
[
2
]
!=
"v1"
||
parts
[
3
]
!=
"diag"
{
status
:=
http
.
StatusBadRequest
http
.
Error
(
w
,
"Expecting format /conservation/v1/diag/"
,
status
)
return
}
//creating a diagnostics struct
diag
:=
&
Diag
{}
//filling the diagstruct with info
diag
.
Uptime
=
Uptime
()
diag
.
Version
=
"V1"
diag
.
RestCountry
=
GetStatusCode
(
"https://restcountries.eu"
)
diag
.
GBIF
=
GetStatusCode
(
"http://api.gbif.org/v1/"
)
//encode the struct back to the user
json
.
NewEncoder
(
w
)
.
Encode
(
diag
)
}
//GetStatusCode function
func
GetStatusCode
(
URL
string
)
int
{
//getrequest on a given url
resp
,
err
:=
http
.
Get
(
URL
)
if
err
!=
nil
{
panic
(
err
)
}
//returning the statuscode of the response
return
resp
.
StatusCode
}
This diff is collapsed.
Click to expand it.
functions.go
+
6
−
4
View file @
0350ff2a
...
...
@@ -4,21 +4,23 @@ import (
"net/http"
)
//GetTheRequest function
//GetTheRequest function
returns a respons from the api
func
GetTheRequest
(
URL
string
,
client
*
http
.
Client
)
*
http
.
Response
{
//do a request on the url
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
URL
,
nil
)
if
err
!=
nil
{
panic
(
err
)
}
//getting response from the client
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
panic
(
err
)
}
//returning the response
return
resp
}
//HandlerNil function
//HandlerNil function
that deals with the / endpoint
func
HandlerNil
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
//fmt.Println("Default Handler: Invalid request received.")
http
.
Error
(
w
,
"Invalid request"
,
http
.
StatusBadRequest
)
http
.
Error
(
w
,
"Invalid request, expected formating conservation/v1/diag or country or species"
,
http
.
StatusBadRequest
)
}
This diff is collapsed.
Click to expand it.
species.go
+
13
−
5
View file @
0350ff2a
...
...
@@ -11,26 +11,35 @@ func SpeciesHandler(w http.ResponseWriter, r *http.Request) {
http
.
Header
.
Add
(
w
.
Header
(),
"content-type"
,
"application/json"
)
APIURL
:=
"http://api.gbif.org/v1/species/"
//splitting the path into smaller parts
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
//checks that the path is right
if
len
(
parts
)
!=
5
||
parts
[
1
]
!=
"conservation"
||
parts
[
2
]
!=
"v1"
||
parts
[
3
]
!=
"species"
||
parts
[
4
]
==
""
{
status
:=
http
.
StatusBadRequest
http
.
Error
(
w
,
"Expecting format /conservation/v1/species/'Spiciesnumber'"
,
status
)
return
}
APIURL
+=
parts
[
4
]
client
:=
http
.
DefaultClient
//setting up a client and does a response
client
:=
http
.
DefaultClient
resp
:=
GetTheRequest
(
APIURL
,
client
)
//appending "name" to the APRURL and does a new response
APIURL
+=
"/name"
resp2
:=
GetTheRequest
(
APIURL
,
client
)
//creating a species struct and a speciesyear struct
species
:=
&
Species
{}
year
:=
&
SpeciesYear
{}
//decode species and speciesyear
DecodeSpecies
(
species
,
resp
,
w
)
DecodeYear
(
resp2
,
year
)
//copy over year into speciesstruct and encode it back to user
species
.
Year
=
year
.
Year
json
.
NewEncoder
(
w
)
.
Encode
(
species
)
...
...
@@ -38,9 +47,9 @@ func SpeciesHandler(w http.ResponseWriter, r *http.Request) {
//DecodeSpecies function
func
DecodeSpecies
(
species
*
Species
,
resp
*
http
.
Response
,
w
http
.
ResponseWriter
)
{
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
species
)
if
species
.
Key
==
0
{
//checking if there is any errors from the decoding
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
species
)
if
err
!=
nil
{
status
:=
http
.
StatusBadRequest
http
.
Error
(
w
,
"Expected a species, got nothing"
,
status
)
return
...
...
@@ -50,6 +59,5 @@ func DecodeSpecies(species *Species, resp *http.Response, w http.ResponseWriter)
//DecodeYear function
func
DecodeYear
(
resp
*
http
.
Response
,
year
*
SpeciesYear
)
{
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
year
)
}
This diff is collapsed.
Click to expand it.
timer.go
+
3
−
0
View file @
0350ff2a
...
...
@@ -2,14 +2,17 @@ package assignment1
import
"time"
//variable that keeps track of time
var
startTime
time
.
Time
//Uptime function
func
Uptime
()
float64
{
//return the time since start as seconds
return
time
.
Since
(
startTime
)
.
Seconds
()
}
//InitTime function
func
InitTime
()
{
//starts the timer
startTime
=
time
.
Now
()
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment