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
Mathilde Hertaas
assignment1
Commits
9a65f259
Commit
9a65f259
authored
2 months ago
by
Mathilde Hertaas
Browse files
Options
Downloads
Patches
Plain Diff
constants and english in status
parent
3bd06dcc
No related branches found
No related tags found
Loading
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
handelers/constants.go
+14
-2
14 additions, 2 deletions
handelers/constants.go
handelers/statushandler.go
+18
-34
18 additions, 34 deletions
handelers/statushandler.go
with
32 additions
and
36 deletions
handelers/
k
onstant
er
.go
→
handelers/
c
onstant
s
.go
+
14
−
2
View file @
9a65f259
...
...
@@ -27,6 +27,18 @@ const POPULATION_DESCRIPTION = "It requires the ISO 3166-2 country code and a op
const
POPULATION_URL
=
"http://localhost:8080/countryinfo/v1/population/{ISO_3166-2_country_code}{?startYear-endYear}"
const
POPULATION_URL_EXAMPLE
=
"http://localhost:8080/countryinfo/v1/population/NO?2010-2015}"
//information for client
//status
const
CNA
=
"countriesnowapi"
const
RCA
=
"restcountriesapi"
const
VERSION
=
"version"
const
UPTIME
=
"uptime"
const
VERSJON
=
"v1"
const
INTRO
=
"API status"
//information from server to client
const
FORMAT_JSON
=
"application/json"
const
HEADER_CONTENT_TYPE
=
"Content-Type"
\ No newline at end of file
const
HEADER_CONTENT_TYPE
=
"Content-Type"
//extern API
const
COUNTRIESNOW_API
=
"http://129.241.150.113:3500/api/v0.1/countries"
const
RESTCOUNTRIES_API
=
"http://129.241.150.113:8080/v3.1/all"
\ No newline at end of file
This diff is collapsed.
Click to expand it.
handelers/statushandler.go
+
18
−
34
View file @
9a65f259
...
...
@@ -2,59 +2,43 @@ package handelers
import
(
"net/http"
"fmt"
"encoding/json"
"time"
)
// Funksjon for å hente statuskode fra et API
// Function for getting status code from API
func
getAPIStatus
(
url
string
)
(
int
,
error
)
{
resp
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
return
0
,
err
}
defer
resp
.
Body
.
Close
()
// Returner HTTP-statuskoden
return
resp
.
StatusCode
,
nil
}
// Handler for Diagnostics endpoint
func
DiagnosticsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
serviceStartTime
time
.
Time
)
{
// Hent statuskoder fra eksterne API-er
countriesNowStatus
,
err
:=
getAPIStatus
(
"http://129.241.150.113:3500/api/v0.1/countries"
)
if
err
!=
nil
{
http
.
Error
(
w
,
"Error fetching CountriesNow status"
,
http
.
StatusInternalServerError
)
return
}
countriesNowStatus
,
err1
:=
getAPIStatus
(
COUNTRIESNOW_API
)
restCountriesStatus
,
err2
:=
getAPIStatus
(
RESTCOUNTRIES_API
)
restCountriesStatus
,
err
:=
getAPIStatus
(
"http://129.241.150.113:8080/v3.1/all"
)
//funker med all
if
err
!=
nil
{
http
.
Error
(
w
,
"Error fetching RestCountries status"
,
http
.
StatusInternalServerError
)
return
if
err1
!=
nil
{
countriesNowStatus
=
0
}
// Bygg responsen med statusinformasjon
response
:=
map
[
string
]
interface
{}{
"countriesnowapi"
:
fmt
.
Sprintf
(
"%d"
,
countriesNowStatus
),
"restcountriesapi"
:
fmt
.
Sprintf
(
"%d"
,
restCountriesStatus
),
"version"
:
"v1"
,
// Versjon av API
"uptime"
:
getUptime
(
serviceStartTime
),
// Oppetid i sekunder
if
err2
!=
nil
{
restCountriesStatus
=
0
}
// Sett Content-Type til application/json
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
// Returner JSON-responsen
json
.
NewEncoder
(
w
)
.
Encode
(
response
)
response
:=
map
[
string
]
any
{
CNA
:
countriesNowStatus
,
RCA
:
restCountriesStatus
,
VERSION
:
VERSJON
,
UPTIME
:
getUptime
(
serviceStartTime
)
,
}
w
.
Header
()
.
Set
(
FORMAT_JSON
,
FORMAT_JSON
)
formattedResponse
,
_
:=
json
.
MarshalIndent
(
response
,
""
,
" "
)
w
.
Write
(
formattedResponse
)
}
// Funksjon for å beregne oppetid
func
getUptime
(
serviceStartTime
time
.
Time
)
int64
{
return
int64
(
time
.
Since
(
serviceStartTime
)
.
Seconds
())
// Bruk serviceStartTime fra parameter
return
int64
(
time
.
Since
(
serviceStartTime
)
.
Seconds
())
}
\ No newline at end of file
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