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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andrea Magnussen
Assignment1
Commits
d4e18109
Commit
d4e18109
authored
5 years ago
by
andmag
Browse files
Options
Downloads
Patches
Plain Diff
divided into different files
parent
815d8053
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
cmd/main.go
+25
-0
25 additions, 0 deletions
cmd/main.go
func.go
+85
-0
85 additions, 0 deletions
func.go
go.mod
+3
-0
3 additions, 0 deletions
go.mod
struct.go
+32
-0
32 additions, 0 deletions
struct.go
with
145 additions
and
0 deletions
cmd/main.go
0 → 100644
+
25
−
0
View file @
d4e18109
package
main
import
(
"assignment1"
"fmt"
"log"
"net/http"
"os"
)
func
main
()
{
port
:=
os
.
Getenv
(
"PORT"
)
if
port
==
""
{
port
=
"8080"
}
http
.
HandleFunc
(
"/"
,
assignment1
.
HandlerNil
)
http
.
HandleFunc
(
"/conservation/v1/country/"
,
assignment1
.
HandlerCountry
)
http
.
HandleFunc
(
"/conservation/v1/species/"
,
assignment1
.
HandlerSpecies
)
//http.HandleFunc("/conservation/v1/diag/", assignment1.HandlerDiag)
fmt
.
Println
(
"Listening on port "
+
port
)
log
.
Fatal
(
http
.
ListenAndServe
(
":"
+
port
,
nil
))
}
This diff is collapsed.
Click to expand it.
main
.go
→
func
.go
+
85
−
0
View file @
d4e18109
package
main
package
assignment1
import
(
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
)
// Coutry comment endpoint
type
Country
struct
{
Code
string
`json:"alpha2Code"`
Countryname
string
`json:"name"`
Countryflag
string
`json:"flag"`
//Categorycodes string `json:"category"`
//Speciescategorycount string `json:"`
}
func
handlerNil
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// HandlerNil kjhfkerjhgk
func
HandlerNil
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Println
(
"Default Handler: Invalid request received."
)
http
.
Error
(
w
,
"Invalid request"
,
http
.
StatusBadRequest
)
}
func
handlerCountry
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
APIURL
:=
"https://restcountries.eu/rest/v2/alpha/"
//SJEKK LENGDE 4
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
APIURL
+=
parts
[
4
]
func
replyRequest
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
url
string
,
c
string
)
{
http
.
Header
.
Add
(
w
.
Header
(),
"content-type"
,
"application/json"
)
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
APIURL
,
nil
)
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
url
,
nil
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
)
}
...
...
@@ -46,24 +29,57 @@ func handlerCountry(w http.ResponseWriter, r *http.Request) {
}
defer
resp
.
Body
.
Close
()
country
:=
&
Country
{}
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
country
)
switch
c
{
case
"country"
:
country
:=
&
Country
{}
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
country
)
json
.
NewEncoder
(
w
)
.
Encode
(
country
)
json
.
NewEncoder
(
w
)
.
Encode
(
country
)
case
"species"
:
species
:=
&
Species
{}
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
species
)
json
.
NewEncoder
(
w
)
.
Encode
(
species
)
}
}
func
main
()
{
// HandlerCountry dskjfhskjfhk
func
HandlerCountry
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
port
:=
os
.
Getenv
(
"PORT"
)
if
port
==
""
{
port
=
"8080"
}
APIURL
:=
"https://restcountries.eu/rest/v2/alpha/"
//SJEKK LENGDE 4
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
/*
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'", status)
return
}
*/
APIURL
+=
parts
[
4
]
c
:=
"country"
replyRequest
(
w
,
r
,
APIURL
,
c
)
}
// HandlerSpecies dfkjekjgh
func
HandlerSpecies
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
APIURL
:=
"http://api.gbif.org/v1/species/"
//SJEKK LENGDE 4
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
APIURL
+=
parts
[
4
]
c
:=
"species"
http
.
HandleFunc
(
"/"
,
handlerNil
)
http
.
HandleFunc
(
"/conservation/v1/country/"
,
handlerCountry
)
fmt
.
Println
(
"Listening on port "
+
port
)
log
.
Fatal
(
http
.
ListenAndServe
(
":"
+
port
,
nil
))
replyRequest
(
w
,
r
,
APIURL
,
c
)
}
This diff is collapsed.
Click to expand it.
go.mod
0 → 100644
+
3
−
0
View file @
d4e18109
module assignment1
go 1.13
This diff is collapsed.
Click to expand it.
struct.go
0 → 100644
+
32
−
0
View file @
d4e18109
package
assignment1
// Country dslkjfslkfjl
type
Country
struct
{
Code
string
`json:"alpha2Code"`
Countryname
string
`json:"name"`
Countryflag
string
`json:"flag"`
//Categorycodes string `json:"category"`
//Speciescategorycount string `json:"`
}
// Species comment endpoint
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"`
//SpeciesName string `json:"genericName"`
ScientificName
string
`json:"scientificName"`
CanonicalName
string
`json:"canonicalName"`
//Extinct bool `json:"extinct"`
}
// Diag comment endpoint
type
Diag
struct
{
Gbif
string
`json:""`
Restcountries
string
`json:""`
Version
string
`json:""`
uptime
string
`json:""`
}
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