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
Mathilde Hertaas
assignment1
Commits
f61a72d0
Commit
f61a72d0
authored
4 months ago
by
Mathilde Hertaas
Browse files
Options
Downloads
Patches
Plain Diff
fixed handeling of correct up time count
parent
6254bc97
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
handelers/statushandler.go
+10
-9
10 additions, 9 deletions
handelers/statushandler.go
main.go
+9
-1
9 additions, 1 deletion
main.go
main/main.go
+26
-0
26 additions, 0 deletions
main/main.go
with
45 additions
and
10 deletions
handelers/statushandler.go
+
10
−
9
View file @
f61a72d0
...
@@ -2,13 +2,13 @@ package handelers
...
@@ -2,13 +2,13 @@ package handelers
import
(
import
(
"net/http"
"net/http"
"time"
"fmt"
"fmt"
"encoding/json"
"encoding/json"
"time"
)
)
var
serviceStartTime
time
.
Time
// Funksjon for å hente statuskode fra et API
// Funksjon for å hente statuskode fra et API
func
getAPIStatus
(
url
string
)
(
int
,
error
)
{
func
getAPIStatus
(
url
string
)
(
int
,
error
)
{
...
@@ -22,14 +22,10 @@ func getAPIStatus(url string) (int, error) {
...
@@ -22,14 +22,10 @@ func getAPIStatus(url string) (int, error) {
return
resp
.
StatusCode
,
nil
return
resp
.
StatusCode
,
nil
}
}
// Funksjon for å beregne oppetid
func
getUptime
()
int64
{
return
int64
(
time
.
Since
(
serviceStartTime
)
.
Seconds
())
}
// Handler for Diagnostics endpoint
// Handler for Diagnostics endpoint
func
DiagnosticsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
DiagnosticsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
serviceStartTime
time
.
Time
)
{
serviceStartTime
=
time
.
Now
()
// Hent statuskoder fra eksterne API-er
// Hent statuskoder fra eksterne API-er
countriesNowStatus
,
err
:=
getAPIStatus
(
"http://129.241.150.113:3500/api/v0.1/countries"
)
countriesNowStatus
,
err
:=
getAPIStatus
(
"http://129.241.150.113:3500/api/v0.1/countries"
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -48,7 +44,7 @@ func DiagnosticsHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -48,7 +44,7 @@ func DiagnosticsHandler(w http.ResponseWriter, r *http.Request) {
"countriesnowapi"
:
fmt
.
Sprintf
(
"%d"
,
countriesNowStatus
),
"countriesnowapi"
:
fmt
.
Sprintf
(
"%d"
,
countriesNowStatus
),
"restcountriesapi"
:
fmt
.
Sprintf
(
"%d"
,
restCountriesStatus
),
"restcountriesapi"
:
fmt
.
Sprintf
(
"%d"
,
restCountriesStatus
),
"version"
:
"v1"
,
// Versjon av API
"version"
:
"v1"
,
// Versjon av API
"uptime"
:
getUptime
(),
// Oppetid i sekunder
"uptime"
:
getUptime
(
serviceStartTime
),
// Oppetid i sekunder
}
}
// Sett Content-Type til application/json
// Sett Content-Type til application/json
...
@@ -57,3 +53,8 @@ func DiagnosticsHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -57,3 +53,8 @@ func DiagnosticsHandler(w http.ResponseWriter, r *http.Request) {
// Returner JSON-responsen
// Returner JSON-responsen
json
.
NewEncoder
(
w
)
.
Encode
(
response
)
json
.
NewEncoder
(
w
)
.
Encode
(
response
)
}
}
// Funksjon for å beregne oppetid
func
getUptime
(
serviceStartTime
time
.
Time
)
int64
{
return
int64
(
time
.
Since
(
serviceStartTime
)
.
Seconds
())
// Bruk serviceStartTime fra parameter
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
main.go
+
9
−
1
View file @
f61a72d0
...
@@ -3,11 +3,19 @@ package main
...
@@ -3,11 +3,19 @@ package main
import
(
import
(
"fmt"
"fmt"
"net/http"
"net/http"
"time"
"git.gvk.idi.ntnu.no/Mathilde/cloud/handelers"
"git.gvk.idi.ntnu.no/Mathilde/cloud/handelers"
)
)
var
serviceStartTime
time
.
Time
func
main
()
{
func
main
()
{
http
.
HandleFunc
(
"/status/"
,
handelers
.
DiagnosticsHandler
)
serviceStartTime
=
time
.
Now
()
http
.
HandleFunc
(
"/status/"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
handelers
.
DiagnosticsHandler
(
w
,
r
,
serviceStartTime
)
})
http
.
HandleFunc
(
"/"
,
handelers
.
HomeHandler
)
http
.
HandleFunc
(
"/"
,
handelers
.
HomeHandler
)
http
.
HandleFunc
(
"/countryinfo/v1/info/"
,
handelers
.
CountryInfoHandler
)
http
.
HandleFunc
(
"/countryinfo/v1/info/"
,
handelers
.
CountryInfoHandler
)
...
...
This diff is collapsed.
Click to expand it.
main/main.go
0 → 100644
+
26
−
0
View file @
f61a72d0
package
main
import
(
"fmt"
"net/http"
"time"
"git.gvk.idi.ntnu.no/Mathilde/cloud/handelers"
)
var
serviceStartTime
time
.
Time
func
main
()
{
serviceStartTime
=
time
.
Now
()
// Send serviceStartTime til DiagnosticsHandler
http
.
HandleFunc
(
"/status/"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
handelers
.
DiagnosticsHandler
(
w
,
r
,
serviceStartTime
)
})
http
.
HandleFunc
(
"/"
,
handelers
.
HomeHandler
)
http
.
HandleFunc
(
"/countryinfo/v1/info/"
,
handelers
.
CountryInfoHandler
)
fmt
.
Println
(
"Server running on port 8080..."
)
if
err
:=
http
.
ListenAndServe
(
":8080"
,
nil
);
err
!=
nil
{
fmt
.
Printf
(
"Server failed to start: %s
\n
"
,
err
)
}
}
\ 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