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
815d8053
Commit
815d8053
authored
5 years ago
by
andmag
Browse files
Options
Downloads
Patches
Plain Diff
main
parent
52a0c375
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.go
+69
-0
69 additions, 0 deletions
main.go
with
69 additions
and
0 deletions
main.go
0 → 100644
+
69
−
0
View file @
815d8053
package
main
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
)
{
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
]
http
.
Header
.
Add
(
w
.
Header
(),
"content-type"
,
"application/json"
)
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
APIURL
,
nil
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
)
}
client
:=
http
.
DefaultClient
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
)
}
defer
resp
.
Body
.
Close
()
country
:=
&
Country
{}
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
country
)
json
.
NewEncoder
(
w
)
.
Encode
(
country
)
}
func
main
()
{
port
:=
os
.
Getenv
(
"PORT"
)
if
port
==
""
{
port
=
"8080"
}
http
.
HandleFunc
(
"/"
,
handlerNil
)
http
.
HandleFunc
(
"/conservation/v1/country/"
,
handlerCountry
)
fmt
.
Println
(
"Listening on port "
+
port
)
log
.
Fatal
(
http
.
ListenAndServe
(
":"
+
port
,
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