Skip to content
Snippets Groups Projects
Commit f61a72d0 authored by Mathilde Hertaas's avatar Mathilde Hertaas
Browse files

fixed handeling of correct up time count

parent 6254bc97
No related branches found
No related tags found
No related merge requests found
...@@ -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
...@@ -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)
......
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment