Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assignment1
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
Johannes Tangen
Assignment1
Commits
42b0dbbf
Commit
42b0dbbf
authored
Oct 20, 2019
by
jotangen
Browse files
Options
Downloads
Patches
Plain Diff
/Diag fixed with json
parent
d31a584a
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
assignment1
+0
-0
0 additions, 0 deletions
assignment1
main.go
+38
-18
38 additions, 18 deletions
main.go
webapp.go
+24
-21
24 additions, 21 deletions
webapp.go
with
62 additions
and
39 deletions
assignment1
+
0
−
0
View file @
42b0dbbf
No preview for this file type
This diff is collapsed.
Click to expand it.
main.go
+
38
−
18
View file @
42b0dbbf
package
main
import
(
"encoding/json"
"fmt"
"html"
"log"
...
...
@@ -14,14 +15,23 @@ import (
// URL given to r, page data loaded.
// formatted HTML is then sent to the page via W
func
urlSplitForCountry
(
a
string
)(
string
,
string
){
const
lengthUrl
=
5
// 0/1=conserv/2=v1/3=country/4=
name/5=
no&limit=10
// 0/1=conserv/2=v1/3=country/4=no&limit=10
partsOne
:=
strings
.
Split
(
a
,
"/"
)
// 0=no & 1=limit=no
partsTwo
:=
strings
.
Split
(
partsOne
[
5
],
"&"
)
// 0=no & 1=limit=10
partsTwo
:=
strings
.
Split
(
partsOne
[
4
],
"&"
)
// 0=&limit 1=10
partsThree
:=
strings
.
Split
(
partsOne
[
5
],
"="
)
partsThree
:=
strings
.
Split
(
partsOne
[
4
],
"="
)
fmt
.
Printf
(
" PartsOne: %d
\n
PartsOne[4]: %d
\n
"
,
len
(
partsOne
),
len
(
partsOne
[
4
]))
if
len
(
partsOne
)
==
lengthUrl
{
CountryCode
:=
partsTwo
[
0
]
fmt
.
Printf
(
"CountryCode is: %s
\n
"
,
CountryCode
)
...
...
@@ -30,6 +40,11 @@ func urlSplitForCountry(a string)(string, string){
fmt
.
Printf
(
"Limit is: %s
\n
"
,
Limit
)
return
CountryCode
,
Limit
}
else
{
fmt
.
Printf
(
"Remember to use /conservation/v1/country/<value>&limit=<value> "
)
return
""
,
""
}
}
func
defaultHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
){
...
...
@@ -45,13 +60,15 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
CountryCode
,
Limit
:=
urlSplitForCountry
(
r
.
URL
.
Path
)
// Get country
Country
,
Ccode
,
Flag
:=
getCountryName
(
CountryCode
)
Country
Info
:=
getCountryName
(
CountryCode
)
fmt
.
Fprintf
(
w
,
"Country name is %s
\n
Country code is: %s
\n
Flag is: %s
\n
"
,
Country
,
Ccode
,
Flag
)
//
fmt.Fprintf(w,"Country name is %s\n Country code is: %s\n Flag is: %s\n ", Country, Ccode, Flag)
// get species from a given country
Species
:=
getSpeciesByCountry
(
CountryCode
,
Limit
)
fmt
.
Fprintf
(
w
,
"Species %v
\n
"
,
Species
)
//fmt.Fprintf(w,"Species %v\n",Species.Collection)
json
.
NewEncoder
(
w
)
.
Encode
(
CountryInfo
)
json
.
NewEncoder
(
w
)
.
Encode
(
Species
)
}
func
speciesHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
){
...
...
@@ -66,16 +83,16 @@ Species := getSpecies(speciesKey)
Year
:=
getYearForSpecie
(
speciesKey
)
// write to web
fmt
.
Fprintf
(
w
,
" Species: %v
\n
Year: %s
\n
"
,
Species
,
Year
)
//fmt.Fprintf(w," Species: %v\n Year: %s\n ", Species, Year)
json
.
NewEncoder
(
w
)
.
Encode
(
Species
)
json
.
NewEncoder
(
w
)
.
Encode
(
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
)
Diagnostic
:=
getDiagnostic
()
fmt
.
Fprintf
(
w
,
" Version: %s
\n
UpTime: %s
\n
"
,
Version
,
Time
)
json
.
NewEncoder
(
w
)
.
Encode
(
Diagnostic
)
}
// Global var type time
...
...
@@ -85,6 +102,9 @@ var Start time.Time
func
main
()
{
port
:=
os
.
Getenv
(
"PORT"
)
// Remove this when deploy
port
=
"8080"
if
port
==
""
{
//log.fatal formatting output and exits
log
.
Fatal
(
"$PORT must be set"
)
...
...
@@ -98,7 +118,7 @@ func main() {
// Add code to this function
http
.
HandleFunc
(
"/"
,
defaultHandler
)
http
.
HandleFunc
(
"/conservation/v1/country/
name/
"
,
viewHandler
)
http
.
HandleFunc
(
"/conservation/v1/country/"
,
viewHandler
)
http
.
HandleFunc
(
"/conservation/v1/species/"
,
speciesHandler
)
...
...
This diff is collapsed.
Click to expand it.
webapp.go
+
24
−
21
View file @
42b0dbbf
...
...
@@ -57,56 +57,58 @@ type SpeciesKeyBody struct{
type
SpeciesYear
struct
{
//Hint: For the year, check the /species/{key}/name path for a given key.
Year
str
in
g
`json:"bracketYear"`
Year
in
t
`json:"bracketYear"`
}
func
getDiagnostic
()
(
int
,
int
,
string
,
time
.
Duration
)
{
type
Diagnostics
struct
{
Gbif
int
Restcountries
int
Version
string
Uptime
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>
func
getDiagnostic
()
Diagnostics
{
*/
//res := Response{}
urlGBIF
:=
apiRootGBIF
respDiag
,
err
:=
http
.
Get
(
urlGBIF
)
fmt
.
Printf
(
"Dette er vår respDiag%s
\n
"
,
respDiag
)
if
err
!=
nil
{
fmt
.
Println
(
"Error:we have a problem"
,
err
)
return
0
,
0
,
""
,
0
return
Diagnostics
{}
}
defer
respDiag
.
Body
.
Close
()
//json.NewDecoder(respDiag.Body).Decode(ci)
//json.NewDecoder(respDiag.Body).Decode(diag.Gbif)
urlRestCountr
:=
apiRootRestCountr
respCountr
,
err
:=
http
.
Get
(
urlRestCountr
)
if
err
!=
nil
{
fmt
.
Println
(
"Error:we have a problem"
,
err
)
return
0
,
0
,
""
,
0
return
Diagnostics
{}
}
defer
respCountr
.
Body
.
Close
()
//json.NewDecoder(respCountr.Body).Decode(diag.Restcountries)
t
:=
time
.
Now
()
elapsed
:=
t
.
Sub
(
Start
)
diag
:=
Diagnostics
{
respDiag
.
StatusCode
,
respCountr
.
StatusCode
,
version
,
elapsed
}
return
respDiag
.
StatusCode
,
respCountr
.
StatusCode
,
version
,
elapsed
return
diag
}
//Should return the name Norway
func
getCountryName
(
country
Nam
e
string
)
(
string
,
string
,
string
)
{
func
getCountryName
(
country
Cod
e
string
)
CntrInfo
{
// Instance of struct
ci
:=
&
CntrInfo
{}
ci
:=
CntrInfo
{}
//Change hardcoded to func parameters "We pass them"
//countryCode := "NO"
//Implement passed parameters
url
:=
apiRoot
+
country
Nam
e
url
:=
apiRoot
+
country
Cod
e
fmt
.
Println
(
"Dette er vår url"
,
url
)
//resp is json, so we need to parse it
...
...
@@ -117,7 +119,7 @@ func getCountryName(countryName string) (string, string, string) {
if
err
!=
nil
{
fmt
.
Println
(
"Error:we have a problem"
,
err
)
return
""
,
""
,
""
return
ci
}
//Closes when surrounded functions are finished
...
...
@@ -126,10 +128,10 @@ func getCountryName(countryName string) (string, string, string) {
//resp.Body JSON
//NewDecoder returns a new decoder that reads from r
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
ci
)
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
ci
)
fmt
.
Println
(
"after json decoder"
,
ci
)
return
ci
.
Name
,
ci
.
CountryCode
,
ci
.
Flag
return
ci
}
// Should return a struct with species and key that has occurrence in a pre defined country
...
...
@@ -181,8 +183,9 @@ func getSpecies (SpeciesKey string) SpeciesKeyBody {
}
func
getYearForSpecie
(
SpeciesKey
string
)
SpeciesYear
{
const
Name
=
"name"
const
Name
=
"
/
name"
sy
:=
SpeciesYear
{}
// http://api.gbif.org/v1/species/5231190/name
url
:=
apiRootSpecies
+
SpeciesKey
+
Name
respYear
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
...
...
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